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");
}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值