【发布时间】:2013-09-16 18:45:53
【问题描述】:
我们使用的是 HttpClient 4.1.2。 当我们尝试将文档上传到服务器时,如果我们尝试上传大小小于 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)
请在下面找到代码:
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);
}
}
任何想法可能/会很棒!感谢您的宝贵时间。
【问题讨论】:
-
对端是服务器...这可能不是客户端的问题。
-
我们使用的是 WebSphere 8。
-
感谢您的回复 Owlstead。服务器端的哪些更改可以解决此问题?
-
您的操作系统、文件系统是否支持此功能? 2 Gb 的限制曾经很常见。
-
不推荐使用传统的 Java HTTP 库和 Java HTTP API 来上传大文件。使用面向 I/O 的框架 (Netty) 是一种不同的方法(即不是 http)。编辑:大文件意味着> 2GB
标签: java apache apache-httpclient-4.x