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.text.SimpleDateFormat;
import java.util.Date;
import org.apache.log4j.Logger;
/**
* 远程文件下载,支持文件断点续传
* @author ketqi
*/
public class FileDownload {
private Logger logger = Logger.getLogger(FileDownload.class);
private static final SimpleDateFormat FORMAT = new SimpleDateFormat("yyyyMMddHHmmss");
// 远程文件的url
private URL url;
// 下载完后的目标临时文件
private File tempFile;
// 失败次数
private int count = 0;
public FileDownload(String url) throws Exception {
this.url = new URL(url);
tempFile = File.createTempFile(FORMAT.format(new Date()), ".xml");
}
/**
* @category 支持断点续传的受控端下载
* @param fileLength
* 读取远程文件的开始位置
* @return
*/
public void execute(long fileLength) {
long currentSize = fileLength;
HttpURLConnection httpConnection = null;
InputStream input = null;
RandomAccessFile targetFile = null;
try {
httpConnection = (HttpURLConnection) url.openConnection();
// 设置User-Agent
httpConnection.setRequestProperty("User-Agent", "NetFox");
// 设置断点续传的开始位置
httpConnection.setRequestProperty("RANGE", "bytes=" + fileLength + "-");
// 获得输入流
input = httpConnection.getInputStream();
targetFile = new RandomAccessFile(tempFile, "rw");
// 定位文件指针到fileLength位置
targetFile.seek(fileLength);
byte[] b = new byte[1024];
int nRead;
// 从输入流中读入字节流,然后写到文件中
while ((nRead = input.read(b, 0, 1024)) > 0) {
(targetFile).write(b, 0, nRead);
currentSize += nRead;
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
e.printStackTrace();
// 60秒钟的间隔进行再次尝试连接
logger.info("等待60秒后再次进行断点下载");
try {
Thread.sleep(60000);
} catch (Exception e1) {
}
// 续传
if (currentSize < getRemoteFileSize()) {
logger.info("在" + currentSize + "续传");
//续传已超过20次放弃下载
if (count < 20) {
execute(currentSize);
}
}
} finally {
if (targetFile != null) {
try {
targetFile.close();
} catch (Exception e) {
}
targetFile = null;
}
if (input != null) {
try {
input.close();
} catch (IOException e) {
}
input = null;
}
if(httpConnection != null){
httpConnection.disconnect();
}
}
}
/**
* @category 已下载的文件
* @return
*/
public File getFile() {
return tempFile;
}
/**
* @category 获得远程文件大小
* @return
*/
public long getRemoteFileSize() {
long size = 0;
try {
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
size = conn.getContentLength();
conn.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
return size;
}
}
转载于:https://blog.51cto.com/ketqi/865612