下载文件工具类

本文介绍了一个实用的Java文件下载工具类,支持断点续传和压缩文件的下载。通过自定义监听器可以实时获取下载进度,并在下载完成时接收通知。


public class DownloadUtils {
private static final int CONNECT_TIMEOUT = 10000;
private static final int DATA_TIMEOUT = 40000;
private final static int DATA_BUFFER = 1024 * 10;

public interface DownloadListener {
public void downloading(int progress);

public void downloaded();
}

public static long download(String urlStr, File dest, boolean append,
DownloadListener downloadListener) throws Exception {
LogUtil.i("开始下载:" + urlStr);
int downloadProgress = 0;
long remoteSize = 0;
long currentSize = 0;
long totalSize = -1;

File parentFile = dest.getParentFile();
if (parentFile != null && !parentFile.exists()) {
parentFile.mkdirs();
}

if (!append || !dest.isFile()) {
dest.delete();
}

if (append && dest.exists()) {
currentSize = dest.length();
}

HttpGet request = new HttpGet(urlStr);

LogUtil.d("currentSize = " + currentSize);

if (currentSize > 0) {
request.addHeader("Range", "bytes=" + currentSize + "-");
}

HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, CONNECT_TIMEOUT);
HttpConnectionParams.setSoTimeout(params, DATA_TIMEOUT);
HttpClient httpClient = new DefaultHttpClient(params);

InputStream is = null;
FileOutputStream os = null;
try {
HttpResponse response = httpClient.execute(request);
int statusCode = response.getStatusLine().getStatusCode();
LogUtil.d("statusCode = " + statusCode);
if (statusCode == HttpStatus.SC_OK|| statusCode == HttpStatus.SC_PARTIAL_CONTENT) {

boolean isBreak = false;
if (statusCode == HttpStatus.SC_PARTIAL_CONTENT) {
isBreak = true;
}
Header[] hs = response.getAllHeaders();
if (hs != null) {
for (int i = 0; i < hs.length; i++) {
String headerName = hs[i].getName();
if ("Accept-Ranges".equalsIgnoreCase(headerName)
|| "Accept-Range".equalsIgnoreCase(headerName)
|| "Content-Ranges"
.equalsIgnoreCase(headerName)
|| "Content-Range".equalsIgnoreCase(headerName)) {
isBreak = true;
}
}
}

remoteSize = response.getEntity().getContentLength(); // max
LogUtil.d("old remoteSize = " + remoteSize);
if (isBreak) {
remoteSize += currentSize;
}
LogUtil.d("new remoteSize = " + remoteSize);

is = response.getEntity().getContent();
Header contentEncoding = response
.getFirstHeader("Content-Encoding");
if (contentEncoding != null
&& contentEncoding.getValue().equalsIgnoreCase("gzip")) {
is = new GZIPInputStream(is);
}
os = new FileOutputStream(dest, isBreak);
byte buffer[] = new byte[DATA_BUFFER];
int readSize = 0;
while ((readSize = is.read(buffer)) != -1) {
os.write(buffer, 0, readSize);
os.flush();
totalSize += readSize;
currentSize += readSize;
downloadProgress = (int) (currentSize * 100 / remoteSize);
// LogUtil.d("currentSize = " + currentSize);
if (downloadListener != null) {
downloadListener.downloading(downloadProgress);
}
if (Thread.currentThread().isInterrupted()) {
LogUtil.i("取消下载:" + urlStr);
break;
}
}
if (totalSize < 0) {
totalSize = 0;
}
LogUtil.d("currentSize = " + currentSize);
} else {
throw new Exception("网络请求失败:" + urlStr);
}

} catch (Exception e) {
throw e;
} finally {
httpClient.getConnectionManager().shutdown();
if (os != null) {
try {
os.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}

if (totalSize < 0) {
throw new Exception("Download file fail: " + urlStr);
}

if (downloadListener != null) {
downloadListener.downloaded();
}

return totalSize;
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值