多线程下载文件

参考网络代码修改


import java.io.File;

import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;




public class MultiDowloadThread {


public static void main(String[] args) {
String url = "http://127.0.0.1:7001/ext4api.zip";
String filePath = "D:\\1.txt";
new DownloadFile(5, url, filePath).download();
}

}




class DownloadFile {

private File localFile;
private String url;
private long fileSize;
private int threadCount;
private String filePath;

public DownloadFile(int count,String url,String filePath){
this.threadCount = count;
this.url = url;
this.filePath = filePath;
}

public void download(){

this.createFileByUrl();

long block = this.fileSize % this.threadCount == 0 ? this.fileSize / this.threadCount : this.fileSize / this.threadCount + 1;

for(int i=0;i<this.threadCount;i++){
long start = i * block;
long end = start + block >= this.fileSize ? this.fileSize : start + block - 1;
new DownloadFileThread(this.url, this.localFile, start, end).start();
}
}

private void createFileByUrl() {
try {
this.localFile = new File(this.filePath);
URL u = new URL(url);
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
conn.setConnectTimeout(15 * 1000);
conn.setRequestMethod("GET");
conn.setRequestProperty(
"Accept",
"image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
conn.setRequestProperty("Accept-Language", "zh-CN");
conn.setRequestProperty("Referer", url);
conn.setRequestProperty("Charset", "UTF-8");
conn.setRequestProperty(
"User-Agent",
"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");
conn.setRequestProperty("Connection", "Keep-Alive");


conn.connect();


if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
RandomAccessFile raf = new RandomAccessFile(this.localFile,
"rw");
this.fileSize = conn.getContentLength();
System.err.println("fileSize " + fileSize);
raf.setLength(fileSize);
raf.close();
}


} catch (Exception e) {
e.printStackTrace();
}
}
}




class DownloadFileThread extends Thread {

private String url;
private File localFile;
private long start;
private long end;

public DownloadFileThread(String url, File localFile, long start, long end){
this.url = url;
this.localFile = localFile;
this.start = start;
this.end = end;
}

@Override
public void run() {
try {
URL u = new URL(url);
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
conn.setConnectTimeout(15 * 1000);
conn.setRequestMethod("GET");
conn.setRequestProperty(
"Accept",
"image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
conn.setRequestProperty("Accept-Language", "zh-CN");
conn.setRequestProperty("Referer", url);
conn.setRequestProperty("Charset", "UTF-8");
conn.setRequestProperty(
"User-Agent",
"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");
conn.setRequestProperty("Connection", "Keep-Alive");


conn.setRequestProperty("Range", "bytes=" + start + "-" + end);// 设置获取实体数据的范围  



conn.connect();


System.err.println(conn.getResponseCode());
/**  
             * 代表服务器已经成功处理了部分GET请求  
              */  
if (conn.getResponseCode() == 206) {
InputStream is = conn.getInputStream();
int len = 0;
byte[] buf = new byte[1024];


RandomAccessFile raf = new RandomAccessFile(localFile, "rwd");
raf.seek(start);
while ((len = is.read(buf)) != -1) {
raf.write(buf, 0, len);
}
raf.close();
is.close();
System.out.println(Thread.currentThread().getName() + "完成下载  : " + start + " -- " + end);


}


} catch (Exception e) {
e.printStackTrace();
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值