您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
46_多线程下载文件的原理
发布时间:2021-02-27 10:52:09编辑:雪饮阅读()
这次多线程下载文件,暂时先单独在javase上面实现,暂不建立在安卓上。
这个eclipse的jee版本支持普通项目和java ee项目一起建立,例如
Eclipse Java EE IDE for Web Developers.
Version: Indigo Service Release 2
Build id: 20120216-1857
(c) Copyright Eclipse contributors and others 2005, 2012. All rights reserved.
因为我这又是另外一个电脑了,这个上面java环境是32位的,这个eclipse也安装32位的,另外就是这个也还没有部署tomcat,那么再次搭建了tomcat也是32位的,另外这里也要重新建立javaweb项目
这次多线程下载文件的文件就放在建立的javaweb项目中
![多线程.png](/d/file/xuewuzhijing/xindebiji/a70cb214715055e8bce2bc0372245212.png)
这里我放置一个output.mp4做为待下载文件,运行到服务后虽然看不到完整地址,但是eclipse中可以通过这样看到,你取消后这个eclipse左上角就出现了这个地址
![多线程.png](/d/file/xuewuzhijing/xindebiji/d0f6f112f4e7f240710c2d7cfff1fb47.png)
那么接下来就同样的在这个eclipse中建立普通javase项目,这个机子上安装的是jre1.8
![多线程.png](/d/file/xuewuzhijing/xindebiji/da0acb4672e1b0e725e87ca3e4c4b954.png)
然后建立包
![多线程.png](/d/file/xuewuzhijing/xindebiji/345d3ab92864cf16bdb6dea2a2b3889b.png)
然后建立类
![多线程.png](/d/file/xuewuzhijing/xindebiji/3c9db075463c0defae20e91aec4f497a.png)
那么这个类的具体实现代码MutileDownload.java:
package down;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
public class MutileDownload {
public static final String path = "http://192.168.43.71:8080/javaweb/output.mp4";
public static void main(String[] args) throws Exception {
TestDown();
}
public static void TestDown() throws Exception {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
int code = conn.getResponseCode();
if (code == 200) {
int len = conn.getContentLength();
RandomAccessFile file = new RandomAccessFile(Util.getFilenName(path),"rwd");
// 1.设置本地文件大小跟服务器的文件大小一致
file.setLength(len);
// 2 .假设开启3 个线程
int threadnumber = 3;
int blocksize = len / threadnumber;
/**
* 线程1 0~ blocksize
* 线程2 1*bolocksize ~ 2*blocksize
* 线程3 2*blocksize ~
* 文件末尾
*/
for (int i = 0; i < threadnumber; i++) {
int startposition = i * blocksize;
int endpositon = (i + 1) * blocksize;
if (i == (threadnumber - 1)) {
// 最后一个线程
endpositon = len;
}
DownLoadTask task = new DownLoadTask(i, path, startposition,
endpositon);
task.start();
}
}
}
}
class DownLoadTask extends Thread {
int threadid;
String filepath;
int startposition;
int endpositon;
public DownLoadTask(int threadid, String filepath, int startposition,
int endpositon) {
this.threadid = threadid;
this.filepath = filepath;
this.startposition = startposition;
this.endpositon = endpositon;
}
public void run() {
try {
URL url=new URL(filepath);
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
//请求头中range可以设置从文件的指定某位置到指定另外一个位置下载,这样就形成了每个线程只下载属于自己的哪一部分
conn.setRequestProperty("Range", "bytes=" + startposition + "-"+ endpositon);
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
InputStream is = conn.getInputStream();
//打开主线程创建的和服务器端待下载的空白文件
RandomAccessFile file = new RandomAccessFile(Util.getFilenName(filepath),"rwd");
// 设置 数据从文件哪个位置开始写
file.seek(startposition);
//从输入流中每次读取1024个单位
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
file.write(buffer, 0, len);
}
file.close();
System.out.println("线程" + threadid + "下载完毕");
} catch (Exception e) {
e.printStackTrace();
}
super.run();
}
}
class Util{
public static String getFilenName(String path) {
int start = path.lastIndexOf("/") + 1;
return path.substring(start, path.length());
}
}
然后运行该程序
![多线程.png](/d/file/xuewuzhijing/xindebiji/937d470618f7224bdc5761b840c61a80.png)
可以看到3个线程都完毕了
![多线程.png](/d/file/xuewuzhijing/xindebiji/697a0b0c4721811a272d67f0ca03c110.png)
它下载后的存放路径可以参考我们的项目对应实际文件路径
![多线程.png](/d/file/xuewuzhijing/xindebiji/6def67cc2c2587c2530ce73fdf5c3130.png)
C:\Users\Administrator\workspace\MutileDownLoadSE
![多线程.png](/d/file/xuewuzhijing/xindebiji/c8800d0860444a84c2c9b3b628ef3938.png)
然后与原来javaweb端这个视频文件稍微比较下文件大小,视频时长,可以确定文件下载的是没有问题的
![多线程.png](/d/file/xuewuzhijing/xindebiji/6486a8cfd104cbf59becb503a40d355b.png)
![多线程.png](/d/file/xuewuzhijing/xindebiji/cd79f0dd24129543a20c5c91f4080d28.png)
关键字词:多线程,下载
下一篇:47_多线程断点续传移植