JAVA多线程下载Demo

JAVA多线程下载Demo
一.案例说明
   获取网络连接,采用多线程的方式下载服务器上的文件。
二.具体实现代码
  1.InternetConnection类
 用于获取网络连接。
完整代码:
package com.internet;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import com.thread.DownloadThread;


public class InternetConnection {
private  int threadcount;

public int getThreadcount() {
return threadcount;
}

public void setThreadcount(int threadcount) {
this.threadcount = threadcount;
}

public InternetConnection(int threadcount) {
super();
this.threadcount=threadcount;
}
public static String getFileName(String path){
int index=path.lastIndexOf("/");
return path.substring(index+1);
}
public void connServices(String pathurl){

//创建URL对象
try {
URL url=new URL(pathurl);
//获取网络连接对象
HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();
//设置请求方式,读取超时以及连接超时时间
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setReadTimeout(5000);
httpURLConnection.setConnectTimeout(5000);
//建立连接
httpURLConnection.connect();
//判断是否请求成功
if(httpURLConnection.getResponseCode()==200){
//获取要下载的文件的大小
int filelength=httpURLConnection.getContentLength();
//给文件创建临时文件及路径
File file=new File(InternetConnection.getFileName(pathurl));
//创建随机存储文件对象
RandomAccessFile randomAccessFile=new RandomAccessFile(file, "rwd");
//设置临时文件的大小与文件大小相同
randomAccessFile.seek(filelength);
//计算下载的长度
int size=filelength/threadcount;
for(int i=0;i<threadcount;i++){
int startindex=i*size;
int endindex=(i+1)*size-1;
if(i==threadcount-1){
endindex=filelength-1;
}
//开辟三个线程,设置每个线程下载的范围
new DownloadThread(pathurl,i, startindex,endindex).start();
}


}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
 2.DownloadThread类,继承Thread类
完整代码:
    package com.thread;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import com.internet.InternetConnection;


public class DownloadThread extends Thread {

private int id;
private int startindex;
private int endindex;
private String path;

public DownloadThread(String pathurl, int id, int startindex, int endindex) {
super();
this.path=pathurl;
this.id = id;
this.startindex = startindex;
this.endindex = endindex;
}
@Override
public void run() {
try {
String urlpath=path;
URL url=new URL(urlpath);
//获取网络连接对象
HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();
//设置请求方式,读取超时以及连接超时时间
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setReadTimeout(5000);
httpURLConnection.setConnectTimeout(5000);
//设置每个线程的请求范围
httpURLConnection.setRequestProperty("Range", "bytes="+startindex+"-"+endindex);
//部分请求成功返回206
if(httpURLConnection.getResponseCode()==206){
//获取输入流对象
InputStream inputStream=httpURLConnection.getInputStream();
//读取长度设置
byte[] b=new byte[1024];
//设置提交文件的名称
File file =new File(InternetConnection.getFileName(urlpath));
RandomAccessFile randomAccessFile=new RandomAccessFile(file,"rwd");
int len=0;
int total=0;
while((len=inputStream.read(b))!=-1){
randomAccessFile.write(b,0,len);
total+=len;
System.out.println("线程"+id+"写出了"+total+"个字节");
}
System.out.println("线程"+id+"写完了"+total+"所有字节-----------");
randomAccessFile.close();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
三.测试入口
public class DownloadTest {

public static void main(String[] args) {
InternetConnection in=new InternetConnection(3);
in.connServices("http://192.168.3.2:8080/baiduditu_775.apk");
}
}
1.得到服务器下载文件的大小,然后在本地设置一个临时文件和服务器端文件大小一致 a)获得访问网络地址 b)通过URL对象的openConnection()方法打开连接,返回一个连接对象 c)设置请求头 i.setRequestMethod ii.setConnectTimeout iii.setReadTimeout d)判断是否响应成功 e)获取文件长度(getContentLength()) f)随机访问文件的读取与写入RandomAccessFile(file, mode) g)设置临时文件与服务器文件大小一致(setLength()) h)关闭临时文件 2.计算出每个线程下载的大小(开始位置,结束位置) a)计算出每个线程下载的大小 b)for循环,计算出每个线程的开始、结束位置 c)最后一个线程处理 3.每创建好一次就要开启线程下载 a)构造方法 b)通过URL对象的openConnection()方法打开连接,返回一个连接对象 c)设置请求头 i.setRequestMethod ii.setConnectTimeout d)判断是否响应成功(206) e)获取每个线程返回的流对象 f)随机访问文件的读取与写入RandomAccessFile(file, mode) g)指定开始位置 h)循环读取 i.保存每个线程下载位置 ii.记录每次下载位置 iii.关闭临时记录位置文件 iv.随机本地文件写入 v.记录已下载大小 i)关闭临时文件 j)关闭输入流 4.为了杀死线程还能继续下载的情况下,从本地文件上读取已经下载文件的开始位置 a)创建保存记录结束位置的文件 b)读取文件 c)将流转换为字符 d)获取记录位置 e)把记录位置赋给开始位置 5.当你的n个线程都下载完毕的时候我进行删除记录下载位置的缓存文件 a)线程下载完就减去 b)当没有正在运行的线程时切文件存在时删除文件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值