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);
            }
        });
    }
相关推荐
ii_best3 小时前
按键精灵支持安卓14、15系统,兼容64位环境开发辅助工具
android
美狐美颜sdk3 小时前
跨平台直播美颜SDK集成实录:Android/iOS如何适配贴纸功能
android·人工智能·ios·架构·音视频·美颜sdk·第三方美颜sdk
Fireworkitte6 小时前
Apache POI 详解 - Java 操作 Excel/Word/PPT
java·apache·excel
weixin-a153003083166 小时前
【playwright篇】教程(十七)[html元素知识]
java·前端·html
DCTANT6 小时前
【原创】国产化适配-全量迁移MySQL数据到OpenGauss数据库
java·数据库·spring boot·mysql·opengauss
Touper.6 小时前
SpringBoot -- 自动配置原理
java·spring boot·后端
黄雪超7 小时前
JVM——函数式语法糖:如何使用Function、Stream来编写函数式程序?
java·开发语言·jvm
ThetaarSofVenice7 小时前
对象的finalization机制Test
java·开发语言·jvm
思则变7 小时前
[Pytest] [Part 2]增加 log功能
开发语言·python·pytest
lijingguang7 小时前
在C#中根据URL下载文件并保存到本地,可以使用以下方法(推荐使用现代异步方式)
开发语言·c#