在提供大型文件供下载时,客户端丢弃连接(Java,Jersey,HTTP,GET)

问题描述 投票:0回答:1

我有一个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;
}

FileStreamingOutputjavax.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之前,我尝试用OutputStreamFile直接建立响应。结果相同。

附加要求:我们不能介绍其他框架(例如Spring ......)

关于如何克服这一限制的任何想法?

java rest http jersey large-files
1个回答
0
投票

我通过在Content-Length标题中指定要传输的文件的大小,即通过添加以下行来简单地解决问题(!):

oResponseBuilder.header("Content-Length", oFile.length());
© www.soinside.com 2019 - 2024. All rights reserved.