这个问题似乎只出现在我试图发布的“大”文件中。
我的代码如下所示:
PostMethod method = new PostMethod(url);
File input = new File(filePathname);
RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");
method.setRequestEntity(entity);
method.setRequestHeader("Content-Disposition", "attachment; filename=xyzzy")
HttpClient client = new HttpClient();
Credentials defaultcreds = new UsernamePasswordCredentials("userid", "pw");
client.getState().setCredentials(new AuthScope("hostname", port, AuthScope.ANY_REALM), defaultcreds);
try {
int statusCode = client.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
throw new Exception("Method failed: " + method.getStatusLine());
}
// Read the response body.
byte[] responseBody = method.getResponseBody();
return new String(responseBody);
}
catch (HttpException e) {
System.err.println("Fatal protocol violation: " + e.getMessage());
throw e;
}
catch (IOException e) {
System.err.println("Fatal transport error: " + e.getMessage());
throw e;
}
finally {
// Release the connection.
method.releaseConnection();
}
异常文本如下所示:
致命传输错误:分块流意外结束 线程“主”java.io.IOException 中的异常:分块流意外结束 在 org.apache.commons.httpclient.ChunkedInputStream.getChunkSizeFromInputStream(ChunkedInputStream.java:252) 在 org.apache.commons.httpclient.ChunkedInputStream.nextChunk(ChunkedInputStream.java:221) 在 org.apache.commons.httpclient.ChunkedInputStream.read(ChunkedInputStream.java:176) 在 java.io.FilterInputStream.read(FilterInputStream.java:127) 在 org.apache.commons.httpclient.AutoCloseInputStream.read(AutoCloseInputStream.java:108) 在 java.io.FilterInputStream.read(FilterInputStream.java:101) 在 org.apache.commons.httpclient.AutoCloseInputStream.read(AutoCloseInputStream.java:127) 在 org.apache.commons.httpclient.HttpMethodBase.getResponseBody(HttpMethodBase.java:690)
无论我使用 getResponseBody() 还是 getResponseBodyAsStream(),都会遇到类似的异常。
我不应该得到太多数据,但我发布了超过 200mb 的数据。