Java SE 多线程下载网络上的文件代码+注释
来源:互联网 发布:课程表安排软件 编辑:程序博客网 时间:2024/05/16 17:12
import java.io.InputStream;import java.io.RandomAccessFile;import java.net.HttpURLConnection;import java.net.URL;import org.junit.Test;public class DownLoader { @Test public void download() throws Throwable{ String filename = "baiduTool.exe"; String path ="http://dlsw.baidu.com/sw-search-sp/soft/e2/28619/BaiduExpert_Setup_2.0.201.1910.1422515416.exe"; URL url = new URL(path); HttpURLConnection conn =(HttpURLConnection)url.openConnection(); conn.setConnectTimeout(5*1000); conn.setRequestMethod("GET"); int filelength = conn.getContentLength();//文件的长度 System.out.println(filelength); RandomAccessFile file = new RandomAccessFile(filename, "rw");//得到随机文件类,第一个参数为文件名称,第二个参数是对文件的操作,rw是读写操作 file.setLength(filelength);//设置本地文件的长度,等于本地文件的长度 file.close();//关闭这个文件 conn.disconnect();//先断掉这个链接,这个方法不调用也可以的 int threadsize = 3;//设置要下载的线程数 int threadlength = filelength %3 == 0?filelength/3:filelength/3+1;//每条线程下载的长度 for(int i= 0;i<threadsize;i++){ int startposition = i* threadlength;//计算每条线程应该从文件的什么位置开始下载 RandomAccessFile threadfile = new RandomAccessFile(filename, "rw"); threadfile.seek(startposition);//从文件的什么位置开始写入数据 new DownLoadThread(i,path,startposition,threadfile,threadlength).start(); } int quit =System.in.read(); while('q' != quit){ Thread.sleep(2*1000); } } private class DownLoadThread extends Thread{ private int threadid; private int startpositon; private RandomAccessFile threadFile; private int threadlength; private String path; public DownLoadThread(int threadid,String path,int startposition,RandomAccessFile threadFile,int threadlength){ this.threadid = threadid; this.path= path; this.startpositon= startposition; this.threadFile = threadFile; this.threadlength =threadlength; } @Override public void run(){ try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5*1000); conn.setRequestMethod("GET"); conn.setRequestProperty("Range","bytes="+startpositon+"-");//从指定文件从什么位置开始下载 InputStream inStream = conn.getInputStream(); byte[] buffer = new byte[1024]; int len =-1; int length =0;//指的是当前线程下载的值,之后将下载的长度和每个线程下载的长度进行比较,如果小于就继续下载,如果大于就停止下循环 while(length<threadlength &&(len = inStream.read(buffer))!=-1){ threadFile.write(buffer,0,len); length +=len;//累计下载长度 //当复制到第二条线程的时候,已经制定了开始写位置,写到自己的长度就应该断掉 } threadFile.close(); inStream.close(); System.out.println("线程"+(threadid+1)+"已经下载完成"); } catch (Exception e) { System.out.println("线程"+(threadid+1)+"下载出错了"+e); } } }}
0 0
- Java SE 多线程下载网络上的文件代码+注释
- Java SE单线程下载网络上的音/视频等大文件
- java SE 文件多线程下载,断点续传 原理
- java se 多线程下载
- java通用文件下载代码带注释
- java下载网络文件代码
- 用VBA代码下载网络上的文件
- 用JAVA代码实现下载网络上的资源
- java实现多线程下载网络文件
- java网络---实现多线程下载文件
- 多线程下载基础示例JAVA SE
- android_33_多线程下载(Java SE版本)
- 多线程下载,拷贝文件java代码
- 多线程下载网络文件
- 【JAVA SE】3.注释
- vba word 下载网络上的文件
- 网络上下载的文件无法删除
- Java多线程下载代码
- 全局变量、局部变量、静态全局变量、静态局部变量的区别
- 单选按钮事件
- JVM内存的设置
- 延年益寿的十个习惯之我见
- mysql 将select查询的值赋给update set语句中
- Java SE 多线程下载网络上的文件代码+注释
- android NFC学习笔记(二)
- 哈希表
- 国内HTML5前端开发框架
- 为Fedora21 aarch64制作RPM包(tengine)
- Cocos2dx技术(三)——cocos2dx基本概念(四)动作
- 【BZOJ3932】【CQOI2015】任务查询系统 可持久化线段树
- BNU 背包密码(编码与解密)
- (二进制与三进制)趣味题