java.net.ProtocolException:流意外结束 [英] java.net.ProtocolException: unexpected end of stream

查看:17193
本文介绍了java.net.ProtocolException:流意外结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在面临一个奇怪的问题,我不能够调试它。我已经实现上传数据流和现在用排球为同一个逻辑,我已经定制在 HurlStack addBodyIfExists API,因此类型application /八位字节流的该机构可以办理。

I am facing a strange issue, and I am not able to debug it out. I have implemented a logic for uploading stream of data and am using Volley for the same, I have customized a logic little bit in HurlStack, addBodyIfExists api,so that body of type "application/octet-stream" can be handled.

我的逻辑是张贴到用户的进步,使UI可以表明用户上传的进度进行更新,下面我的逻辑一样。

My logic is to post progress to user, so that UI can be updated indicating user progress in upload, below my logic for same.

            int toRead = length; // File length
            byte[] data = new byte[4096];
            connection.setDoOutput(true);
            if(length != -1) {
                connection.setFixedLengthStreamingMode(length);
            } else {
                connection.setChunkedStreamingMode(4096);
            }

            OutputStream os;
            int i;
            int count;

            os = connection.getOutputStream();
            int progress= 0;

               try {
                    for(i = 0; (count= is.read(data)) > 0; ++i) { // is, is not null and contains a valid input stream
                        os.write(data, 0, count); // at this line am getting unexpected end of stream
                        progress+= count;
                        if(i % 20 == 0) {
                            rs.deliverProgress(progress, 0L);
                            progress= 0;
                        }
                    }

                    os.flush();
                } finally {
                    if(is != null) {
                        is.close();
                    }

                    if(os != null) {
                        os.close();
                    }

                }

在code上午上面执行得到这个,虽然我已经验证,输出流不为空,也不做输入流,它在读取循环本身的第一次迭代中失败了,我看到它已经阅读4096字节,然后试图写的一样。

on executing above code am getting this, although I have verified, output stream is not null, neither do input stream, it fails in first iteration of read loop itself, am seeing it has read 4096 bytes and then trying to write the same.

java.net.ProtocolException: unexpected end of stream
            at com.android.okhttp.internal.http.HttpConnection$FixedLengthSink.close(HttpConnection.java:326)
            at com.android.okio.RealBufferedSink.close(RealBufferedSink.java:174)
            at com.android.okio.RealBufferedSink$1.close(RealBufferedSink.java:142)

在上面调试任何帮助将他高度AP preciated。

any help in debugging above will he highly appreciated.

推荐答案

<一个href=\"http://stackoverflow.com/questions/8202813/unexpected-end-of-stream-error-on-download\">This可以帮助你:

这异常是由FixedLengthInputStream抛出时所期望的字节数(通常在响应的内容长度的头设置)比在响应中的实际数据更大。检查内容长度的头是正确的。 (如果你正在为提供内容长度自己的价值,确保它是正确的。)

That exception is thrown by FixedLengthInputStream when the expected number of bytes (usually set in the content-length header of the response) is larger than the actual data in the response. Check that the content-length header is correct. (If you're supplying your own value for the content length, make sure it is correct.)

这将有助于你看到code,设置了输入流。

It would help to see your code that sets up the input stream.

这篇关于java.net.ProtocolException:流意外结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆