Java多线程下载文件实现案例详解
更新时间:2020年05月18日 14:52:13 投稿:yaominghui
这篇文章主要介绍了Java多线程下载文件实现案例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
原理解析:
利用RandomAccessFile在本地创建一个随机访问文件,文件大小和服务器要下载的文件大小相同。 根据线程的数量(假设有三个线程),服务器的文件三等分,并把我们在本地创建的文件同样三等分,每个线程下载自己负责的部分,到相应的位置即可。
示例图:
代码如下
import java.io.InputStream; import java.io.RandomAccessFile; import java.net.HttpURLConnection; import java.net.URL; public class MutilDownload { private static String path = "http://192.168.80.85:8080/test.doc"; private static final int threadCount = 3; public static void main(String[] args) { try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); int responseCode = conn.getResponseCode(); if (responseCode == 200) { int contentLength = conn.getContentLength(); System.out.println("length" + contentLength); RandomAccessFile rafAccessFile = new RandomAccessFile("test.doc", "rw"); rafAccessFile.setLength(contentLength); int blockSize = contentLength / threadCount; for (int i = 0; i < threadCount; i++) { int startIndex = i * blockSize; //每个现成下载的开始位置 int endIndex = (i + 1) * blockSize - 1;// 每个线程的结束位置 if (i == threadCount - 1) { //最后一个线程 endIndex = contentLength - 1; } new DownloadThread(startIndex, endIndex, i).start(); } } } catch (Exception e) { } } private static class DownloadThread extends Thread { private int startIndex; private int endIndex; private int threadId; public DownloadThread(int startIndex, int endIndex, int threadId) { this.startIndex = startIndex; this.endIndex = endIndex; this.threadId = threadId; } @Override public void run() { try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); conn.setRequestProperty("Range", "bytes=" + startIndex + "-" + endIndex); //固定写法,请求部分资源 int responseCode = conn.getResponseCode(); // 206表示请求部分资源 if (responseCode == 206) { RandomAccessFile rafAccessFile = new RandomAccessFile("test.doc", "rw"); rafAccessFile.seek(startIndex); InputStream is = conn.getInputStream(); int len = -1; byte[] buffer = new byte[1024]; while ((len = is.read(buffer)) != -1) { rafAccessFile.write(buffer, 0, len); } rafAccessFile.close(); System.out.println("线程" + threadId + "下载完成"); } } catch (Exception e) { } } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
Java中Collection、List、Set、Map之间的关系总结
今天小编就为大家分享一篇关于Java中Collection、List、Set、Map之间的关系总结,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧2019-02-02Java组件commons fileupload实现文件上传功能
这篇文章主要为大家详细介绍了Java组件commons fileupload实现文件上传功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2016-10-10关于Java中配置ElasticSearch集群环境账号密码的问题
这篇文章主要介绍了Java中配置ElasticSearch集群环境账号密码的问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2022-04-04java使用xfire搭建webservice服务的过程详解
这篇文章主要介绍了java使用xfire搭建webservice服务的过程。使用xfire搭建webService的服务,可以在浏览器访问。对此感兴趣的可以了解一下2020-07-07在IntelliJ IDEA中配置SSH服务器开发环境并实现固定地址远程连接的操作方法
本文主要介绍如何在IDEA中设置远程连接服务器开发环境,并结合Cpolar内网穿透工具实现无公网远程连接,然后实现远程Linux环境进行开发,本例使用的是IDEA2023.2.5版本,感兴趣的朋友跟随小编一起看看吧2024-01-01
最新评论