Android部分下载的文件报告文件大小错误 [英] Android Partially Downloaded Files Report Wrong File Size

查看:172
本文介绍了Android部分下载的文件报告文件大小错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在整理一些代码以从Android中的HTTP地址下载文件。如果下载在途中失败,我想支持恢复下载。

I'm putting together some code to download files from an HTTP address in Android. I'd like to support download resumption if the download fails mid way.

开始下载时获得的输出,然后终止wifi连接并再次重新启动几次:以下内容:

The output I get when starting the download, then killing the wifi connection and restarting again several times is the following:

开始大小0
停止大小12333416
开始大小12333416
停止大小 16058200
开始大小 3724784

我不明白为什么在第一次恢复后,部分下载的文件的后续文件大小读数不匹配。

I cannot understand why after the first resumption, subsequent file size readings of the partially downloaded file do not match.

预先感谢!

public void download(String source, String target) throws IOException {
    BufferedOutputStream outputStream = null;
    InputStream inputStream = null;

    try {
        File targetFile = new File(target);
        currentBytes = targetFile.length();

        Log.i(TAG, "Start size " + String.valueOf(currentBytes));
        outputStream = new BufferedOutputStream(new FileOutputStream(targetFile));

        // create the input stream
        URLConnection connection = (new URL(source)).openConnection();
        connection.setConnectTimeout(mCoTimeout);
        connection.setReadTimeout(mSoTimeout);

        inputStream = connection.getInputStream();
        inputStream.skip(currentBytes);

        // calculate the total bytes
        totalBytes = connection.getContentLength();

        byte[] buffer = new byte[1024];
        int bytesRead;

        while ((bytesRead = inputStream.read(buffer)) > 0) {
            // write the bytes to file
            outputStream.write(buffer, 0, bytesRead);
            outputStream.flush();

            currentBytes += bytesRead;
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (outputStream != null) {
            // close the output stream
            outputStream.flush();
            outputStream.close();
        }

        if (inputStream != null) {
            // close the input stream
            inputStream.close();
        }

        Log.i(TAG, "Stop size " + String.valueOf(currentBytes));
    }
}


推荐答案

有您做错了两件事:


  1. 要恢复下载到文件,您应该附加文件而不是重写文件。对输出流使用特殊的构造函数:

  1. To resume download to file you should append, not rewrite the file. Use special constructor for output stream:

FileOutputStream(targetFile,true)

FileOutputStream(targetFile, true)

请求部分内容服务器上的文件,您应该使用HTTP 1.1属性 Range。您可以这样操作:

To request part of file from server you should use HTTP 1.1 property "Range". You can do it like this:

HttpURLConnection httpConnection =(HttpURLConnection)连接;
connection.setRequestProperty( Range, bytes = + currentBytes +-);

HttpURLConnection httpConnection = (HttpURLConnection) connection; connection.setRequestProperty("Range", "bytes=" + currentBytes + "-");

这篇关于Android部分下载的文件报告文件大小错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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