JAVA实现多线程下载

来源:互联网 发布:手机降低亮度软件 编辑:程序博客网 时间:2024/05/01 19:23

JAVA多线程下载,多线程下载是为了更多的抢占服务器的资源,以达到最快的下载速度,但是,不建议开启过多线程,因为每台PC的CPU执行效率都不一样,如果开启线程较多,反而会下载的非常慢。

import java.io.File;import java.io.InputStream;import java.io.RandomAccessFile;import java.net.HttpURLConnection;import java.net.URL;import java.util.Date;public class MulThreadDownload {private long startTime;public static void main(String[] args) throws Exception {System.out.println("开始下载");String path="http://192.168.1.120:8080/android-studio-bundle-130.677228-windows.exe";new MulThreadDownload().download(path,2);}/** * 下载文件 * @param path 网络文件路径 * @param threadsize 线程数,不建议开启过多线程。 * @throws Exception  */private void download(String path,int threadsize) throws Exception {startTime=new Date().getTime();//获取文件开始现在时间System.out.println("开始时间:"+new Date());URL url=new URL(path);HttpURLConnection conn=(HttpURLConnection)url.openConnection();conn.setConnectTimeout(5000);conn.setRequestMethod("GET");if(conn.getResponseCode()==200){int length=conn.getContentLength();//获取网络文件长度File file=new File(getFileName(path));//如果不写路径则默认存放在项目的根目录下RandomAccessFile accessFile=new RandomAccessFile(file, "rwd");//在本地生成一个长度相等的文件accessFile.setLength(length);//把文件的长度设置的和网络上文件长度相等accessFile.close();//计算每条线程负责下载的数据量int block=length%threadsize==0 ? length/threadsize :length/threadsize+1;for (int threadid = 0; threadid < threadsize; threadid++) {System.out.println("第"+(threadid+1)+"线程下载开始");new DownloadThread(threadid,block,url,file).start();}}else{System.out.println("下载失败");}}/** * 线程类,负责下载操作 */private class DownloadThread extends Thread{private int threadid;//线程IDprivate int block;//每条线程负责下载的数据量private URL url;//线程下载路径private File file;//线程下载文件/** * 下载线程 * @param threadid 线程ID * @param block 每条线程负责下载的数据量 * @param url 线程下载的路径 * @param file 线程下载的文件 */public DownloadThread(int threadid, int block, URL url, File file) {this.threadid=threadid;this.block=block;this.url=url;this.file=file;}@Overridepublic void run() {int start=threadid*block;//计算该线程从网络文件的什么位置开始下载int end=(threadid+1)*block-1;//下载到网络文件的什么位置结束try{RandomAccessFile accessFile=new RandomAccessFile(file, "rwd");//在本地生成一个长度相等的文件accessFile.seek(start);//移动指针到文件的某个位置HttpURLConnection conn=(HttpURLConnection)url.openConnection();conn.setConnectTimeout(5000);conn.setRequestMethod("GET");conn.setRequestProperty("Range","bytes="+start+"-"+end);//指定从网络上哪个区域开始下载数据//注意,分段下载的网络请求返回编码是206而不是200if(conn.getResponseCode()==206){InputStream inStream=conn.getInputStream();byte[] buffer=new byte[1024];int len=0;while((len=inStream.read(buffer))!=-1){accessFile.write(buffer, 0, len);}accessFile.close();inStream.close();}System.out.println("第"+(threadid+1)+"线程已经下载完成");if((threadid+1)==3){//如果线程下载ID等于最大线程数,说明下载完成,并且获取当前时间统计下载的总时间System.out.println("结束时间:"+new Date());long conutTime=new Date().getTime()-startTime;System.out.println("下载总耗用时间(毫秒):"+conutTime);}}catch(Exception e){e.printStackTrace();}}}/** * 取得文件名称 * @param path * @return */private String getFileName(String path) {return path.substring(path.lastIndexOf("/")+1);}}


原创粉丝点击