java – 错误时自动恢复的HTTP Async Downloader

我的应用程序中有一个异步下载器,但有时连接丢失,尤其是当我在移动连接上并且文件很大(> 10 MB)时.
有没有办法在下载停止时捕获,然后强制它继续完成下载的结果?

这是异步任务doInBackground:

protected String doInBackground(String... aurl) {
    int count;

    try {

        URL url = new URL(aurl[0]);
        URLConnection conexion = url.openConnection();
        // conexion.setRequestProperty("Range","bytes=" + downloaded + "-");
        conexion.connect();

        int lenghtOfFile = conexion.getContentLength();
        pesoVideo = lenghtOfFile / 1048576;
        output = "/sdcard/" + folderString + "/" + nomeFile + ".mp3";
        InputStream input = new BufferedInputStream(url.openStream());
        OutputStream output = new FileOutputStream(
        VideoDownloaderBrowserActivity.this.output);

        byte data[] = new byte[1024];
        long total = 0;

        while ((count = input.read(data)) != -1) {
            total += count;
            publishProgress("" + (int) ((total * 100) / lenghtOfFile));
            output.write(data,count);
        }

        output.flush();
        output.close();
        input.close();

    } catch (Exception e) {
    }

    return null;
}

这是onProgressUpdate:

protected void onProgressUpdate(String... progress) {
    if (Integer.parseInt(progress[0]) > progresso) {
        ...
    }
}

解决方法

这是一个 thread讨论在API 9下面的Android中的可恢复下载.否则 DownloadManager对于较新版本也是一个不错的选择.

基本上,您需要在服务器上启用字节服务以允许可恢复的下载.

相关文章

本文从从Bitcask存储模型讲起,谈轻量级KV系统设计与实现。从...
内部的放到gitlab pages的博客,需要统计PV,不蒜子不能准确...
PCM 自然界中的声音非常复杂,波形极其复杂,通常我们采用的...
本文介绍如何离线生成sst并在线加载,提供一种用rocksdb建立...
验证用户输入是否正确是我们应用程序中的常见功能。Spring提...
引入pdf2dom <dependency> <groupId&a...