Java多线程下载文件详解编程语言
2023-06-13 09:20:29 时间
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, 0, len);
raf.close();
is.close();
System.out.println("第" + threadId + "线程下载完毕");
} catch (Exception e) {
e.printStackTrace();
}
10782.html
cjavamac相关文章
- Java多线程详解_java支持多线程
- java grouping,介绍 Java 8 groupingBy Collector
- java 删除目录下所有文件_Java删除文件、目录及目录下所有文件的方法实例
- Java删除文件(delete file in java)[通俗易懂]
- 用Java IO流实现下载文件
- JAVA是HttpURLConnection进行多线程文件下载
- java输出值取后两位小数,Java输出结果保留两位小数
- eclipse怎么导入java文件_Eclipse如何导入JAVA工程?如何将项目导入Eclipse中?
- java环境_Java基础篇——环境配置
- java applet介绍,Java Applet教程介绍[通俗易懂]
- java date当前时间_JAVA中获取当前系统时间
- Java数组循环_java遍历object数组
- JAVA生成uuid_java接口default方法
- Java 文件多线程下载详解编程语言
- Java命名规范详解编程语言
- Java RandomAccessFile类:动态读取文件内容
- Java实现Redis事务管理(redis事务java)
- 服务器上的文件Java获取Linux服务器文件:简单又高效的操作方法(java获取linux)
- 数据自动清理处理突增数据:Redis Java自动清理过期数据(redisjava过期)
- 时间利用Java操作Redis实现数据的过期时间管理(redisjava过期)
- Linux下Java应用打包实践(linux java打包)
- 在Linux上实现Java程序的运行(linux运行java程序)
- java多线程复制文件的实例代码
- java使用rmi传输大文件示例分享