gpt4 book ai didi

android - 下载时流错误意外结束?

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:07:09 24 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Android:“Unexpected end of stream” exception downloading large files

我正在下载一个大约的文件。 5MB 使用 HttpURLConnection 但在下载过程中途我在我的代码的这一行收到 “流的意外结束” 错误:

                     while ((count = input.read(data)) > 0) {

这是日志:

11-20 16:05:55.749: ERROR/PRINTSTACK(3425): STACK:unexpected end of stream
11-20 16:05:55.749: WARN/System.err(3425): java.io.IOException: unexpected end of stream
11-20 16:05:55.749: WARN/System.err(3425): at org.apache.harmony.luni.internal.net.www.protocol.http.FixedLengthInputStream.read(FixedLengthInputStream.java:47)
11-20 16:05:55.749: WARN/System.err(3425): at java.io.BufferedInputStream.read(BufferedInputStream.java:319)
11-20 16:05:55.749: WARN/System.err(3425): at java.io.FilterInputStream.read(FilterInputStream.java:133)
11-20 16:05:55.759: WARN/System.err(3425): at com.conjure.skiproj.DownloadService$Job.process(DownloadService.java:265)
11-20 16:05:55.759: WARN/System.err(3425): at com.conjure.skiproj.DownloadService$1.run(DownloadService.java:193)
11-20 16:05:55.759: WARN/System.err(3425): at java.lang.Thread.run(Thread.java:1019)

求助!!1

编辑:关于设置输入流的代码的更多信息:

    HttpURLConnection conexion = (HttpURLConnection)url.openConnection();
conexion.setRequestMethod("GET");
conexion.setReadTimeout(20000);
conexion.connect();
File file = new File(root.getAbsolutePath()+"/", fileName);

int lenghtOfFile = conexion.getContentLength();

InputStream input = new BufferedInputStream(url.openStream());

OutputStream output = new FileOutputStream(file);

byte data[] = new byte[8192];
...........................

编辑 2:在以下行的 25% 下载中出现新错误 IndexOutOfBoundsException:

  output.write(data, 0, count);

日志如下:

11-20 17:47:02.311: ERROR/totaltotal(303): 24
11-20 17:47:02.311: INFO/System.out(303): countcountcount:4332
11-20 17:47:02.330: ERROR/totaltotal(303): 24
11-20 17:47:02.330: INFO/System.out(303): countcountcount:2904
11-20 17:47:02.330: ERROR/totaltotal(303): 25
11-20 17:47:02.330: INFO/System.out(303): countcountcount:1452
11-20 17:47:02.330: ERROR/totaltotal(303): 25
11-20 17:47:02.650: INFO/System.out(303): countcountcount:4356
11-20 17:47:02.650: ERROR/totaltotal(303): 25
11-20 17:47:02.650: INFO/System.out(303): countcountcount:-1
11-20 17:47:02.660: ERROR/totaltotal(303): 25
11-20 17:47:02.892: DEBUG/dalvikvm(303): GC_FOR_MALLOC freed 10770 objects / 490896 bytes in 143ms
11-20 17:47:03.060: ERROR/PRINTSTACK(303): STACK:Arguments out of bounds
11-20 17:47:03.060: WARN/System.err(303): java.lang.IndexOutOfBoundsException: Arguments out of bounds
11-20 17:47:03.070: WARN/System.err(303): at java.io.FileOutputStream.write(FileOutputStream.java:288)
11-20 17:47:03.080: WARN/System.err(303): at com.conjure.skiproj.DownloadService$Job.process(DownloadService.java:275)
11-20 17:47:03.080: WARN/System.err(303): at com.conjure.skiproj.DownloadService$1.run(DownloadService.java:191)
11-20 17:47:03.080: WARN/System.err(303): at java.lang.Thread.run(Thread.java:1096)

编辑 3:我将错误追溯到 FixedLengthInputStream 类,然后进一步追溯到 AbstractHttpInputStream 类,其中有这个方法:

/**
* Calls abort on the cache entry and disconnects the socket. This
* should be invoked when the connection is closed unexpectedly to
* invalidate the cache entry and to prevent the HTTP connection from
* being reused. HTTP messages are sent in serial so whenever a message
* cannot be read to completion, subsequent messages cannot be read
* either and the connection must be discarded.
*
* <p>An earlier implementation skipped the remaining bytes, but this
* requires that the entire transfer be completed. If the intention was
* to cancel the transfer, closing the connection is the only solution.
*/
protected final void unexpectedEndOfInput() {
if (cacheRequest != null) {
cacheRequest.abort();
}
httpURLConnection.releaseSocket(false);
}

所以似乎在 Http 消息错误的情况下,整个下载流都被取消了。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com