Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当我使用HttpClient时,为什么我的数据流会意外结束?_Java_Apache Commons Httpclient - Fatal编程技术网

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

Java 当我使用HttpClient时,为什么我的数据流会意外结束?,java,apache-commons-httpclient,Java,Apache Commons 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.setRequestHe

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

我的代码如下所示:

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) 异常文本如下所示:

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) 致命传输错误:分块流意外结束 线程“main”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的数据。

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

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

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

第一-

Java客户端错误“通过http Java.io.IOException发送数据时出错异常:块末尾应为CRLF:79/82 java.io.IOException:块末尾应为CRLF:79/82“

第二-

来自Java Clinet的错误“通过http发送数据时出错Java.io.IOException:分块流意外结束 java.io.IOException:分块流意外结束“

通过使用分块流大小更改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

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

第一-

Java客户端错误“通过http Java.io.IOException发送数据时出错异常:块末尾应为CRLF:79/82 java.io.IOException:块末尾应为CRLF:79/82“

第二-

来自Java Clinet的错误“通过http发送数据时出错Java.io.IOException:分块流意外结束 java.io.IOException:分块流意外结束“

通过使用分块流大小更改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

Note=nDE替换为n229

我想说的是,当代码执行方法时会发生异常。getResponseBody()试图找出如何添加我看到的异常文本…你在那里写了一篇大文章。上传需要多长时间?(可能只是一个超时吗?)几分钟,也许有一个超时。我一直在“玩”代码,发现100MB的文件没有问题,175MB的文件也没有问题,所以我得到了我有问题的文件的新副本,并将它们放在与我没有问题的文件相同的文件夹中,突然之间,他们上传了没有任何分块的流,却意外地结束了异常。嗯,这很奇怪。因此,其他一些事情正在发生,它只是触发服务器厌倦并关闭您的底层流。跟踪这类问题到可以修复的特定问题是非常困难的。我想说的是,当代码执行方法时会发生异常。getResponseBody()试图找出如何添加我看到的异常文本…你在那里做了一篇大文章。上传需要多长时间?(可能只是一个超时吗?)几分钟,也许有一个超时。我一直在“玩”代码,发现100MB的文件没有问题,175MB的文件也没有问题,所以我得到了我有问题的文件的新副本,并将它们放在与我没有问题的文件相同的文件夹中,突然之间,他们上传了没有任何分块的流,却意外地结束了异常。嗯,这很奇怪。因此,其他一些事情正在发生,它只是触发服务器厌倦并关闭您的底层流。追踪这类问题到可以解决的特定问题是非常困难的。你好,Malasani,你能在你的错误代码中添加一个代码块和一两个短语来解释你的更改吗?谢谢你好,Malasani,你能给你的错误代码添加一个代码块和一两个短语来解释你的更改吗?谢谢