java断点下载文件_java远程文件下载,支持文件断点续传

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 

logger.info("在" + currentSize + "续传");

//续传已超过20次放弃下载

if (count 

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;

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值