Java多线程下载文件
发布日期:2022-03-18 05:04:14 浏览次数:6 分类:技术文章

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

文献

https://blog.csdn.net/wu6660563/article/details/76607316

1,N个线程下载单个文件

public class DownloadWithRange implements Runnable {
private String urlLocation; private String filePath; private long start; private long end; DownloadWithRange(String urlLocation, String filePath, long start, long end) {
this.urlLocation = urlLocation; this.filePath = filePath; this.start = start; this.end = end; } @Override public void run() {
try {
HttpURLConnection conn = getHttp(); conn.setRequestProperty("Range", "bytes=" + start + "-" + end); File file = new File(filePath); RandomAccessFile out = null; if (file != null) {
out = new RandomAccessFile(file, "rw"); } out.seek(start); InputStream in = conn.getInputStream(); byte[] b = new byte[1024]; int len = 0; while ((len = in.read(b)) >= 0) {
out.write(b, 0, len); } in.close(); out.close(); } catch (Exception e) {
e.getMessage(); } } public HttpURLConnection getHttp() throws IOException {
URL url = null; if (urlLocation != null) {
url = new URL(urlLocation); } HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(5000); conn.setRequestMethod("GET"); return conn; }}

2,定义线程Pool

public class DownloadFileWithThreadPool {
public void getFileWithThreadPool(String urlLocation, String filePath, int poolLength) throws IOException {
ExecutorService threadPool = Executors.newCachedThreadPool(); long len = getContentLength(urlLocation); System.out.println(len); for (int i = 0; i < poolLength; i++) {
long start = i * len / poolLength; long end = (i + 1) * len / poolLength - 1; if (i == poolLength - 1) {
end = len; } System.out.println(start+"---------------"+end); DownloadWithRange download = new DownloadWithRange(urlLocation, filePath, start, end); threadPool.execute(download); } threadPool.shutdown(); } public static long getContentLength(String urlLocation) throws IOException {
URL url = null; if (urlLocation != null) {
url = new URL(urlLocation); } HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(5000); conn.setRequestMethod("GET"); long len = conn.getContentLength(); return len; }}

3,测试下载单个文件

public static void main(String[] args) {
Date startDate = new Date(); DownloadFileWithThreadPool pool = new DownloadFileWithThreadPool(); try {
pool.getFileWithThreadPool("http://mpge.5nd.com/2016/2016-11-15/74847/1.mp3", "D:\\1.mp3", 100); } catch (IOException e) {
e.printStackTrace(); } System.out.println(new Date().getTime() - startDate.getTime()); }

4,多文件下载,单文件单线程

/** * @author Nick 带线程池的文件下载类,线程大小10 *      文件数少的情况下,体现不大 * @version V1.0.0 * @Date 2017/8/2 20:43 */public class FileDownConnManager {
private static final Logger logger = LoggerFactory.getLogger(FileDownConnManager.class); private static final FileDownConnManager connManager = new FileDownConnManager(); private static ExecutorService executorService = Executors.newFixedThreadPool(10); //10个线程跑 public static FileDownConnManager getDefaultManager() {
return connManager; } public static byte[] fileDown(final String netURL) throws ExecutionException, InterruptedException {
Future
future = executorService.submit(new Callable
() {
@Override public byte[] call() throws Exception {
Date date = new Date(); URL url; byte[] getData = new byte[0]; InputStream is = null; try {
url = new URL(netURL); URLConnection connection = url.openConnection(); is = connection.getInputStream(); getData = readInputStream(is); } catch (IOException e) {
logger.error("从URL获得字节流数组失败 " + ExceptionUtils.getMessage(e)); } finally {
try {
is.close(); } catch (IOException e) {
logger.error("从URL获得字节流数组流释放失败 " + ExceptionUtils.getMessage(e)); } } return getData; } }); return future.get(); }}

5,测试

@Test    public void test2() throws ExecutionException, InterruptedException, IOException {
long time1 = System.currentTimeMillis(); for(int i = 0; i < 15; i++) {
byte[] by1 = FileDownConnManager.fileDown("http://mpge.5nd.com/2016/2016-11-15/74847/1.mp3"); FileUtils.writeByteArrayToFile(new File("D:\\test2_"+i+".mp3"), by1); } System.out.println(System.currentTimeMillis() - time1); } @Test public void test3() throws IOException {
long time1 = System.currentTimeMillis(); for(int i = 0; i < 15; i++) {
byte[] by1 = FileFromUrlUtil.getInputStreamFromUrl("http://mpge.5nd.com/2016/2016-11-15/74847/1.mp3"); FileUtils.writeByteArrayToFile(new File("D:\\test3_"+i+".mp3"), by1); } System.out.println(System.currentTimeMillis() - time1); }

转载地址:https://blog.csdn.net/qq_37430469/article/details/122829409 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:多线程异步返回值
下一篇:springcloud整合redis集群

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2024年03月12日 03时13分40秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

java动态代码_Java Agent入门学习之动态修改代码 2019-04-21
python集合如何去除重复数据_Python 迭代删除重复项,集合删除重复项 2019-04-21
iview 自定义时间选择器组件_Vue.js中使用iView日期选择器并设置开始时间结束时间校验功能... 2019-04-21
java 验证码校验_JavaWeb验证码校验功能代码实例 2019-04-21
java多线程初学者指南_Java多线程初学者指南(4):线程的生命周期 2019-04-21
java进程user是jenkins_java 学习:在java中启动其他应用,由jenkins想到的 2019-04-21
java添加资源文件_如何在eclipse中将资源文件夹添加到我的Java项目中 2019-04-21
java的三种修饰符_3分钟弄明白JAVA三大修饰符 2019-04-21
mysql source skip_redis mysql 中的跳表(skip list) 查找树(btree) 2019-04-21
java正则过滤Linux命令_linux - grep常用正则表达式,过滤文本内容 2019-04-21
java流过程_Java IO流和文件操作实现过程解析 2019-04-21
processing编译java_java – 使用Processing在Android SDK中编译build.... 2019-04-21
java sun.org.mozilla_maven编译找不到符号 sun.org.mozilla.javascript.internal 2019-04-21
php curl 输出到文件,PHP 利用CURL(HTTP)实现服务器上传文件至另一服务器 2019-04-21
PHP字符串运算结果,PHP运算符(二)"字符串运算符"实例详解 2019-04-21
PHP实现 bcrypt,如何使php中的bcrypt和Java中的jbcrypt兼容 2019-04-21
php8安全,PHP八大安全函数解析 2019-04-21
php基础语法了解和熟悉的表现,PHP第二课 了解PHP的基本语法以及目录结构 2019-04-21
matlab中lag函数用法,MATLAB movavg函数用法 2019-04-21
matlab变形监测,基于matlab的变形监测数据处理与分析_毕业设计论文 2019-04-21