[英]Connection dropped by client when serving large files for download (Java, Jersey, HTTP, GET)
我有一個HTTP服務器,該服務器提供下載文件,其中一些文件非常大(可以為7 GB或更大)。 從某些網絡下載這些文件時,連接斷開,我們在tomcat catalina日志中發現以下錯誤:
org.apache.catalina.connector.ClientAbortException: java.io.IOException: Connection reset by peer
at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:393)
at org.apache.tomcat.util.buf.ByteChunk.flushBuffer(ByteChunk.java:426)
at org.apache.tomcat.util.buf.ByteChunk.append(ByteChunk.java:339)
at org.apache.catalina.connector.OutputBuffer.writeBytes(OutputBuffer.java:418)
at org.apache.catalina.connector.OutputBuffer.write(OutputBuffer.java:406)
at org.apache.catalina.connector.CoyoteOutputStream.write(CoyoteOutputStream.java:97)
at org.glassfish.jersey.message.internal.CommittingOutputStream.write(CommittingOutputStream.java:229)
at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:2147)
...
SEVERE: org.glassfish.jersey.server.ServerRuntime$Responder: An I/O error has occurred while writing a response message entity to the container output stream.
org.glassfish.jersey.server.internal.process.MappableException: org.apache.catalina.connector.ClientAbortException: java.io.IOException: Broken pipe
...
(讓我知道是否需要更多日志行)
我們認為這些錯誤是由於某些防火牆/代理/ NAT配置引起的:對於某些網絡,我們還確定了確定性降低連接的閾值。 我們需要找到一種解決這些問題的方法,而無需要求客戶端更改其配置(可以這樣做,因為相同的客戶端可以通過HTTP從Dropbox下載相同的文件)。 此外,必須對客戶端進行身份驗證(即,具有有效的會話)。
這是我的最新代碼:
@GET
@Path("download")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response download(@HeaderParam("x-session-token") String sSessionId, @QueryParam("filename") String sFileName)
{
User oUser = MyLib.GetUserFromSession(sSessionId);
if (oUser == null) {
return Response.status(Status.UNAUTHORIZED).build();
}
File oFile = getEntryFile(sFileName);
ResponseBuilder oResponseBuilder = null;
if(oFile == null) {
oResponseBuilder = Response.serverError();
} else {
FileStreamingOutput oStream = new FileStreamingOutput(oFile);
oResponseBuilder = Response.ok(oStream);
oResponseBuilder.header("Content-Disposition", "attachment; filename="+ oFile.getName());
}
return oResponseBuilder.build();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
FileStreamingOutput
是javax.ws.rs.core.StreamingOutput
的擴展:
public class FileStreamingOutput implements StreamingOutput {
final File m_oFile;
public FileStreamingOutput(File oFile){
if(null==oFile) {
throw new NullPointerException("FileStreamingOutput.FileStreamingOutput: passed a null File");
}
m_oFile = oFile;
}
@Override
public void write(OutputStream oOutputStream) throws IOException, WebApplicationException {
if(null == oOutputStream) {
throw new NullPointerException("FileStreamingOutput.write: passed a null OutputStream");
}
InputStream oInputStream = null;
try {
oInputStream = new FileInputStream(m_oFile);
long lThreshold = 2L*1024*1024*1024;
long lSize = m_oFile.length();
if(lSize > lThreshold) {
IOUtils.copyLarge(oInputStream, oOutputStream);
} else {
IOUtils.copy(oInputStream, oOutputStream);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if( oOutputStream!=null ) {
oOutputStream.flush();
oOutputStream.close();
}
if( oInputStream !=null ) {
oInputStream.close();
}
}
}
}
在使用StreamingOutput
之前,我嘗試使用OutputStream
或直接使用File
構建響應。 結果相同。
附加要求:我們不能引入其他框架(例如Spring ...)
關於如何克服此限制的任何想法?
我通過在Content-Length
標頭中指定要傳輸的文件的大小來簡單地解決了該問題(!),即,添加了以下行:
oResponseBuilder.header("Content-Length", oFile.length());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.