繁体   English   中英

Android : 如何使用 HttpUrlConnection 暂停和恢复下载?

[英]Android : How to pause and resume Download using HttpUrlConnection?

我想为 android 制作一个下载管理器我想知道如何添加暂停和恢复功能我正在使用 android 的 HttpUrlConnection 类来获取数据。我还想知道如何访问我从 HttpUrlConnection 获得的 InputStream。 getInputStream() 由于互联网连接而中断(比如用户关闭互联网连接)。 任何示例代码都会有所帮助

下载器示例在这里

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);
}

积分https : //stackoverflow.com/users/705297/pomatu

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM