Java实现文件下载断点续传(一)

参考文章:https://www.ibm.com/developerworks/cn/java/joy-down/

1.原理介绍

想象一下我们下载一个10G的文件,当下载到9.99G的时候断网了。。。

断点续传支持从文件上次中断的地方开始传送数据,而并非是从文件开头传送。

体现在HTTP请求和响应上就会有区别

假设下载文件为:

http://dl_dir.qq.com/invc/cyclone/QQDownload_Setup_39_708_p1.exe

普通文件下载的请求和响应

断点续传时候的请求和响应

这地方需要拦截HttpURLConnection的请求。

 

2.如何用Java实现

主要利用的是RandomAccessFile类的seek方法。

RandomAccessFile的特点在于任意访问文件的任意位置,是基于字节访问的;
可通过getFilePointer()获取当前指针所在位置 ;
可通过seek()移动指针,这体现了它的任意性;
seek用于设置文件指针位置,设置后会从当前指针的下一位读取或写入。

这地方我在IBM文章基础上简化并调整了一版,自测了下。搬上来分享一下。

 

 

2.1 工具类

package com.laoxu.demo.breaktransfer;

public class Utility {
    public Utility()
    {
    }
    public static void sleep(int nSecond)
    {
        try{
            Thread.sleep(nSecond);
        }
        catch(Exception e)
        {
            e.printStackTrace ();
        }
    }
    public static void log(String sMsg)
    {
        System.err.println(sMsg);
    }
    public static void log(int sMsg)
    {
        System.err.println(sMsg);
    }
}

2.2 文件信息实体

package com.laoxu.demo.breaktransfer;

/**
 *  远程下载文件信息
 */
public class SiteFileInfo {
    private String sSiteURL; //文件下载链接
    private String sFilePath; //保存的文件路径
    private String sFileName; //保存的文件名称
    public SiteFileInfo()
    {
        this("","","");
    }
    public SiteFileInfo(String sURL, String sPath, String sName)
    {
        sSiteURL= sURL;
        sFilePath = sPath;
        sFileName = sName;
    }
    public String getSSiteURL()
    {
        return sSiteURL;
    }
    public void setSSiteURL(String value)
    {
        sSiteURL = value;
    }
    public String getSFilePath()
    {
        return sFilePath;
    }
    public void setSFilePath(String value)
    {
        sFilePath = value;
    }
    public String getSFileName()
    {
        return sFileName;
    }
    public void setSFileName(String value)
    {
        sFileName = value;
    }
}

2.3 写文件类

package com.laoxu.demo.breaktransfer;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.Serializable;

/**
 *  写文件
 */
public class FileAccess implements Serializable{
    RandomAccessFile oSavedFile;
    long nPos;
    public FileAccess() throws IOException
    {
        this("",0);
    }
    public FileAccess(String sName, long nPos) throws IOException
    {
        oSavedFile = new RandomAccessFile(sName,"rw");
        this.nPos = nPos;
        oSavedFile.seek(nPos);
    }
    public synchronized int write(byte[] b,int nStart,int nLen)
    {
        int n = -1;
        try{
            oSavedFile.write(b,nStart,nLen);
            n = nLen;
        }
        catch(IOException e)
        {
            e.printStackTrace ();
        }
        return n;
    }
}

2.4 主程序

package com.laoxu.demo.breaktransfer;

import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;

/**
 * @Description: 断点续传主程序
 * @Author laoxu
 * @Date 2019/12/7 15:31
 **/
public class SiteFileFetch{
    SiteFileInfo fileInfo = null; // 文件信息实体
    long startPos;    // 开始位置
    long endPos;      // 结束位置
    long fileLength; // 文件长度

    File localFile = null;          // 保存的文件
    String localFilePath;      // 本地文件路径
    boolean bFirst = true; // 是否第一次取文件
    boolean bStop = false; // 停止标志


    public SiteFileFetch(SiteFileInfo entity){
        fileInfo = entity;
        localFilePath = entity.getSFilePath()+File.separator+entity.getSFileName();
        localFile = new File(localFilePath);
        // 判断本地文件是否存在
        if(localFile.exists()){
            startPos = localFile.length();
            bFirst = false;
        }
        fileLength = getFileSize();

        if(fileLength < 0){
            System.err.println("非法文件!");
        }else{
            endPos = fileLength;
        }

    }

    public void download() {
        if(startPos<endPos){
            try {
                FileAccess fileAccess = new FileAccess(localFilePath,startPos);

                // 配置fiddler代理
                Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 8888));
                URL url = new URL(fileInfo.getSSiteURL());
                HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection(proxy);

                httpConnection.setRequestProperty("User-Agent", "LX");
                String sProperty = "bytes=" + startPos + "-";
                httpConnection.setRequestProperty("RANGE", sProperty);
                Utility.log(sProperty);
                InputStream input = httpConnection.getInputStream();
                //logResponseHead(httpConnection);
                byte[] b = new byte[1024];
                int nRead;
                while ((nRead = input.read(b, 0, 1024)) > 0 && startPos < endPos
                        && !bStop) {
                    startPos += fileAccess.write(b, 0, nRead);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else {
            System.out.println("文件已存在!");
        }

    }

    // 获得文件长度
    private long getFileSize() {
        int nFileLength = -1;
        try {
            URL url = new URL(fileInfo.getSSiteURL());
            HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
            httpConnection.setRequestProperty("User-Agent", "Lx");
            int responseCode = httpConnection.getResponseCode();
            if (responseCode >= 400) {
                System.out.println(responseCode);
                return -2; //-2 represent access is error
            }
            String sHeader;
            for (int i = 1; ; i++) {
                sHeader = httpConnection.getHeaderFieldKey(i);
                if (sHeader != null) {
                    if (sHeader.equals("Content-Length")) {
                        nFileLength = Integer.parseInt(httpConnection.getHeaderField(sHeader));
                        break;
                    }
                } else
                    break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Utility.log(nFileLength);
        return nFileLength;
    }
}

2.5 测试程序

package com.laoxu.demo.breaktransfer;

public class TestMethod {
    public TestMethod() {
        try {
            SiteFileInfo fileInfo = new SiteFileInfo("http://dl_dir.qq.com/invc/cyclone/QQDownload_Setup_39_708_p1.exe",
                    "D:\\tmp",
                    "QQDownload_Setup_39_708_p1.exe");
            SiteFileInfo fileInfo2 = new SiteFileInfo("http://www.luohanye.com/astros.json",
                    "D:\\tmp",
                    "astros.json");
            SiteFileFetch fileFetch = new SiteFileFetch(fileInfo);
            fileFetch.download();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new TestMethod();
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2019-12-08 00:59  一锤子技术员  阅读(74)  评论(0编辑  收藏  举报  来源