登录
注册
开源
企业版
高校版
搜索
帮助中心
使用条款
关于我们
开源
企业版
高校版
私有云
Gitee AI
NEW
我知道了
查看详情
登录
注册
代码拉取完成,页面将自动刷新
开源项目
>
手机/移动开发
>
Android组件/项目
&&
捐赠
捐赠前请先登录
取消
前往登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
Watch
不关注
关注所有动态
仅关注版本发行动态
关注但不提醒动态
5
Star
43
Fork
19
黎明白昼
/
HQW
代码
Issues
16
Pull Requests
0
Wiki
统计
流水线
服务
Gitee Pages
质量分析
Jenkins for Gitee
腾讯云托管
腾讯云 Serverless
悬镜安全
阿里云 SAE
Codeblitz
我知道了,不再自动展开
更新失败,请稍后重试!
Issues
/
详情
移除标识
内容风险标识
本任务被
标识为内容中包含有代码安全 Bug 、隐私泄露等敏感信息,仓库外成员不可访问
下载file时,如果文件目录不存在,需要创建文件目录
待办的
#I953H6
黎明白昼
拥有者
创建于
2024-03-01 16:43
``` package com.huanqi.http.download; import androidx.annotation.NonNull; import com.huanqi.android.Utils.HQWFileUtil; import com.huanqi.android.Utils.HQWLogUtil; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Timer; import java.util.TimerTask; import okhttp3.Call; import okhttp3.Callback; import okhttp3.Response; import okhttp3.ResponseBody; public class HQWDownloadManager { Timer timer; File mFile; long mContentLength; DownloadListener callBack; public void startTimer(){ if (timer!=null){ timer.cancel(); } timer=new Timer(); timer.schedule(new TimerTask() { @Override public void run() { if (callBack!=null&&mFile!=null){ callBack.onDownloading(mFile, mFile.length(), mContentLength, HQWFileUtil.getPrintSize(mFile.length()), HQWFileUtil.getPrintSize(mContentLength)); } } },0,1000); } public void cleanTimer(){ if (timer!=null){ timer.cancel(); } } public static void DownloadFile(String url, File file, HttpMachine httpMachine, DownloadListener callBack){ HQWDownloadManager hqwDownloadManager=new HQWDownloadManager(); hqwDownloadManager.startDownloadFile(url,file,httpMachine,callBack); } public void startDownloadFile(String url, File file, HttpMachine httpMachine, DownloadListener callBack) { if ( !file.getParentFile().exists()){ file.getParentFile().mkdirs(); } Call call = DownloadConfig.getInstance().getCall(url); this.callBack=callBack; call.enqueue(new Callback() { @Override public void onFailure(@NonNull Call call, @NonNull IOException e) { cleanTimer(); callBack.onFailed(file, e); } @Override public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException { InputStream is = null; FileOutputStream out = null; try { ResponseBody responseBody = response.body(); long contentLength = responseBody.contentLength(); is = responseBody.byteStream(); out = new FileOutputStream(file); byte[] buffer = new byte[1024]; int len = -1; startTimer(); while ((len = is.read(buffer)) != -1) { out.write(buffer, 0, len); mFile=file; mContentLength=contentLength; // callBack.onDownloading(file, file.length(), contentLength, HQWFileUtil.getPrintSize(file.length()), HQWFileUtil.getPrintSize(contentLength)); } call.cancel(); cleanTimer(); callBack.onSucceed(file); } catch (Exception v) { call.cancel(); is.close(); if (out!=null){ out.flush(); out.close(); } cleanTimer(); HQWLogUtil.logi("日志打印",v.getMessage()); callBack.onFailed(file, v); } } }); if (httpMachine != null) { httpMachine.setController(new HttpController() { @Override public void Stop() { if (call != null) { call.cancel(); cleanTimer(); } } @Override public void Start() { DownloadFile(url, file, httpMachine, callBack); } }); } } /** * 断点续传下载功能 * url 超链接 * file 文件保存地址 * HttpMachine 下载管理器 * callBack调用回调 */ public static void ResumeDownloadFile(String url, File file, HttpMachine httpMachine, DownloadListener downloadListener) { DownloadRegister downloadRegister = new DownloadRegister(); ResumeDownloadCheckFileSize(url, file, downloadRegister, new FileSizeController() { @Override public void equality() {//文件存在并且相等 downloadListener.onSucceed(file); } @Override public void unlikeness() {//文件不存在或不相等 Call call = DownloadConfig.getInstance().getCall(url, file, downloadRegister.getHttpFileCountSize()); downloadRegister.setCall(call); call.enqueue(new Callback() { @Override public void onFailure(@NonNull Call call, @NonNull IOException e) { downloadListener.onFailed(file, e); } @Override public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException { InputStream is = null; FileOutputStream out = null; try { ResponseBody responseBody = response.body(); is = responseBody.byteStream(); out = new FileOutputStream(file, true); byte[] buffer = new byte[1024]; int len = -1; while ((len = is.read(buffer)) != -1) { out.write(buffer, 0, len); downloadListener.onDownloading(file, file.length(), downloadRegister.getHttpFileCountSize(), HQWFileUtil.getPrintSize(file.length()), HQWFileUtil.getPrintSize(downloadRegister.getHttpFileCountSize())); } call.cancel(); downloadListener.onSucceed(file); } catch (Exception v) { call.cancel(); is.close(); if (out!=null){ out.flush(); out.close(); } downloadListener.onStop(file); } } }); } @Override public void onFailed(File file, Exception e) { downloadListener.onFailed(file, e); } }); if (httpMachine != null) { httpMachine.setController(new HttpController() { @Override public void Stop() { if (downloadRegister.getCall() != null) { downloadRegister.getCall().cancel(); } } @Override public void Start() { ResumeDownloadFile(url, file, httpMachine, downloadListener); } }); } } /** * 断点续传文件相等检查 * * @param url 超链接 * @param file 文件保存地址 * @param downloadRegister 网络文件大小寄存器 * @param fileSizeController 文件大小相等与否回调 */ public static void ResumeDownloadCheckFileSize(String url, File file, DownloadRegister downloadRegister, FileSizeController fileSizeController) { DownloadConfig.getInstance().getCall(url).enqueue(new Callback() { @Override public void onFailure(@NonNull Call call, @NonNull IOException e) { fileSizeController.onFailed(file, e); } @Override public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException { downloadRegister.setFileCountSize((file.exists() && file.length() > 0) ? file.length() : 0); downloadRegister.setHttpFileCountSize(response.body().contentLength()); if (file.exists() && file.length() >= response.body().contentLength()) {//文件存在并且相等 fileSizeController.equality(); } else { fileSizeController.unlikeness(); } } }); } } ```
``` package com.huanqi.http.download; import androidx.annotation.NonNull; import com.huanqi.android.Utils.HQWFileUtil; import com.huanqi.android.Utils.HQWLogUtil; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Timer; import java.util.TimerTask; import okhttp3.Call; import okhttp3.Callback; import okhttp3.Response; import okhttp3.ResponseBody; public class HQWDownloadManager { Timer timer; File mFile; long mContentLength; DownloadListener callBack; public void startTimer(){ if (timer!=null){ timer.cancel(); } timer=new Timer(); timer.schedule(new TimerTask() { @Override public void run() { if (callBack!=null&&mFile!=null){ callBack.onDownloading(mFile, mFile.length(), mContentLength, HQWFileUtil.getPrintSize(mFile.length()), HQWFileUtil.getPrintSize(mContentLength)); } } },0,1000); } public void cleanTimer(){ if (timer!=null){ timer.cancel(); } } public static void DownloadFile(String url, File file, HttpMachine httpMachine, DownloadListener callBack){ HQWDownloadManager hqwDownloadManager=new HQWDownloadManager(); hqwDownloadManager.startDownloadFile(url,file,httpMachine,callBack); } public void startDownloadFile(String url, File file, HttpMachine httpMachine, DownloadListener callBack) { if ( !file.getParentFile().exists()){ file.getParentFile().mkdirs(); } Call call = DownloadConfig.getInstance().getCall(url); this.callBack=callBack; call.enqueue(new Callback() { @Override public void onFailure(@NonNull Call call, @NonNull IOException e) { cleanTimer(); callBack.onFailed(file, e); } @Override public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException { InputStream is = null; FileOutputStream out = null; try { ResponseBody responseBody = response.body(); long contentLength = responseBody.contentLength(); is = responseBody.byteStream(); out = new FileOutputStream(file); byte[] buffer = new byte[1024]; int len = -1; startTimer(); while ((len = is.read(buffer)) != -1) { out.write(buffer, 0, len); mFile=file; mContentLength=contentLength; // callBack.onDownloading(file, file.length(), contentLength, HQWFileUtil.getPrintSize(file.length()), HQWFileUtil.getPrintSize(contentLength)); } call.cancel(); cleanTimer(); callBack.onSucceed(file); } catch (Exception v) { call.cancel(); is.close(); if (out!=null){ out.flush(); out.close(); } cleanTimer(); HQWLogUtil.logi("日志打印",v.getMessage()); callBack.onFailed(file, v); } } }); if (httpMachine != null) { httpMachine.setController(new HttpController() { @Override public void Stop() { if (call != null) { call.cancel(); cleanTimer(); } } @Override public void Start() { DownloadFile(url, file, httpMachine, callBack); } }); } } /** * 断点续传下载功能 * url 超链接 * file 文件保存地址 * HttpMachine 下载管理器 * callBack调用回调 */ public static void ResumeDownloadFile(String url, File file, HttpMachine httpMachine, DownloadListener downloadListener) { DownloadRegister downloadRegister = new DownloadRegister(); ResumeDownloadCheckFileSize(url, file, downloadRegister, new FileSizeController() { @Override public void equality() {//文件存在并且相等 downloadListener.onSucceed(file); } @Override public void unlikeness() {//文件不存在或不相等 Call call = DownloadConfig.getInstance().getCall(url, file, downloadRegister.getHttpFileCountSize()); downloadRegister.setCall(call); call.enqueue(new Callback() { @Override public void onFailure(@NonNull Call call, @NonNull IOException e) { downloadListener.onFailed(file, e); } @Override public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException { InputStream is = null; FileOutputStream out = null; try { ResponseBody responseBody = response.body(); is = responseBody.byteStream(); out = new FileOutputStream(file, true); byte[] buffer = new byte[1024]; int len = -1; while ((len = is.read(buffer)) != -1) { out.write(buffer, 0, len); downloadListener.onDownloading(file, file.length(), downloadRegister.getHttpFileCountSize(), HQWFileUtil.getPrintSize(file.length()), HQWFileUtil.getPrintSize(downloadRegister.getHttpFileCountSize())); } call.cancel(); downloadListener.onSucceed(file); } catch (Exception v) { call.cancel(); is.close(); if (out!=null){ out.flush(); out.close(); } downloadListener.onStop(file); } } }); } @Override public void onFailed(File file, Exception e) { downloadListener.onFailed(file, e); } }); if (httpMachine != null) { httpMachine.setController(new HttpController() { @Override public void Stop() { if (downloadRegister.getCall() != null) { downloadRegister.getCall().cancel(); } } @Override public void Start() { ResumeDownloadFile(url, file, httpMachine, downloadListener); } }); } } /** * 断点续传文件相等检查 * * @param url 超链接 * @param file 文件保存地址 * @param downloadRegister 网络文件大小寄存器 * @param fileSizeController 文件大小相等与否回调 */ public static void ResumeDownloadCheckFileSize(String url, File file, DownloadRegister downloadRegister, FileSizeController fileSizeController) { DownloadConfig.getInstance().getCall(url).enqueue(new Callback() { @Override public void onFailure(@NonNull Call call, @NonNull IOException e) { fileSizeController.onFailed(file, e); } @Override public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException { downloadRegister.setFileCountSize((file.exists() && file.length() > 0) ? file.length() : 0); downloadRegister.setHttpFileCountSize(response.body().contentLength()); if (file.exists() && file.length() >= response.body().contentLength()) {//文件存在并且相等 fileSizeController.equality(); } else { fileSizeController.unlikeness(); } } }); } } ```
评论 (
0
)
黎明白昼
创建了
任务
登录
后才可以发表评论
状态
待办的
待办的
进行中
已完成
已关闭
负责人
未设置
标签
未设置
标签管理
里程碑
未关联里程碑
未关联里程碑
Pull Requests
未关联
未关联
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
未关联
分支 (2)
标签 (38)
master
demo
1.9.9
1.9.8
1.9.7
1.9.6
1.9.5
1.9.0
1.6.8
1.6.4
1.6.3
1.6.2
1.6.1
1.6.0
1.5.9
1.5.8
1.5.7
1.5.1
1.4.1
1.4.0
Beta1.3.7
1.3.6
1.3.5
1.3.4
1.3.3
1.2.8
1.2.7
1.2.6
1.2.5
1.2.4
1.2.3
1.2.2
1.2.1
1.1.7
1.1.6
1.1.5
1.1.2
1.1.1
1.0.9
1.0.8
开始日期   -   截止日期
-
置顶选项
不置顶
置顶等级:高
置顶等级:中
置顶等级:低
优先级
不指定
严重
主要
次要
不重要
参与者(1)
Android
1
https://gitee.com/BAILIS/HQW.git
[email protected]
:BAILIS/HQW.git
BAILIS
HQW
HQW
点此查找更多帮助
搜索帮助
Git 命令在线学习
如何在 Gitee 导入 GitHub 仓库
Git 仓库基础操作
企业版和社区版功能对比
SSH 公钥设置
如何处理代码冲突
仓库体积过大,如何减小?
如何找回被删除的仓库数据
Gitee 产品配额说明
GitHub仓库快速导入Gitee及同步更新
什么是 Release(发行版)
将 PHP 项目自动发布到 packagist.org
评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册