您的位置:首页 > 理论基础 > 计算机网络

[工具库]JFileDownloader工具类——多线程下载网络文件,并保存在本地

2013-02-20 17:37 351 查看
本人大四即将毕业的准程序员(JavaSE、JavaEE、android等)一枚,小项目也做过一点,于是乎一时兴起就写了一些工具。

我会在本博客中陆续发布一些平时可能会用到的工具。

代码质量可能不是很好,大家多担待!

代码或者思路有不妥之处,还希望大牛们能不吝赐教哈!

以下代码为本人原创,转载请注明:

本文转载,来自:/article/4917445.html

JFileDownloader:用于多线程下载网络文件,并保存在本地。

源码如下:

1.JFileDownloader类:主要负责下载的初始化可启动工作。

View Code

package com.wangjie.extrautil.jfiledownloader;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;

/**
*
* 真正的下载线程,该线程用于执行该线程所要负责下载的数据。
*
* @author wangjie
* @version 创建时间:2013-2-7 上午11:58:24
*/
public class JFileDownloadThread extends Thread{
private String urlPath;
private File destFile;
private long startPos;
/**
* 此线程需要下载的数据长度。
*/
public long length;
/**
* 此线程现在已下载好了的数据长度。
*/
public long currentLength;

private JFileDownloaderNotificationThread notificationThread;
private boolean isRunning = true;

/**
* 构造方法,可生成配置完整的JFileDownloadThread对象
* @param urlPath 要下载的目标文件URL
* @param destFile 要保存的目标文件
* @param startPos 该线程需要下载目标文件第几个byte之后的数据
* @param length 该线程需要下载多少长度的数据
* @param notificationThread 通知进度线程
*/
public JFileDownloadThread(String urlPath, File destFile, long startPos,
long length, JFileDownloaderNotificationThread notificationThread) {
this.urlPath = urlPath;
this.destFile = destFile;
this.startPos = startPos;
this.length = length;
this.notificationThread = notificationThread;
}
/**
* 该方法将执行下载功能,并把数据存储在目标文件中的相应位置。
*/
@Override
public void run() {
RandomAccessFile raf = null;
HttpURLConnection conn = null;
InputStream is = null;
try {
//            URL url = new URL("http://localhost:8080/firstserver/files/hibernate.zip");
URL url = new URL(urlPath);
conn = (HttpURLConnection)url.openConnection();
conn.setConnectTimeout(20 * 1000);
is = conn.getInputStream();
raf = new RandomAccessFile(destFile, "rw");
raf.setLength(conn.getContentLength()); // 设置保存文件的大小
//            raf.setLength(conn.getInputStream().available());

// 设置读入和写入的文件位置
is.skip(startPos);
raf.seek(startPos);

currentLength = 0; // 当前已下载好的文件长度
byte[] buffer = new byte[1024 * 1024];
int len = 0;
while(currentLength < length && -1 != (len = is.read(buffer))){
if(!isRunning){
break;
}
if(currentLength + len > length){
raf.write(buffer, 0, (int)(length - currentLength));
currentLength = length;
notificationThread.notificationProgress(); // 通知进度线程来更新进度
return;
}else{
raf.write(buffer, 0, len);
currentLength += len;
notificationThread.notificationProgress(); // 通知进度线程来更新进度
}
}
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
is.close();
raf.close();
conn.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}

}
/**
* 取消该线程下载
* @author wangjie
*/
public void cancelThread(){
isRunning = false;
}

}


使用方法如下:

String urlPath = "http://localhost:8080/firstserver/files/test.zip";
String destFilePath = "C:\\Users\\admin\\Desktop\\杂\\临时仓库\\test.zip";
int threadCount = 3;

JFileDownloader downloader = new JFileDownloader(urlPath, destFilePath, threadCount);
//或者:
JFileDownloader downloader = new JFileDownloader()
.setUrlPath(urlPath)
.setDestFilePath(destFilePath)
.setThreadCount(threadCount)
.setFileDownloadListener(new JFileDownloadListener() { // 设置进度监听器
public void downloadProgress(int progress, double speed, long remainTime) {
System.out.println("文件已下载:" + progress + "%,下载速度为:" + speed + "kb/s,剩余所需时间:" + remainTime + "毫秒");
}
public void downloadCompleted(File file, long downloadTime) {
System.out.println("文件:" + file.getName() + "下载完成,用时:" + downloadTime + "毫秒");
}
});
try {
downloader.startDownload(); // 开始下载
} catch (Exception e) {
e.printStackTrace();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: