实现多线程下载的代码
来源:互联网 发布:程序员加薪申请 编辑:程序博客网 时间:2024/06/05 13:33
本代码实现多线程下载功能,通过三个线程下载服务器上的文件。
package com.sdu.wmct.download;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://58.194.170.166:8080/web/QQWubiSetup.exe";String path = "http://58.194.170.166:8080/web/gg.gif";int threadsize = 3;new MulThreadDownload().download(path, threadsize);}private void download(String path, int threadsize) throws Exception {URL downpath = new URL(path);HttpURLConnection conn = (HttpURLConnection) downpath.openConnection();conn.setConnectTimeout(5000);conn.setRequestMethod("GET");if(conn.getResponseCode() == 200){int length = conn.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, downpath, block, file).start();}}}//负责下载操作private final class DownloadThread extends Thread{private int threadid;private URL downpath;private int block;private File file;public DownloadThread(int threadid, URL downpath, int block, File file) {this.threadid = threadid;this.downpath = downpath;this.block = block;this.file = file;}public void run() {int startposition = threadid * block;//从网络文件的什么位置开始下载数据int endposition = (threadid+1) * block - 1;//下载到网络文件的什么位置结束//指示该线程要从网络文件的startposition位置开始下载,下载到endposition位置结束//Range:bytes=startposition-endpositiontry{RandomAccessFile accessFile = new RandomAccessFile(file, "rwd");accessFile.seek(startposition);//移动指针到文件的某个位置HttpURLConnection conn = (HttpURLConnection) downpath.openConnection();conn.setConnectTimeout(5000);conn.setRequestMethod("GET");conn.setRequestProperty("Range", "bytes="+ startposition+ "-"+ endposition);InputStream inStream = conn.getInputStream();byte[] buffer = new byte[1024];int len = 0;while( (len = inStream.read(buffer)) != -1 ){accessFile.write(buffer, 0, len);}accessFile.close();inStream.close();System.out.println("第"+ (threadid+1)+ "线程下载完成");}catch (Exception e) {e.printStackTrace();}}}/** * 获取文件名称 * @param path 下载路径 * @return */private static String getFileName(String path) {return path.substring(path.lastIndexOf("/")+ 1);}}
- 实现多线程下载的代码
- 多线程断点下载的代码实现
- PHP多线程批量采集下载美女图片的实现代码
- 多线程下载的代码例子
- Java代码实现多线程下载和断点续传
- Java代码实现多线程下载和断点续传
- JAVA多线程下载的实现
- 多线程断点下载的实现
- android多线程下载的实现
- JAVA多线程下载的实现
- JavaSE多线程下载的实现
- 多线程-多线程方式1的代码实现
- Android 中实现多线程下载和断点续传的原理和代码
- 多线程下代码及多线程下载的深入研究
- Java多线程下载代码
- 多线程下载 关键代码
- Python3 多线程下载代码
- 多线程下载示例代码
- Delphi XE2下的MapX异常
- VC Windows shell扩展编程(为系统右键菜单添加二级菜单傻瓜教程)
- 关于Bos 开发中使用字段类型是选择已有基础资料的的源代码
- Remedy Java API 建单
- poj 2629 Common permutation
- 实现多线程下载的代码
- 紧急状况不犯愁,上班族司机必备的六款实用App
- android ApiDemo学习(一)service——6 Remote Service Controller
- java进行xls转xml
- Git学习笔记(八) Git基本操作
- "Format" did not complete normally。MyEclipse手动格式化web.xml文件错误
- java面试笔试题总结
- gloox 连接 openfire 使用笔记 --openfire 配置
- Remedy表单样式修改之我见