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);
            }
        });
    }
相关推荐
草莓熊Lotso5 小时前
C++11 核心特性实战:列表初始化 + 右值引用与移动语义(附完整代码)
java·服务器·开发语言·汇编·c++·人工智能·经验分享
初夏睡觉6 小时前
从0开始c++,但是重置版,第1篇(c++基本框架)
开发语言·c++
CaspianSea8 小时前
编译Android 16 TV模拟器(一)
android
workflower11 小时前
时序数据获取事件
开发语言·人工智能·python·深度学习·机器学习·结对编程
CoderYanger12 小时前
C.滑动窗口-求子数组个数-越长越合法——2799. 统计完全子数组的数目
java·c语言·开发语言·数据结构·算法·leetcode·职场和发展
廋到被风吹走12 小时前
【数据库】【MySQL】InnoDB外键解析:约束机制、性能影响与最佳实践
android·数据库·mysql
C++业余爱好者12 小时前
Java 提供了8种基本数据类型及封装类型介绍
java·开发语言·python
想用offer打牌12 小时前
RocketMQ如何防止消息丢失?
java·后端·架构·开源·rocketmq
林杜雨都12 小时前
Action和Func
开发语言·c#
皮卡龙12 小时前
Java常用的JSON
java·开发语言·spring boot·json