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();}}}}


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 淘宝买的衣服味道很大怎么办 三国杀账号忘了怎么办 宽带连接被删了 怎么办 手机被偷了qq怎么办 手机丢了微信怎么办啊 手机店把手机修坏了怎么办 在手机店买到山寨机手机怎么办 有人在qq群上骂我怎么办 qq群一直有人骚扰怎么办 苹果手机QQ图标不在桌面上怎么办 苹果手机长按不能删除怎么办 qq发的图片过期怎么办 九黎八卦在仓库怎么办 飞猪f2领了万豪银卡没住怎么办 机械键盘摁键冲突怎么办 玩游戏键盘没反应怎么办 玩枪战游戏头晕恶心怎么办 手机看视频不能横屏怎么办 苹果手机安全码忘记了怎么办 信用卡安全码忘记了怎么办 联想电脑管家阻止我安装软件怎么办 word恢复后打开乱码怎么办 苹果手机局域网也登陆不了怎么办 剑三账号冻结7天怎么办 荒野行动无缘无故被限制时间怎么办 电脑打游戏闪屏怎么办 微信下载出现数据包出错怎么办 安卓平板闪退怎么办 剑侠世界2出了1怎么办 苹果手机有木马病毒删除不了怎么办 苹果手机病毒了怎么办【解决方法】 苹果手机点击病毒链接怎么办 苹果手机有病毒啦怎么办 苹果7p反复重启怎么办 苹果手机屏幕触屏不灵怎么办 苹果手机屏翘起来了怎么办 苹果x手机触屏失灵怎么办 苹果7手机变成黑白屏怎么办 苹果手机屏不动了怎么办 苹果6老是卡屏怎么办 苹果x卡屏死机怎么办