java多线程下载网络资源
程序员文章站
2022-05-05 20:42:17
...
在java中,我们可以通过URL和HttpURLConnection实现对资源的多线程下载,以此来减少资源的下载时间,下面是一个实现多线程下载的例子,不足的是没实现断点下载的功能
编写一个测试类,传入一个资源路径,当然前提是该资源网站没有设置防盗链
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
public class DownLoadTool{
public void resourceDownLoad(String path) throws Exception {
//网络资源路径
//如path = "http://www.hackvip.com/desk/d/0/3/2006110911245141091.jpg";
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setConnectTimeout(6*1000);
conn.setRequestMethod("GET");
//获得网络上文件的大小
int fileSize = conn.getContentLength();
//线程的数量
int threadNum = 4;
//我用三个线程对文件进行下载,每个线程下载量为
int block = fileSize/threadNum + 1;
conn.disconnect();
//保存在项目的根目录下,如我下载的是一张图片
File file = new File("a.jpg");
RandomAccessFile rfile = new RandomAccessFile(file,"rw");
rfile.setLength(fileSize);
rfile.close();
for (int i = 0; i < threadNum; i++) {
int startPosition = i*block;
RandomAccessFile randomFile = new RandomAccessFile(file,"rw");
//从文件的什么位置开始写入
randomFile.seek(startPosition);
new DownLoadThread(i,url,randomFile,startPosition,block).start();
}
byte[] b = new byte[1];
System.in.read(b);
while(!(b[0]=='q')){
Thread.sleep(5*1000);
}
}
private class DownLoadThread extends Thread{
private URL url = null;
private RandomAccessFile rfile = null;
private int startPosition = 0;
private int block = 0;
private int i = 0;
public DownLoadThread(int i,URL url, RandomAccessFile rfile,
int startPosition, int block) {
this.i = i;
this.url = url;
this.rfile = rfile;
this.startPosition = startPosition;
this.block = block;
}
@Override
public void run() {
try {
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Range", "bytes" + startPosition + "-");
conn.setConnectTimeout(6*1000);
InputStream is = conn.getInputStream();
byte[] buffer = new byte[1024];
int len = -1;
int readSize = 0;
while(readSize<block && ((len=is.read(buffer))!=-1)){
rfile.write(buffer, 0, len);
readSize += len;
}
rfile.close();
is.close();
conn.disconnect();
System.out.println("第" + (++i) + "个线程下载完成");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
编写一个测试类,传入一个资源路径,当然前提是该资源网站没有设置防盗链
import org.junit.Test;
public class DownLoadTest {
@Test
public void test() {
String path = "http://www.hackvip.com/desk/d/0/3/2006110911245141091.jpg";
try {
new DownLoadTool().resourceDownLoad(path);
} catch (Exception e) {
e.printStackTrace();
}
}
}
上一篇: C语言学习笔记(六)数组
下一篇: 使用java下载网络资源