Java多线程下载文件
发布时间:2020-05-24 19:24:30 所属栏目:Java 来源:互联网
导读:Java多线程下载文件
|
下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。 脚本之家小编现在分享给大家,也给大家做个参考。 import java.io.File;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
public class MultiThreadDownFile {
public static void main(String[] args) {
try {
String filePath = "http://dl_dir.qq.com/qqfile/qd/QQ2012Beta3_QQProtect2.8.exe";
int threadNum = 5;
new MultiThreadDown().down(filePath,threadNum);
} catch (Exception e) {
e.printStackTrace();
}
}
}
class MultiThreadDown extends Thread {
private int threadId;
private int block;
private URL url;
private File file;
public MultiThreadDown() {
}
public MultiThreadDown(int threadId,int block,URL url,File file) {
super();
this.threadId = threadId;
this.block = block;
this.url = url;
this.file = file;
}
public void down(String path,int threadNum) throws Exception {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
try {
if (conn.getResponseCode() == 200) {
int fileLength = conn.getContentLength();// Get the length of
// NetFile
System.out.println("网络文件大小:" + fileLength);
File file = new File(getFilename(path));
RandomAccessFile raf = new RandomAccessFile(file,"rwd");
raf.setLength(fileLength);
raf.close();
// 计算每条线程的下载量
int block = fileLength % threadNum == 0 ? fileLength
/ threadNum : fileLength / threadNum + 1;
for (int threadId = 0; threadId < threadNum; threadId++) {
new MultiThreadDown(threadId,block,url,file).start();
System.out.println("线程" + threadId + "开始");
}
} else {
System.out.println("Connection failed.");
}
} catch (Exception e) {
System.out.println("连接失败.");
e.printStackTrace();
}
}
private String getFilename(String path) {
return path.substring(path.lastIndexOf("/") + 1);
}
@Override
public void run() {
super.run();
int start = threadId * block;// 计算线程下载的网络文件的文件位置
int end = (threadId + 1) * block - 1;// 线程的结束位置
try {
RandomAccessFile raf = new RandomAccessFile(file,"rwd");
raf.seek(start);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
conn.setRequestProperty("Range","bytes=" + start + "-" + end);
if (conn.getResponseCode() == 206) {
InputStream is = conn.getInputStream();
byte[] buf = new byte[1024];
int len = 0;
while ((len = is.read(buf)) != -1) {
raf.write(buf,len);
}
raf.close();
is.close();
}
System.out.println("第" + threadId + "线程下载完毕");
} catch (Exception e) {
e.printStackTrace();
}
}
}
以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。 如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- java – Hibernate的批量获取算法如何工作?
- Java实现按行读取大文件
- java – JarScan,扫描所有子文件夹中的特定类的所有JAR文件
- Apache Web服务器,Java Web服务 – Hello World教程
- java – Hibernate Cascading Delete不按预期工作
- criteria – Jpa QueryBuilder where子句中的多个表达式不起
- 基于java servlet过滤器和监听器(详解)
- 与父级扩展的JavaFX布局
- java – file.canWrite(); file.canRead(); file.canExceut
- java – 比较器是新的还是可注射的对象?
