有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java当我使用HttpClient时,为什么我的数据流会意外结束?

这个问题似乎只出现在我试图发布的“大”文件中

我的代码如下所示:

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

异常文本如下所示:

Fatal transport error: chunked stream ended unexpectedly
Exception in thread "main" java.io.IOException: chunked stream ended unexpectedly
    at org.apache.commons.httpclient.ChunkedInputStream.getChunkSizeFromInputStream(ChunkedInputStream.java:252)
    at org.apache.commons.httpclient.ChunkedInputStream.nextChunk(ChunkedInputStream.java:221)
    at org.apache.commons.httpclient.ChunkedInputStream.read(ChunkedInputStream.java:176)
    at java.io.FilterInputStream.read(FilterInputStream.java:127)
    at org.apache.commons.httpclient.AutoCloseInputStream.read(AutoCloseInputStream.java:108)
    at java.io.FilterInputStream.read(FilterInputStream.java:101)
    at org.apache.commons.httpclient.AutoCloseInputStream.read(AutoCloseInputStream.java:127)
    at org.apache.commons.httpclient.HttpMethodBase.getResponseBody(HttpMethodBase.java:690)

无论使用getResponseBody()还是getResponseBodyAsStream(),我都会遇到类似的异常

我不应该得到太多的数据,但我发布了超过200mb的数据


共 (2) 个答案

  1. # 1 楼答案

    可能老了,可以节省一些时间。。。。。 我遇到了一个错误,服务器是Python,Clinet是Java

    1-

    Error from Java Client "Error while sending data over http java.io.IOException: CRLF expected at end of chunk: 79/82 java.io.IOException: CRLF expected at end of chunk: 79/82"

    第二-

    Error from Java Clinet "Error while sending data over http java.io.IOException: chunked stream ended unexpectedly java.io.IOException: chunked stream ended unexpectedly"

    这两个错误都通过使用分块流大小更改ok响应得到解决

    一个有问题

    HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nTransfer-Encoding: chunked\r\nServer: Jetty(6.1.26)\r\n\r\nDE\r\n
    

    决议如下:

    HTTP/1.1 200 OK\r\nContent-Length: 20000\r\nContent-Type: application/json\r\nTransfer-Encoding: chunked\r\nServer: Jetty(6.1.26)\r\n\r\n229\r\n
    

    注=nDE替换为n229

  2. # 2 楼答案

    我可以通过改变PostMethod的requestHeader中指定的文件名值的长度来解决这个问题。我在请求头中包含了完整文件路径名的编码版本。通过反复试验,我发现我“发布”的文件的成败似乎取决于它所在的文件夹。一个长文件夹文件路径名不起作用,而一个短文件夹文件路径名(尽管与同一个文件)起作用。所以我从请求头中删除了路径名,只开始包含文件名,我不再看到问题