Java实现多线程下载
package cn.test.DownLoad; import java.io.File;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL; public class MultiPart {
public void down() throws Exception
{
//1、声明URL
String fileName="a.rar";
String path="http://localhost:8080/day23_MultiThreadDownLoad/file/"+fileName;
URL url=new URL(path);
//2、返回连接对象
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
//3、设置请求类型
conn.setRequestMethod("GET");
//4、设置允许接收消息
conn.setDoInput(true);
//5、连接
conn.connect();
//6、状态码
int code=conn.getResponseCode();
if(code==200)
{
int sum=conn.getContentLength();//总长度
String downFile="d:\\"+fileName;
//7、创建一个相同大小的空文件
RandomAccessFile file=new RandomAccessFile(new File(downFile), "rw");
file.setLength(sum);
file.close();
//8、声明线程数量
int threadCount=3;
//9、声明每个线程的下载量
int threadSize=sum/threadCount+((sum%threadCount==0)?0:1);
for(int i=0;i<threadCount;i++)
{
int start=i*threadSize;
int end=start+threadSize-1;
System.out.println("线程: "+i+" : "+start+" : "+end);
//10、启动线程
new myThread(start,end,downFile,url).start();
}
}
//11、关闭连接
conn.disconnect();
} public static void main(String[] args) {
try {
new MultiPart().down();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("OK");
}
} class myThread extends Thread
{
private int start;
private int end;
private String downFile;
private URL url;
public myThread(int start, int end, String downFile, URL url) {
this.start = start;
this.end = end;
this.downFile = downFile;
this.url = url;
} public void run() {
try {
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setDoInput(true);
//设置从哪里下载。断点
conn.setRequestProperty("range", "bytes="+start+"-"+end);
conn.connect();
int code=conn.getResponseCode();
if(code==206)
{
int size=conn.getContentLength();
InputStream in=conn.getInputStream();
//写同一文件
RandomAccessFile file=new RandomAccessFile(new File(downFile), "rw");
//设置从文件的哪里开始写
file.seek(start);
byte[] b=new byte[1024];
int len=-1;
while((len=in.read(b))!=-1)
{
file.write(b, 0, len);
}
file.close();
}
conn.disconnect(); } catch (Exception e) {
e.printStackTrace();
}
}
}
Java实现多线程下载的更多相关文章
- Java实现多线程下载,支持断点续传
完整代码:https://github.com/iyuanyb/Downloader 多线程下载及断点续传的实现是使用 HTTP/1.1 引入的 Range 请求参数,可以访问Web资源的指定区间的内 ...
- Java实现多线程下载 URL以及URLConnection
主线程: public class MultiThreadDown { public static void main(String[] args) throws Exception{ //初始化Do ...
- 【Java EE 学习 22 下】【单线程下载】【单线程断点下载】【多线程下载】
一.文件下载简述 1.使用浏览器从网页上下载文件,Servlet需要增加一些响应头信息 (1)response.setContentType("application/force-downl ...
- Java 仿迅雷多线程下载
package net.webjoy.jackluo.android_json; /** * 1.http Range "bytes="+ start+end * 2.Random ...
- JAVA基础知识之网络编程——-网络基础(Java的http get和post请求,多线程下载)
本文主要介绍java.net下为网络编程提供的一些基础包,InetAddress代表一个IP协议对象,可以用来获取IP地址,Host name之类的信息.URL和URLConnect可以用来访问web ...
- JAVA多线程下载网络文件
JAVA多线程下载网络文件,开启多个线程,同时下载网络文件. 源码如下:(点击下载 MultiThreadDownload.java) import java.io.InputStream; im ...
- Java开发之多线程下载和断点续传
代码实现了多线程下载和断点续传功能 import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream ...
- java多线程下载和断点续传
java多线程下载和断点续传,示例代码只实现了多线程,断点只做了介绍.但是实际测试结果不是很理想,不知道是哪里出了问题.所以贴上来请高手修正. [Java]代码 import java.io.File ...
- Java之多线程断点下载的实现
RandomAccessFile类: 此类的实例支持对随机訪问文件的读取和写入.随机訪问文件的行为相似存储在文件系统中的一个大型 byte 数组. 存在指向该隐含数组.光标或索引,称为文件指针.输入操 ...
随机推荐
- cms中if标签的使用
<#if order.EnFriendly ?? && order.EnFriendly==1> <td class="text-center"& ...
- c构造函数
构造函数 任何一们面向对象语言里都会涉及构造函数这一概念,只是实现的方式各有差异.需要这main函数之前执行一段代码是非常容易的事情,只需要声明一对象的全局变量,在构造函数可以为所欲为干你想干的事 ...
- Windows平台下主要的内存管理途径
new / delete malloc / free CoTaskMemAlloc / CoTaskMemFree IMalloc::alloc / IMalloc/free G ...
- html动态编辑框
简述: 随着在输入框中增加字符,动态矿高度增加(IE9及以上 chrome firefox) 由于IE8 不支持oninput函数,所以不能实现此效果 事件函数: function feedDivO ...
- Search a 2D Matrix ——LeetCode
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- LeetCode——Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- SRM593(1-250pt,500pt)
SRM 593 DIV1 250pt 题意:有如下图所示的平面,每个六边形有坐标.将其中一些六边形染色,要求有边相邻的两个六边形不能染同一种颜色.给定哪些六边形需要染色,问最少需要多少种颜色. 解法: ...
- C++ STL之map常用指令
只记载本人在ACM中常用的函数. map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力. map内部为一颗 ...
- 在sql语句中使用plsql变量
示例代码如下: create or replace type ua_id_table is table of number; declare v_tab ua_id_table;begin v_tab ...
- Django URL传递参数的方法总结
1 无参数情况 配置URL及其视图如下: 1 2 3 4 (r'^hello/$', hello) def hello(request): return HttpResponse("He ...