Java多线程使用线程池实现文件下载

版权声明:本博文仅供学习、参考、技术讨论,版权归笔者/译者所有。 https://blog.csdn.net/qq_38025219/article/details/84315550

多线程下载原理:

1、基本思路是将文件分段切割、分段传输、分段保存。

2、分段切割用到HttpUrlConnection对象的setRequestProperty(“Range”, “bytes=” + start + “-” + end)方法。

3、分段传输用到HttpUrlConnection对象的getInputStream()方法。

4、分段保存用到RandomAccessFile的seek(int start)方法。

5、创建指定长度的线程池,循环创建线程,执行下载操作。

代码设计:

总共代码分为三个类,DownloadWithRange 、DownloadFileWithThreadPool和test。其中test相当于实际使用中框架中的controll层代码。DownloadWithRange 实现Runnable接口,DownloadFileWithThreadPool主要是线程池操作和调用DownloadWithRange 类。test就是一个测试类。

package file;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
/**
 *   2018/11/21.
 */
public class DownloadWithRange implements Runnable
{
    private String urlLocation;

    private String filePath;

    private long start;

    private long end;

    DownloadWithRange(String urlLocation, String filePath, long start, long end)
    {
        this.urlLocation = urlLocation;
        this.filePath = filePath;
        this.start = start;
        this.end = end;
    }

    @Override
    public void run()
    {
        try
        {
            HttpURLConnection conn = getHttp();
            conn.setRequestProperty("Range", "bytes=" + start + "-" + end);

            File file = new File(filePath);
            RandomAccessFile out = null;
            if (file != null)
            {
                out = new RandomAccessFile(file, "rwd");
            }
            out.seek(start);
            InputStream in = conn.getInputStream();
            byte[] b = new byte[1024];
            int len = 0;
            while ((len = in.read(b)) != -1)
            {
                out.write(b, 0, len);
            }
            in.close();
            out.close();
        }
        catch (Exception e)
        {
            e.getMessage();
        }

    }

    public HttpURLConnection getHttp() throws IOException
    {
        URL url = null;
        if (urlLocation != null)
        {
            url = new URL(urlLocation);
        }
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(5000);
        conn.setRequestMethod("GET");

        return conn;
    }

}

package file;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
/**
 *   2018/11/21.
 */
public class DownloadFileWithThreadPool
{
    public void getFileWithThreadPool(String urlLocation,String filePath, int poolLength) throws IOException
    {
        Executor threadPool = Executors.newFixedThreadPool(poolLength);
        
        long len = getContentLength(urlLocation);
        System.out.println("文件大小:"+len);
        for(int i=0;i<poolLength;i++)
        {
            long start=i*len/poolLength;
            long end = (i+1)*len/poolLength-1;
            if(i==poolLength-1)
            {
                end =len;
            }
            DownloadWithRange download=new DownloadWithRange(urlLocation, filePath, start, end);
            threadPool.execute(download);
            try {
				Thread.sleep(2);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
    }

    public static long getContentLength(String urlLocation) throws IOException
    {
        URL url = null;
        if (urlLocation != null)
        {
            url = new URL(urlLocation);
        }
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(5000);
        conn.setRequestMethod("GET");
        long len = conn.getContentLength();

        return len;
    }
}


package file;

import java.io.IOException;



public class test {
	 public static  void main(String [] args){
		 DownloadFileWithThreadPool dfw = new DownloadFileWithThreadPool();
		 try {  // "http://10.128.99.13"+"/usr/ulitech/dataroot/scadadata/2018/11.tar.gz"
			 long startTime=System.currentTimeMillis(); 
			 dfw.getFileWithThreadPool("http://10.128.99.13:8080"+"/sysobjects/sysobjects.db","E:/1/11.rar",10);
			 long endTime=System.currentTimeMillis();  
			 System.out.println("耗费时间: "+(endTime-startTime)+" ms");
			 System.out.println("下载成功!");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	    }
}

思考:

1.对于下载路径处理?路径的有效性?
2.多个小文件的效率?
后续进行分析和解答。。。。。。。。。。

猜你喜欢

转载自blog.csdn.net/qq_38025219/article/details/84315550