2013-03-05 21 views
0

我的應用程序分發文件和收集統計信息。 前段時間我注意到如果用戶嘗試從android下載文件 - 服務器收到兩個請求,而不是一個。第一次請求失敗,原因是Caused by: java.net.SocketException: Connection reset by peer: socket write error 第二次請求處理正確,用戶在手機上收到文件。所以它或多或少都可以,但在這種情況下,我的統計數據不正確。我看到similiar問題上[其他線程] [1]Android瀏覽器問題。下載使兩個請求,而不是一個

[1]:https://stackoverflow.com/a/8579181/273418但目前還沒有任何解決方案

的servlet代碼分發文件

try { 
    StringBuilder typeHeader = new StringBuilder("application/vnd.android.package-archive"); 
    String contentType = FdConstants.CONTENT_TYPE_HEADER.getValue(); 
    response.setHeader(contentType, typeHeader.toString()); 
    StringBuilder dispositionHeader = new StringBuilder("attachment; filename=\""); 
    dispositionHeader.append(fileName.toUpperCase()); 
    dispositionHeader.append("\""); 
    String contentDisposition = FdConstants.CONTENT_DISPOSITION_HEADER.getValue(); 
    response.setHeader(contentDisposition, dispositionHeader.toString()); 
    response.setContentLength(fileStream.available()); 
    // copy it to response's OutputStream 
    IOUtils.copy(fileStream, response.getOutputStream()); 
    response.flushBuffer(); 

} finally { 
    IOUtils.closeQuietly(fileStream); 
} 
+0

如何下載文件?請添加一些代碼。 – RvdK 2013-03-05 14:32:54

+0

通過網址瀏覽器 – Diyko 2013-03-05 14:59:15

+0

你爲什麼然後談論'你的''應用程序?您是否使用WebView,Android上的不同瀏覽器(海豚/鉻),電視上的瀏覽器在自定義應用程序中嘗試過它? – RvdK 2013-03-05 15:20:35

回答

相關問題