Java 多线程下载
来源:互联网 发布:水果合成软件下载 编辑:程序博客网 时间:2024/06/05 07:51
开发环境:win 7 (64 bit) + eclipse
还未实现断点下载,如需实现断点下载,则需要额外增加一个配置文件:
import java.io.InputStream;import java.io.RandomAccessFile;import java.net.HttpURLConnection;import java.net.URL;public class DownTest {public static void main(String[] args) throws Exception {String path = "http://im.baidu.com/download/BaiduHi_4.2_Beta.exe";String filename = "BaiduHi_4.2_Beta.exe";//匿名内部类访问局部变量需要加关键字finalfinal DownThread dt = new DownThread(path, filename, 5);dt.down();//定义一个匿名内部类new Thread(){public void run(){while(dt.getpercentage() < 1){System.out.println("已完成: " + dt.getpercentage());try{Thread.sleep(1000);}catch (Exception io){io.printStackTrace();}}}}.start();}}class DownThread{private String path;//下载资源的URL路径private String targetfile;//下载的文件保存位置private int threadnum;//线程数private int filesize; //下载文件的总字节数private Down[] threads;public DownThread(String path, String targetfile, int threadnum){this.path = path;this.targetfile = targetfile;this.threadnum = threadnum;threads = new Down[threadnum];}public void down() throws Exception{URL url = new URL(path);HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setConnectTimeout(5000);conn.setRequestMethod("GET");filesize = conn.getContentLength();conn.disconnect();int cursize = filesize / threadnum + 1;for(int i = 0; i < threadnum; i++){int startpos = i * cursize ;RandomAccessFile file = new RandomAccessFile(targetfile, "rw");file.seek(startpos);threads[i] = new Down(startpos, cursize, file);threads[i].start();}}//计算下载进度public double getpercentage(){int sum = 0;for(int i = 0; i < threadnum; i++){sum += threads[i].length;}return sum * 1.0 / filesize;}//定义一个非静态内部类private class Down extends Thread{private int starpos;private int cursize;private RandomAccessFile curfile;public int length;public Down(int starpos, int cursize, RandomAccessFile curfile){this.starpos = starpos;this.cursize = cursize;this.curfile = curfile;}public void run(){try{URL url = new URL(path);HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setConnectTimeout(5000);conn.setRequestMethod("GET");InputStream ins = conn.getInputStream();System.out.println(Thread.currentThread().getName() + " is downloading now...");ins.skip(this.starpos);byte[] buf = new byte[1024];int hasread = 0;while(length < cursize && (hasread = ins.read(buf)) != -1){this.curfile.write(buf, 0, hasread);//累计该线程下载的总字节数length += hasread;}curfile.close();ins.close();System.out.println(Thread.currentThread().getName() + " downloading is done...");}catch (Exception io){io.printStackTrace();}}}}
- java 多线程下载
- java 多线程断点下载
- Java多线程下载
- java多线程下载
- java多线程下载
- Java多线程下载
- java多线程下载
- Java多线程下载
- java多线程下载程序
- Java多线程下载文件
- java多线程下载文件
- java实现多线程下载
- Java多线程下载
- Java多线程下载文件
- java多线程下载
- Java多线程下载文件
- Java多线程下载文件
- Java多线程下载代码
- BNU Choosing number 矩阵快速幂
- dll依赖跟踪(sxstrace.exe)
- vs2010编译配置Boost 1.54.0
- Linux脚本文件相关操作
- 再次使用cin失败解决方法
- Java 多线程下载
- Oracle
- disruptor 比 actor 模型还要好!
- UVa 572 - Oil Deposits
- 使用UltraISO制作ISO镜像文件
- DWR框架总结
- 在tomcat下部署工程
- Yahoo web performance improve
- android 开发学习1_用户界面(连载)