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;
}
}
分享到:
相关推荐
文件下载的工具类java代码,设置编码格式,避免下载中文乱码
Java文件工具类
项目中添加该工具类,用一下语句调用 FTPUtil ftpUtil = new FTPUtil("172.16.1.41",21,"admin","123456"); ftpUtil.downLoadFile("20190913.txt","D:\\ftplocal\\20190913.txt"); ftpUtil.uploadFile&#...
java文件下载工具类,可以下载文件
本文主要目的是记录自己基于SpringMVC实现的文件上传和下载的工具类的编写,代码经过测试可以直接运行在以后的项目中。开发的主要思路是对上传和下载文件进行抽象,把上传和下载的核心功能抽取出来分装成类。
java文件下载工具类
使用apache的common-net.jar,实现ftp文件的上传和下载,封装成工具类,方便开发。
ftp 上传 下载 工具类 ftp 上传 下载 工具类 ftp 上传 下载 工具类 ftp 上传 下载 工具类
apache工具类,StringUtil和CollectionUtils,,以及其他的Util,apache工具类,StringUtil,,以及其他的Util,
文件操作工具类,包含生成保存,复制,删除,读取,获取文件名,获取文件列表等等,只有你想不到,没有你找不到的Android端工具类,复制到项目中可直接使用
很强大的最底层的socket文件上传功能
图片的上传下载及工具类 上传图片,且在页面显示图片 上传下载的工具类
6.[工具类] 使用Java程序来实现HTTP文件的队列下载 .java 7.[工具类] 文件操作工具类 .java 8.[工具类] 序列化保存为XML文件的工具类 .java 9.[工具类] 验证码img .jsp 10.[工具类] 中文验证.jsp ......... 等等20几...
很好的文件操作工具类 好东西要分享
使用Apache commons net组件实现ftp上传与下载功能
字符串工具类/数据类型转换类/集合工具类/数组工具类/Properties文件操作类/常用流操作工具类/编码工具类/Json工具类/日期工具类/下载文件工具类/解压ZIP工具类/文件编码转码
[工具类] 使用Java程序来实现HTTP文件的队列下载 .java [工具类] 文件操作工具类 .java [工具类] 序列化保存为XML文件的工具类 .java [工具类] 一个压缩工具类 .java [工具类] 用java编写简单UDP网络通信程序 ....
改工具是方便自己开发FTP上传下载需求写的,直接拿来就可以使用,方便那些自己懒得写,或者是想借鉴开发的人用。
文件处理工具类,文件处理工具类,文件处理工具类,文件处理工具类