简体   繁体   English

使用apache HttpClient 4.1.2上传3 GB文档

[英]Upload 3 GB document using apache HttpClient 4.1.2

We are using HttpClient 4.1.2. 我们正在使用HttpClient 4.1.2。 When we try to upload a document to the server, which is working good if we try to upload a document of size less than 2 GB, If I upload more than 2 GB document then I see the error below.. 当我们尝试将文档上载到服务器时,如果尝试上载小于2 GB的文档,则效果很好,如果上载超过2 GB的文档,则会看到以下错误。

java.net.SocketException: Connection reset by peer: socket write error
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
    at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
    at com.sun.net.ssl.internal.ssl.OutputRecord.writeBuffer(OutputRecord.java:297)
    at com.sun.net.ssl.internal.ssl.OutputRecord.write(OutputRecord.java:286)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.writeRecordInternal(SSLSocketImpl.java:743)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.writeRecord(SSLSocketImpl.java:731)
    at com.sun.net.ssl.internal.ssl.AppOutputStream.write(AppOutputStream.java:59)
    at org.apache.http.impl.io.AbstractSessionOutputBuffer.write(AbstractSessionOutputBuffer.java:153)
    at org.apache.http.impl.io.ContentLengthOutputStream.write(ContentLengthOutputStream.java:114)
    at org.apache.commons.io.output.ProxyOutputStream.write(ProxyOutputStream.java:90)
    at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:1859)

Please find the code below: 请在下面找到代码:

try {
    // Requires a multi-part post
    MultipartEntity requestEntity = new MultipartEntity();
    post.setEntity(requestEntity);

    requestEntity.addPart(MIME_TYPE_NAME, new StringBody(mimeType));

    requestEntity.addPart(USER_ID_PARAMETER, new StringBody(loginUser
            .getUserid().toLowerCase()));
    requestEntity.addPart(USER_PASSWORD_PARAMETER, new StringBody(
            loginUser.getPassword()));

    requestEntity.addPart(DOCUMENT_PART_NAME,
            new ProgressReportingFileBody(filePath, uploadListener));

    post.getParams().setBooleanParameter(
            CoreProtocolPNames.USE_EXPECT_CONTINUE, true);

    // Find out what happened with the request
    HttpContext context = new BasicHttpContext();
    // Add AuthCache to the execution context
    context.setAttribute(ClientContext.AUTH_CACHE, authCache);
    // Perform the POST
    setCurrentRequest(post);
    HttpResponse response = httpClient.execute(targetHost, post,
            context);
    // Get the response back
    message = response.getEntity();

    // Ensure the middleware returned an acceptable response
    checkError(response, context);
    checkInterrupted(post);

    Header documentId = response.getFirstHeader("DOCUMENT_ID");

    oDoc.sMimeType = mimeType;

} catch (ClientProtocolException ex) {
    throw new IwException(-3, "HTTP Error: " + ex.getMessage(),
            ex.toString());
} catch (IOException ex) {
    checkInterrupted(post);
    log.error("Failed to Upload Document", ex);
    throw new IwException(-3, "Upload Document Error: "
            + ex.getMessage(), ex.toString());
} finally {
    try {
        if (message != null) {
            // Clean up to allow another HTTP client call
            EntityUtils.consume(message);
        }
    } catch (IOException ex) {
        log.error("Failed to Close HTTP Connection", ex);
    }
}

Any ideas what it could be / would be great! 任何想法可能是/将是伟大的! Thanks for your time. 谢谢你的时间。

Upload in chunks, each having size of 256 Mb or about. 分块上传,每个块大小为256 Mb或大约256 Mb。 Join on the sever side. 在服务器端加入。 On the programming level, this is easy to do. 在编程级别,这很容易做到。 Also, uploading 3 Gb probably takes very long. 另外,上传3 Gb可能会花费很长时间。 In case of failure, it makes sense to have possibility to retry with smaller chunk. 如果发生故障,则有可能使用较小的块重试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM