java urlconn 下载慢_使用HttpURLConnection下载文件时出现 java.io.FileNotFoundException彻底解决办法...

import java.io.File;

import java.io.IOException;

import java.io.InputStream;

import java.io.RandomAccessFile;

import java.net.HttpURLConnection;

import java.net.URL;

import java.net.URLEncoder;

/**

* 多线程下载

* @author bing

*

*/

public class OmbDownloadOfThreadsUtil {

private String urlPath ; // 资源网络路径

private String targetFilePath ; // 所下载文件的保存路径

private int threadNum ; // 启用多少条线程进行下载

// 用于下载线程对象集合

private DownloadThread[] downloadThreads ;

// 要下载文件的大小

private int fileSize ;

public OmbDownloadOfThreadsUtil(String urlPath, String targetFilePath,

int threadNum) {

this.urlPath=urlPath;

this.targetFilePath=targetFilePath;

this.threadNum=threadNum;

downloadThreads=newDownloadThread[threadNum] ;

}

public void downloadFile() throws Exception{

URL url=newURL(urlPath) ;

HttpURLConnection conn= (HttpURLConnection) url.openConnection() ;

conn.setConnectTimeout(4*1000) ;

conn.setRequestMethod("GET") ;

conn.setRequestProperty(

"Accept",

"image/gif, image/jpeg, image/pjpeg, image/pjpeg, " +

"application/x-shockwave-flash, application/xaml+xml, " +

"application/vnd.ms-xpsdocument, application/x-ms-xbap, " +

"application/x-ms-application, application/vnd.ms-excel, " +

"application/vnd.ms-powerpoint, application/msword, */*");

conn.setRequestProperty("Accept-Language", "zh-CN");

conn.setRequestProperty("Charset", "UTF-8");

//设置浏览器类型和版本、操作系统,使用语言等信息

conn.setRequestProperty(

"User-Agent",

"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; Trident/4.0; " +

".NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; " +

".NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");

//设置为长连接

conn.setRequestProperty("Connection", "Keep-Alive");

//得到要下载文件的大小

fileSize=conn.getContentLength() ;

System.out.println("fileSize:"+fileSize);

//断开连接

conn.disconnect() ;

//计算每条线程需要下载的大小

int preThreadDownloadSize=fileSize/threadNum+1 ;

System.out.println("preThreadDownloadSize:"+preThreadDownloadSize);

RandomAccessFile file=newRandomAccessFile(targetFilePath, "rw") ;

file.setLength(fileSize) ;

file.close() ;

for (int i=0; i

// 计算每条线程下载的起始位置

int startPos=i*preThreadDownloadSize+1 ;

RandomAccessFile currentPart=newRandomAccessFile(targetFilePath, "rw") ;

currentPart.seek(startPos) ;

downloadThreads[i] = new DownloadThread(startPos,preThreadDownloadSize,currentPart) ;

new Thread(downloadThreads[i]).start() ;

}

}

/**

* 获取下载的完成百分比

* @return 完成的百分比

*/

public double getCompleteRate() {

// 统计多条线程已经下载的总大小

int sumSize=0;

for (int i=0; i

sumSize += downloadThreads[i].hasReadLength;

}

// 返回已经完成的百分比

return sumSize * 1.0 / fileSize;

}

/**

* 用于下载的线程

* @author bing

*

*/

private final class DownloadThread implements Runnable{

private int startPos ;

private int preThreadDownloadSize ;

private RandomAccessFile currentPart ;

//已下载长度

private int hasReadLength ;

public DownloadThread(int startPos, int preThreadDownloadSize,

RandomAccessFile currentPart) {

this.startPos=startPos;

this.preThreadDownloadSize=preThreadDownloadSize;

this.currentPart=currentPart;

}

@Override

public void run() {

InputStream inputStream=null;

try{

URL url=newURL(urlPath) ;

HttpURLConnection conn= (HttpURLConnection) url.openConnection() ;

conn.setConnectTimeout(4*1000) ;

conn.setRequestMethod("GET") ;

conn.setRequestProperty(

"Accept",

"image/gif, image/jpeg, image/pjpeg, image/pjpeg, " +

"application/x-shockwave-flash, application/xaml+xml, " +

"application/vnd.ms-xpsdocument, application/x-ms-xbap, " +

"application/x-ms-application, application/vnd.ms-excel, " +

"application/vnd.ms-powerpoint, application/msword, */*");

conn.setRequestProperty("Accept-Language", "zh-CN");

conn.setRequestProperty("Charset", "UTF-8");

inputStream=conn.getInputStream() ;

inputStream.skip(startPos) ;//定位到开始位置

byte[] buffer=newbyte[1024] ;

int temp=0;

while(hasReadLength

&&(temp=inputStream.read(buffer))!=-1){

currentPart.write(buffer,0,temp) ;

hasReadLength += temp ;

}

}catch(Exception e){

e.printStackTrace() ;

}finally{

try {

currentPart.close() ;

} catch (Exception e) {

e.printStackTrace();

}

try {

inputStream.close() ;

} catch (Exception e) {

e.printStackTrace();

}

}

}

}

public static void main(String[] args) throws Exception {

String songName="许嵩 - 半城烟沙.mp3";

songName=URLEncoder.encode(songName,"UTF-8") ;

String urlPath="http://172.16.2.50:8080/mp3/"+songName ;

String targetDir="E:"+File.separator+songName ;

OmbDownloadOfThreadsUtil odtu=newOmbDownloadOfThreadsUtil(urlPath,targetDir, 6) ;

odtu.downloadFile() ;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值