android从ftp,阿里云,腾讯云下载zip

复制代码
//1.开始下载语言包
public static void downloadFileLanguage(String path) {
    activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            System.out.print("-------java---downloadFileLanguage------path:" + path + "\n");
            File sdDir = new File(activity.getFilesDir(), aikitPathReal);///data/user_de/0/org.cocos2d.demo/files/xzhd/aikit/
            aikitPathInData = sdDir.getAbsolutePath() + "/";
            FileUtils.createDirectory(aikitPathInData);
            String url = "https://tan-xin-game.oss-cn-beijing.aliyuncs.com/client/LandHengHonor/webPC5/RES_XTTS/" + path + ".zip";
            DataTool.downloadFile(url, aikitPathInData, path + ".zip");
        }
    });
}
复制代码
/**
     * 下载zip文件
     *
     * @param url
     */
    public static void downloadFile(final String url, String aikitPathInData, String localzip) {
        OkHttpClient.Builder builder = new OkHttpClient.Builder().connectTimeout(20, TimeUnit.SECONDS).writeTimeout(5, TimeUnit.SECONDS).readTimeout(5, TimeUnit.SECONDS);
        Request request = new Request.Builder().url(url).build();
        builder.build().newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                InputStream is = null;
                byte[] buf = new byte[4096];
                int len = 0;
                FileOutputStream fos = null;
                // 储存下载文件的目录
                String savePath = aikitPathInData;//Tools.isExistDir_html(filePath);
                try {

                    is = response.body().byteStream();
                    long total = response.body().contentLength();
//                    File file = new File(savePath, getNameFromUrl(url));
                    File file = new File(savePath, localzip);
                    fos = new FileOutputStream(file);
                    long sum = 0;
                    while ((len = is.read(buf)) != -1) {
                        fos.write(buf, 0, len);
                        sum += len;
                        int progress = (int) (sum * 1.0f / total * 100);
                        // 下载中
                        System.out.print("---------------progress:" + progress);
                    }
                    fos.flush();
                } catch (Exception e) {
                    //
                    e.printStackTrace();
                } finally {
                    try {
                        if (is != null) is.close();
                    } catch (IOException e) {
                    }
                    try {
                        if (fos != null) fos.close();
                    } catch (IOException e) {
                    }
                }
                //下载完成,先删除过时数据,并解压最新的热更新数据(必须这样操作 不然会导致无法覆盖旧数据,导致数据更新失败)
                // deleteOldData();
                DataTool.unZip(aikitPathInData + localzip);
            }
        });
    }
相关推荐
nbwenren18 分钟前
node.js内置模块之---crypto 模块
java
冬奇Lab36 分钟前
Camera2 API架构基础:Android视频系统的大门
android·音视频开发·源码阅读
三道渊1 小时前
C语言:文件I/O
c语言·开发语言·数据结构·c++
hnlgzb1 小时前
安卓app kotlin语法,Hilt是什么东西?
android·开发语言·kotlin
没用的阿_吉1 小时前
windows10 Qt5.15.14 msvc2019 编译部署
开发语言·qt
聊聊MES那点事1 小时前
JavaScript图表控件AG Charts使用教程:使用AG Charts React实时更新柱状图
开发语言·javascript·react.js·图表控件
weyyhdke1 小时前
springboot和springframework版本依赖关系
java·spring boot·后端
ywf12151 小时前
Go基础之环境搭建
开发语言·后端·golang
是有头发的程序猿2 小时前
用Open Claw接口做1688选品、价格监控、货源对比
开发语言·c++·人工智能
chools2 小时前
Java后端拥抱AI开发之个人学习路线 - - Spring AI【第一期】
java·人工智能·学习·spring·ai