2013-09-16 54 views
0

我們使用的是HttpClient 4.1.2。 當我們嘗試上傳文件到服務器時,如果我們嘗試上傳大小小於2 GB的文檔,這很有效,如果我上傳2 GB以上的文檔,那麼我會看到下面的錯誤信息:使用apache上傳3 GB文件HttpClient 4.1.2

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) 

請在下面找到代碼:

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); 
    } 
} 

任何想法可能是什麼/是偉大的!謝謝你的時間。

+0

對等體是服務器......這可能不是客戶端的問題。 –

+0

我們正在使用WebSphere 8. – user2371505

+0

感謝您的回覆Owlstead。服務器端的哪些變化可以解決這個問題? – user2371505

回答

1

分塊上傳,每塊大小爲256 Mb或者大約。加入服務器端。在編程層面上,這很容易做到。另外,上傳3 GB可能需要很長時間。如果發生故障,可以使用較小的塊重試。

+0

較小的塊將更實際,因爲HTTP開銷請求的大小相對較小。 –

+0

謝謝Audrius。我確實接受這個選擇。我們有各種文件(如視頻,JPEG或任何格式)。不確定我們將如何分割任何類型的文檔? – user2371505

+0

如果您以後在內容本身上獨立正確加入塊,則內容不會丟失。您不需要知道或關心實際的文檔格式。 – h22

相關問題