Java多线程下载文件
package com.test.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://pic.4j4j.cn/upload/pic/20130909/681ebf9d64.jpg"; new MulThreadDownload().download(path,3); } public void download (String path,int threadsize) throws Exception{ URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5000); conn.setRequestMethod("GET"); if(conn.getResponseCode() == 200){ //获取网络文件长度 int length = conn.getContentLength(); //新建本地文件保存下载数据 File file = new File(getFilename(path)); //计算每条线程负责下载的数据量 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("下载失败!"); } } 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; } public void run() { int start = threadid * block; //本线程下载数据写入文件开始位置 int end = (threadid+1) * block - 1; //本线程下载数据写入文件结束位置 try { //创建一个随机访问文件流对象 RandomAccessFile accessFile = new RandomAccessFile(file, "rwd"); //文件指针偏移至正确写入位置 accessFile.seek(start); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5000); conn.setRequestMethod("GET"); //设置请求数据的范围 conn.setRequestProperty("Range", "bytes="+start+"-"+end); if(conn.getResponseCode() == 206){//状态码206:(部分内容) 服务器成功处理了部分 GET 请求 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(); } } } private String getFilename(String path) { return path.substring(path.lastIndexOf("/")+1); }}Java多线程下载文件的更多相关文章
- java 多线程下载文件 以及URLConnection和HttpURLConnection的区别
使用 HttpURLConnection 实现多线程下载文件 注意GET大写//http public class MultiThreadDownload { public static void m ...
- java 多线程下载文件并实时计算下载百分比(断点续传)
多线程下载文件 多线程同时下载文件即:在同一时间内通过多个线程对同一个请求地址发起多个请求,将需要下载的数据分割成多个部分,同时下载,每个线程只负责下载其中的一部分,最后将每一个线程下载的部分组装起来 ...
- java多线程下载文件和断点下载
多线程,断点下载文件 import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; impor ...
- 最新---java多线程下载文件
import java.io.InputStream; import java.io.RandomAccessFile; import java.net.HttpURLConnection; impo ...
- JAVA多线程下载网络文件
JAVA多线程下载网络文件,开启多个线程,同时下载网络文件. 源码如下:(点击下载 MultiThreadDownload.java) import java.io.InputStream; im ...
- java 网络编程基础 InetAddress类;URLDecoder和URLEncoder;URL和URLConnection;多线程下载文件示例
什么是IPV4,什么是IPV6: IPv4使用32个二进制位在网络上创建单个唯一地址.IPv4地址由四个数字表示,用点分隔.每个数字都是十进制(以10为基底)表示的八位二进制(以2为基底)数字,例如: ...
- java多线程下载和断点续传
java多线程下载和断点续传,示例代码只实现了多线程,断点只做了介绍.但是实际测试结果不是很理想,不知道是哪里出了问题.所以贴上来请高手修正. [Java]代码 import java.io.File ...
- 多线程下载文件,ftp文件服务器
1: 多线程下载文件 package com.li.multiplyThread; import org.apache.commons.lang3.exception.ExceptionUtils; ...
- Java多线程下载分析
为什么要多线程下载 俗话说要以终为始,那么我们首先要明确多线程下载的目标是什么,不外乎是为了更快的下载文件.那么问题来了,多线程下载文件相比于单线程是不是更快? 对于这个问题可以看下图. 横坐标是线程 ...
随机推荐
- C++ Contest Code preprocessor
大概可以拿来方便拉模板 变量名.语法都是瞎整的你感觉有用随便用好了.. #include<bits/stdc++.h> using namespace std; map<string ...
- Using AntiForgeryToken make it better
原文发布时间为:2011-09-01 -- 来源于本人的百度文章 [由搬家工具导入] http://weblogs.asp.net/srkirkland/archive/2010/04/14/guar ...
- js7:表单的学习,Forms对象
原文发布时间为:2008-11-09 -- 来源于本人的百度文章 [由搬家工具导入] dreamveawer中,选择插入——表单——然后后面的几个选项进行学习: 大体上这么些类型: <html& ...
- duilib入门简明教程 -- 前言(1) (转)
原文转自 :http://www.cnblogs.com/Alberl/p/3341956.html 关于duilib的介绍就不多讲了,一来不熟,二来小伙伴们想必已经对比了多个界面库,也无需赘 ...
- VIM使用技巧3
假如有如下代码: var foo = "method("+argument1+","+argument2+")" 任务:在每个“+”前后各 ...
- 复制View对象
Mark一下 - (UIView*)duplicate:(UIView*)view { NSData * tempArchive = [NSKeyedArchiver archivedDataWit ...
- Chrome扩展修改页面代码执行环境的方法
Chrome的扩展程序可以通过content scripts向页面中注入js代码,所注入的js代码能够对页面中所有的DOM对象进行操作.由于Chrome在js执行环境上对页面代码和content sc ...
- 关于main函数的参数
#include <stdio.h> int main(int argc, char const *argv[]) { int i; for ( i = 0; i < argc; i ...
- String、Stringbuffer和Stringbuilder之间的区别
关于这三个类在字符串处理中的位置不言而喻,那么他们到底有什么优缺点,到底什么时候该用谁呢?下面我们从以下几点说明一下 1.在执行速度方面:Stringbuilder>Stringbuffer&g ...
- cordova 中de.sitewaerts.cordova.documentviewer 插件 看pdf图片缩略图与实际图片不一致
//if (document == nil) // Unarchive failed so create a new ReaderDocument object //{ document = [[Re ...