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);
            }
        });
    }
相关推荐
澈20744 分钟前
C++并查集:高效解决连通性问题
java·c++·算法
郝学胜-神的一滴2 小时前
Qt 入门 01-01:从零基础到商业级客户端实战
开发语言·c++·qt·程序人生·软件构建
测试员周周2 小时前
【Appium 系列】第06节-页面对象实现 — LoginPage 实战
开发语言·前端·人工智能·python·功能测试·appium·测试用例
alexhilton2 小时前
Android上的ZeroMQ:用发布/订阅模式连接Linux服务
android·kotlin·android jetpack
2401_873479402 小时前
运营活动被薅羊毛怎么防?用IP查询+设备指纹联动封堵漏洞
java·网络·tcp/ip·github
ShiJiuD6668889992 小时前
大事件板块一
java
摇滚侠2 小时前
@Autowired 和 @Resource 的区别
java·开发语言
风别鹤2 小时前
Cocos Creator无法识别Android SDK
android
应用市场2 小时前
Android A/B 无缝更新机制深度剖析
android·网络
Wy_编程3 小时前
go语言中的结构体
开发语言·后端·golang