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

多线程下载原理:

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

{

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.多个小文件的效率?

后续进行分析和解答。。。。。。。。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值