恢复下载不工作的机器人 [英] Resume Download not working in android
本文介绍了恢复下载不工作的机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这$ C $下恢复下载不正常的机器人,但它工作正常,在Java应用程序。在这里,我想下载一个ZIP文件,它会继续下载,但最终结果是无效的zip文件。
的BufferedInputStream在= NULL;
FileOutputStream中FOS = NULL;
的BufferedOutputStream回合= NULL;
尝试 {
下载= 0;
HttpURLConnection的连接=(HttpURLConnection类)url.openConnection();
如果(ISSUE_DOWNLOAD_STATUS.intValue()== ECMConstant.ECM_DOWNLOADING){
档案文件=新的文件(destination_path可);
如果(file.exists()){
下载=(int)的file.length();
}
}
connection.setRequestProperty(范围,字节=+下载+ - );
connection.connect();
大小= connection.getContentLength();
Dialog.setMax(大小);
在=新的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;
的System.out.println(下载);
onProgressUpdate((INT)(下载* 100 /大小));
}
更迭=真;
}赶上(例外五){
e.printStackTrace();
} 最后 {
尝试 {
附寄();
bout.close();
}赶上(IOException异常E){
e.printStackTrace();
}
}
感谢。
解决方案
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(下载);
}
This code for resuming download is not working properly in Android, although it works fine in a Java application. Here I am trying to download a zip file, and it will resume the download, but the net result is an invalid zip file.
BufferedInputStream in = null;
FileOutputStream fos = null;
BufferedOutputStream bout=null;
try {
downloaded=0;
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=" + downloaded + "-");
connection.connect();
size=connection.getContentLength();
Dialog.setMax(size);
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;
System.out.println(downloaded);
onProgressUpdate((int)(downloaded*100/size));
}
succes=true;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
in.close();
bout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Thanks.
解决方案
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);
}
这篇关于恢复下载不工作的机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文