恢复下载在android中不起作用
这段用于恢复下载的代码在Android中无法正常工作,尽管它在Java应用程序中运行良好。在这里,我试图下载一个zip文件,它将继续下载,但最终结果是一个无效的zip文件恢复下载在android中不起作用,android,resume-download,Android,Resume Download,这段用于恢复下载的代码在Android中无法正常工作,尽管它在Java应用程序中运行良好。在这里,我试图下载一个zip文件,它将继续下载,但最终结果是一个无效的zip文件 BufferedInputStream in = null; FileOutputStream fos = null; BufferedOutputStream bout=null; try { downloaded=0; Ht
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();
}
}
谢谢。您的zip文件已损坏,因为您认为流将从指定的范围字节恢复。它实际上从一开始就再次流式传输,因此您的文件比原始文件大。长话短说,您的服务器不支持range属性。请在您的问题中添加一些详细信息:您到底面临什么问题?有什么错误吗?如果是的话,你能把跟踪贴出来吗?如果我们想回答你,我们需要更多的细节。实际上我正在尝试下载一个zip文件…它也将继续下载..但最终结果是无效的zip文件..我还将添加一个
连接.setReadTimeout()
和一个连接.setConnectionTimeout()
你好,Lijo,'发布\u download\u STATUS.intValue()'我不了解此值。请给我此文件的参考。文件=新文件(目标路径);如果(file.exists()){downloaded=(int)file.length();connection.setRequestProperty(“范围”,“字节=”+(file.length())+“-”;}}其他{connection.setRequestProperty(“范围”,“字节=”+下载+“-”;}
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
int buf = 1024;
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 = new FileOutputStream(DESTINATION_PATH, downloaded == 0 ? false : true);
bout = new BufferedOutputStream(fos, buf);
byte[] data = new byte[buf];
while ((int x = in.read(data, 0, buf)) >= 0) {
bout.write(data, 0, x);
downloaded += x;
progressBar.setProgress(downloaded);
}