网络连接失败后恢复文件

时间:2019-05-02 06:28:58

标签: android file download resume general-network-error

我正在编写一个程序以从Azure下载文件。但是在网络出现故障后它不会恢复。该文件正在使用IntentService在后台下载。我提到了this.,我想跟踪我已下载文件的哪些部分,如果被中断,请恢复到我离开的地方。我什至无法在Google上找到任何东西。如果有人知道发生网络错误时如何恢复文件的解决方案。帮助将不胜感激。

try {
            byte data[] = new byte[1024 * 4];
            fileSize = body.contentLength();
            InputStream bis = new BufferedInputStream(body.byteStream(), 1024 * 8);
            mFile = ContextCompat.getExternalFilesDirs(this, null)[1];
            File outputFile = new File(FILE_STORAGE_PATH + File.separator + "download", fileName);
            OutputStream output = new FileOutputStream(outputFile);
            long total = 0;
            long startTime = System.currentTimeMillis();
            int timeCount = 1;
            while ((count = bis.read(data)) != -1) {
                total += count;
                totalFileSize = (int) (fileSize / (Math.pow(1024, 2)));
                double current = Math.round(total / (Math.pow(1024, 2)));
                int progress = (int) ((total * 100) / fileSize);
                long currentTime = System.currentTimeMillis() - startTime;
                Download download = new Download();
                download.setTotalFileSize(totalFileSize);
                download.setFileSize(fileSize);
                download.setFileName(fileName);
                download.setFileVersion(fileVersion);
                if (currentTime > 1000 * timeCount) {
                    download.setCurrentFileSize((int) current);
                    download.setProgress(progress);
                    sendNotification(download);
                    timeCount++;
                }
                output.write(data, 0, count);
            }
            onDownloadComplete();
            output.flush();
            output.close();
            bis.close();
        } catch (Exception e) {
            Log.e("Error: ", e.getMessage());
        }

0 个答案:

没有答案