实现暂停/恢复的文件下载 [英] Implement pause/resume in file downloading

查看:171
本文介绍了实现暂停/恢复的文件下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现暂停/恢复我的下载管理器,我在网上搜索并阅读了几篇文章,并根据他们改变我的code,但履历表似乎无法正常工作,任何想法?

 如果(!downloadPath.exists())
                    downloadPath.mkdirs();

                如果(outputFileCache.exists())
                {
                    downloadedSize = outputFileCache.length();
                    connection.setAllowUserInteraction(真正的);
                    connection.setRequestProperty(范围,字节=+ downloadedSize + - );
                    connection.setConnectTimeout(14000);
                    connection.connect();
                    输入=新的BufferedInputStream(connection.getInputStream());
                    输出=新的FileOutputStream(outputFileCache,真正的);
                    input.skip(downloadedSize); //跳过下载大小
                }
                其他
                {
                    connection.setConnectTimeout(14000);
                    connection.connect();
                    输入=新的BufferedInputStream(url.openStream());
                    输出=新的FileOutputStream(outputFileCache);
                }

                文件长度= connection.getContentLength();


                字节的数据[] =新的字节[1024];
                诠释计数= 0;
                INT __progress = 0;
                长总= downloadedSize;

                而((计数= input.read(数据))= -1放大器;!&安培;!this.isInterrupted())
                {
                    共有+ =计数;
                    output.write(数据,0,计数);
                    __progress =(int)的(总* 100 /文件长度);

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

解决方案

好了固定的问题,这是我的code为其他用户谁想要实现暂停/恢复:

 如果(outputFileCache.exists())
        {
            connection.setAllowUserInteraction(真正的);
            connection.setRequestProperty(范围,字节=+ outputFileCache.length()+ - );
        }

        connection.setConnectTimeout(14000);
        connection.setReadTimeout(20000);
        connection.connect();

        如果(connection.getResponse code()/ 100!= 2)
            抛出新的异常(无效响应code!);
        其他
        {
            字符串connectionField = connection.getHeaderField(内容范围);

            如果(connectionField!= NULL)
            {
                的String [] connectionRanges = connectionField.substring(字节=长度()。)分裂( - )。
                downloadedSize = Long.valueOf(connectionRanges [0]);
            }

            如果(connectionField == NULL和放大器;&安培; outputFileCache.exists())
                outputFileCache.delete();

            文件长度= connection.getContentLength()+ downloadedSize;
            输入=新的BufferedInputStream(connection.getInputStream());
            输出=新的RandomAccessFile(outputFileCache,RW);
            output.seek(downloadedSize);

            字节的数据[] =新的字节[1024];
            诠释计数= 0;
            INT __progress = 0;

            而((计数= input.read(数据,0,1024))!= -1
                    &功放;&安培; __progress!= 100)
            {
                downloadedSize + =计数;
                output.write(数据,0,计数);
                __progress =(int)的((downloadedSize * 100)/文件长度);
            }

            output.close();
            input.close();
       }
 

I'm trying to implement pause/resume in my download manager, I search the web and read several articles and change my code according them but resume seems not working correctly, Any ideas?

                if (!downloadPath.exists()) 
                    downloadPath.mkdirs(); 

                if (outputFileCache.exists())
                {
                    downloadedSize = outputFileCache.length();
                    connection.setAllowUserInteraction(true);
                    connection.setRequestProperty("Range", "bytes=" + downloadedSize + "-");
                    connection.setConnectTimeout(14000);
                    connection.connect();
                    input = new BufferedInputStream(connection.getInputStream());
                    output = new FileOutputStream(outputFileCache, true);
                    input.skip(downloadedSize); //Skip downloaded size
                }
                else
                {
                    connection.setConnectTimeout(14000);
                    connection.connect();
                    input = new BufferedInputStream(url.openStream());
                    output = new FileOutputStream(outputFileCache);
                }

                fileLength = connection.getContentLength();                 


                byte data[] = new byte[1024];
                int count = 0;
                int __progress = 0;
                long total = downloadedSize;

                while ((count = input.read(data)) != -1 && !this.isInterrupted()) 
                {
                    total += count;
                    output.write(data, 0, count);
                    __progress = (int) (total * 100 / fileLength);

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

解决方案

Okay problem fixed, here is my code for other users who wants to implement pause/resume:

        if (outputFileCache.exists())
        {
            connection.setAllowUserInteraction(true);
            connection.setRequestProperty("Range", "bytes=" + outputFileCache.length() + "-");
        }

        connection.setConnectTimeout(14000);
        connection.setReadTimeout(20000);
        connection.connect();

        if (connection.getResponseCode() / 100 != 2)
            throw new Exception("Invalid response code!");
        else
        {
            String connectionField = connection.getHeaderField("content-range");

            if (connectionField != null)
            {
                String[] connectionRanges = connectionField.substring("bytes=".length()).split("-");
                downloadedSize = Long.valueOf(connectionRanges[0]);
            }

            if (connectionField == null && outputFileCache.exists())
                outputFileCache.delete();

            fileLength = connection.getContentLength() + downloadedSize;
            input = new BufferedInputStream(connection.getInputStream());
            output = new RandomAccessFile(outputFileCache, "rw");
            output.seek(downloadedSize);

            byte data[] = new byte[1024];
            int count = 0;
            int __progress = 0;

            while ((count = input.read(data, 0, 1024)) != -1 
                    && __progress != 100) 
            {
                downloadedSize += count;
                output.write(data, 0, count);
                __progress = (int) ((downloadedSize * 100) / fileLength);
            }

            output.close();
            input.close();
       }

这篇关于实现暂停/恢复的文件下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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