博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
我的Java开发学习之旅------>Java多线程下载文件 实例
阅读量:7073 次
发布时间:2019-06-28

本文共 3007 字,大约阅读时间需要 10 分钟。

import java.io.File;import java.io.InputStream;import java.io.RandomAccessFile;import java.net.HttpURLConnection;import java.net.URL;public class MulThreadDownload {	public static void main(String[] args) throws Exception {		String path = "http://192.168.1.100:8080/Hello/Big.exe";		new MulThreadDownload().download(path, 3);	}	/**	 * 下载文件	 * 	 * @param path	 *            网络文件路径	 * @param threadSize	 *            线程数	 * @throws Exception	 */	private void download(String path, int threadSize) throws Exception {		URL url = new URL(path);		HttpURLConnection connection = (HttpURLConnection) url.openConnection();		connection.setRequestMethod("GET");		connection.setConnectTimeout(5000);		if (connection.getResponseCode() == 200) {			int length = connection.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++) {				new DownloadThread(threadId, block, url, file).start();			}		} else {			System.out.println("download fail");		}	}	private class DownloadThread extends Thread {		private int threadId;		private int block;		private URL url;		private File file;		public DownloadThread(int threadId, int block, URL url, File file) {			this.threadId = threadId;			this.block = block;			this.url = url;			this.file = file;		}		@Override		public void run() {			int start = threadId * block; // 计算该线程从网络文件什么位置开始下载			int end = (threadId + 1) * block - 1; // 计算下载到网络文件什么位置结束			try {				RandomAccessFile accessFile = new RandomAccessFile(file, "rwd");				accessFile.seek(start);  //从start开始				HttpURLConnection connection = (HttpURLConnection) url						.openConnection();				connection.setRequestMethod("GET");				connection.setConnectTimeout(5000);				//设置获取资源数据的范围,从start到end				connection.setRequestProperty("Range", "bytes=" + start + "-"						+ end);				//注意多线程下载状态码是 206  不是200				if (connection.getResponseCode() == 206) {					InputStream inputStream = connection.getInputStream();					byte[] buffer = new byte[1024];					int len = 0;					while ((len = inputStream.read(buffer)) != -1) {						accessFile.write(buffer, 0, len);					}					accessFile.close();					inputStream.close();				}				System.out.println("第" + (threadId + 1) + "条线程已经下载完成");			} catch (Exception e) {				e.printStackTrace();			}		}	}	/**	 * 获取文件名称	 * 	 * @param path	 *            网络文件路径	 * @return	 */	private String getFileName(String path) {		return path.substring(path.lastIndexOf("/") + 1);	}}

==================================================================================================

  作者:欧阳鹏  欢迎转载,与人分享是进步的源泉!

  转载请保留原文地址

==================================================================================================

转载于:https://www.cnblogs.com/ouyangpeng/archive/2013/04/04/8538415.html

你可能感兴趣的文章
算法实验1 两个数组的中位数
查看>>
仓储管理的目标
查看>>
gcc g++ 参数介绍
查看>>
本博客供喜欢JAVA的同学一起交流学习
查看>>
trie树
查看>>
xshell常用命令大全
查看>>
秒杀?能不能先预估下服务器能不能顶的住再玩啊!!!
查看>>
Oracle回顾
查看>>
R中数据结构
查看>>
mysql数据库学习(二)--表操作
查看>>
学习Qt的一些心得笔记
查看>>
cookie与session组件
查看>>
Windows Server 2008 R2下将JBoss安装成windows系统服务
查看>>
关于dubbo服务的xml配置文件报错的问题
查看>>
Escape
查看>>
运营商 WLAN
查看>>
并发编程 —— ScheduledThreadPoolExecutor
查看>>
zabbix 监控域名证书到期时间!!!!
查看>>
Java Magic. Part 1: java.net.URL
查看>>
异步实现服务器推送消息(聊天功能示例)
查看>>