Filedownloader: 下载待更新的apk文件,结果还未下载完成就回调completed()

Created on 10 Feb 2017  ·  6Comments  ·  Source: lingochamp/FileDownloader

下载待更新的apk文件,结果还未下载完成就回调completed(),最终数据是:sofar:61384501byte;total:61550308byte;
首先在MyApllication的onCreate()中调用如下所示:

FileDownloader.init(getApplicationContext());
然后在DownloadService中调用如下所示:
FileDownloader.getImpl().create(apkDownloadUrl) .setPath(apkSaveDir, true) .setCallbackProgressTimes(300) .setMinIntervalUpdateSpeed(400) .setListener(new FileDownloadListener() { // 在回调函数里做相应的UI处理 }).start();
最终结果:sofar:61384501;total:61550308; 而且文件不是*.apk而是fd6f241bcbc7dde4051ad5b1b550502a

question

All 6 comments

这个是不可能出现的,demo中几千的一起下载都没有遇到过类似的问题。

在下载的最后如果sofar不等于total会直接回调error,可参考code

            // Step 8, Compare between the downloaded so far bytes with the total bytes.
            if (soFar == total) {

                // Step 9, rename the temp file to the completed file.
                renameTempFile();

                // Step 10, remove data from DB.
                helper.remove(mId);

                // callback completed
                onComplete(total);

                return true;
            } else {
                throw new RuntimeException(
                        FileDownloadUtils.formatString("sofar[%d] not equal total[%d]", soFar, total));
            }

有可能原因

是否是启动了多个下载,都是写入同一个文件导致的,如果是这个问题,关注下这个issue: #471

你可以DEBUG下上面的code位置看看(DEBUG的时候需要注意,要默认情况下这个是在下载进程中执行,因此要DEBUG下载进程),这样也可以有助于你定位问题。

首先非常谢谢这么快就回答我的问题!其次很抱歉,是我个人疏忽了,文件其实完全下载了,只是最后下载完成的那一次没有调用progress()而是直接调用completed(),我一时情急做出错误的判断,静下心来adb shell进去看是已经完全下载完成的,可以关掉这个问题了。
最后,简单问下我直接使用NotificationSampleActivity这里的功能,那么setPath()里的参数pathAsDirectory无论设置与否怎么保存从服务器获取的文件名和后缀呢?(代码还没看完,所以想作者您简单指点下,没时间回答也没关系,仍旧谢谢!)

关于BaseDownloadTask#setPath中的第二个参数,你可以留一下JavaDoc,当pathAsDirectory被设置为true以后,所传入的path将会被当做存储的文件夹路径,而文件名会从响应头的contentDisposition从获取,如果响应头中不存在这个字段,便会通过FileDownloadUtil#generateFileName进行生成文件名

/**
 * @param path            The absolute path for saving the download file.
 * @param pathAsDirectory {@code true}: if the {@code path} is absolute directory to store the
 *                        downloading file, and the {@code filename} will be found in
 *                        contentDisposition from the response as default, if can't find
 *                        contentDisposition,the {@code filename} will be generated by
 *                        {@link FileDownloadUtils#generateFileName(String)}  with {@code url}.
 *                        </p>
 *                        {@code false}: if the {@code path} = (absolute directory/filename).
 * @see #isPathAsDirectory()
 * @see #getFilename()
 */
BaseDownloadTask setPath(final String path, final boolean pathAsDirectory);

真心很抱歉打扰你过元宵节!其次很感谢回答问题速度太快了,刚看到了下确实是响应头里没有contentDisposition这个字段,祝元宵节快乐!!!

@Res2013 哈哈,谢谢。我一个人在杭州,也没啥气氛,没有打扰到。

Was this page helpful?
0 / 5 - 0 ratings