62,621
社区成员
发帖
与我相关
我的任务
分享
package com.suzhou.jackey;
import java.io.BufferedInputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.concurrent.CountDownLatch;
public class FileDownloadTest2 {
private static final int THREAD_COUNT = 10;
private CountDownLatch latch = new CountDownLatch(THREAD_COUNT);
private long completeLength = 0;
private long fileLength;
//进行下载的线程
class DownloadThread extends Thread {
private URL url;
private RandomAccessFile file;
private long from;
private long end;
/**
* @param url 下载的URL
* @param file 下载完成之后存储的文件
* @param from 当前线程对应文件的起始位置
* @param end 当前线程对应文件的结束位置
*/
DownloadThread(URL url, RandomAccessFile file, long from, long end) {
this.url = url;
this.file = file;
this.from = from;
this.end = end;
}
public void run() {
try {
Long file_request_time = System.currentTimeMillis();
long pos = from;
byte[] buf = new byte[1024];
HttpURLConnection cn = (HttpURLConnection) url.openConnection();
//因为有些网站根据它判断是否是盗链接
cn.setRequestProperty("Referer", "http://172.28.131.208:8080");
//设置请求的起始和结束位置。
cn.setRequestProperty("Range", "bytes=" + from + "-" + end);
//如果请求错误,重试。
if(cn.getResponseCode() != 200 && cn.getResponseCode() != 206){
System.out.println("Responce Code: " + cn.getResponseCode());
run();
return;
}
BufferedInputStream bis = new BufferedInputStream(cn.getInputStream());
int len;
while ((len = bis.read(buf)) > 0) {
synchronized (file) {
file.seek(pos);
file.write(buf, 0, len);
}
pos += len;
completeLength += len;
if(completeLength * 100 / fileLength % 10 == 0){
//System.out.println(Thread.currentThread().getName() + ":" + completeLength * 100 / fileLength + "%");
}
}
cn.disconnect();
long transfer_time = System.currentTimeMillis();
long downLoadTime = transfer_time - file_request_time;
System.out
.println(Thread.currentThread().getName() + "--------StartTime--------------- :"
+ milltoString2(file_request_time));
System.out
.println(Thread.currentThread().getName() + "--------End Time--------------- :"
+ milltoString2(transfer_time));
System.out
.println(Thread.currentThread().getName() + "--------DownLoad Time----------------- :"
+ milltoString(downLoadTime));
} catch (Exception e) {
e.printStackTrace();
}
latch.countDown();
}
}
public static String milltoString(long millseconds) { // convert millionseconds
// into string
DateFormat formatter = new SimpleDateFormat("mm分ss.SSS秒");
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(millseconds); // connection sum time ;
return formatter.format(calendar.getTime());
}
public static String milltoString2(long millseconds) { // convert millionseconds
// into string
DateFormat formatter = new SimpleDateFormat("HH时mm分ss.SSS秒");
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(millseconds); // connection sum time ;
return formatter.format(calendar.getTime());
}
public void download(String address) throws Exception {
URL url = new URL(address);
URLConnection cn = url.openConnection();
cn.setRequestProperty("Referer", "http://172.28.131.208:8080");
fileLength = cn.getContentLength();
//RandomAccessFile file = new RandomAccessFile("Example2.rar", "rw");
System.out.println("file size:" + (float)fileLength/1024/1024 + "M");
//计算每个线程请求的起始位置和结束位置。
long pos = 0;
for (int i = 0; i < THREAD_COUNT; i++) {
RandomAccessFile file = new RandomAccessFile("Example"+ i + ".rar", "rw");
new DownloadThread(url, file, pos, fileLength).start();
}
latch.await();
}
public static void main(String[] args) throws Exception {
long time = System.currentTimeMillis();
new FileDownloadTest2()
.download("http://172.28.131.208:8080/Example.rar");
System.out.println("共用时:"+milltoString(System.currentTimeMillis() - time));
}
}