简历HTTP文件下载在java中 [英] Resume http file download in java
本文介绍了简历HTTP文件下载在java中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
网址URL =新的URL(http://download.thinkbroadband.com/20MB.zip);
URLConnection的连接= url.openConnection();
文件fileThatExists =新的文件(路径);
的OutputStream输出=新的FileOutputStream(路径,真实);
connection.setRequestProperty(范围,字节=+ fileThatExists.length()+ - );
connection.connect();
INT lenghtOfFile = connection.getContentLength();
的InputStream输入=新的BufferedInputStream(url.openStream());
字节的数据[] =新的字节[1024];
总长= 0;
而((计数= input.read(数据))!= - 1){
共有+ =计数;
output.write(数据,0,计数);
}
在此code我尝试恢复下载。目标文件是20MB。但是,当我停止下载的10MB,那么contunue,我得到的文件,文件大小为30MB。它似乎继续写入文件,但不能部分来自服务器下载。 wget的-c与此文件的伟大工程。我怎样才能恢复文件下载?
解决方案
HttpURLConnection的连接=(HttpURLConnection类)url.openConnection();
如果(ISSUE_DOWNLOAD_STATUS.intValue()== ECMConstant.ECM_DOWNLOADING){
档案文件=新的文件(destination_path可);
如果(file.exists()){
下载=(int)的file.length();
connection.setRequestProperty(范围,字节=+(file.length())+ - );
}
}其他{
connection.setRequestProperty(范围,字节=+下载+ - );
}
connection.setDoInput(真正的);
connection.setDoOutput(真正的);
progressBar.setMax(connection.getContentLength());
在=新的BufferedInputStream(connection.getInputStream());
FOS =(下载== 0)?新的FileOutputStream(destination_path可):新的FileOutputStream(destination_path可,真正的);
布特=新的BufferedOutputStream(FOS,1024);
byte []的数据=新的字节[1024];
INT X = 0;
而((X = in.read(数据,0,1024))> = 0){
bout.write(数据,0,x)的;
下载+ = X;
progressBar.setProgress(下载);
}
这是不是我的code,但它的工作原理。
URL url = new URL("http://download.thinkbroadband.com/20MB.zip");
URLConnection connection = url.openConnection();
File fileThatExists = new File(path);
OutputStream output = new FileOutputStream(path, true);
connection.setRequestProperty("Range", "bytes=" + fileThatExists.length() + "-");
connection.connect();
int lenghtOfFile = connection.getContentLength();
InputStream input = new BufferedInputStream(url.openStream());
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
output.write(data, 0 , count);
}
in this code I try to resume download. Target file is 20MB. But when I stop download on 10mb, then contunue, I get file with filesize 30MB. It seems that it continue writing to file, but cant partly download from server. Wget -c works great with this file. How can I resume file download?
解决方案
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
if(ISSUE_DOWNLOAD_STATUS.intValue()==ECMConstant.ECM_DOWNLOADING){
File file=new File(DESTINATION_PATH);
if(file.exists()){
downloaded = (int) file.length();
connection.setRequestProperty("Range", "bytes="+(file.length())+"-");
}
}else{
connection.setRequestProperty("Range", "bytes=" + downloaded + "-");
}
connection.setDoInput(true);
connection.setDoOutput(true);
progressBar.setMax(connection.getContentLength());
in = new BufferedInputStream(connection.getInputStream());
fos=(downloaded==0)? new FileOutputStream(DESTINATION_PATH): new FileOutputStream(DESTINATION_PATH,true);
bout = new BufferedOutputStream(fos, 1024);
byte[] data = new byte[1024];
int x = 0;
while ((x = in.read(data, 0, 1024)) >= 0) {
bout.write(data, 0, x);
downloaded += x;
progressBar.setProgress(downloaded);
}
This is not my code, but it works.
这篇关于简历HTTP文件下载在java中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文