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);
            }
        });
    }
相关推荐
C_Liu_18 分钟前
C++:list
开发语言·c++
my rainy days25 分钟前
C++:友元
开发语言·c++·算法
编程乐学27 分钟前
安卓非原创--基于Android Studio 实现的天气预报App
android·ide·android studio·课程设计·大作业·天气预报·安卓大作业
小梁努力敲代码30 分钟前
java数据结构--List的介绍
java·开发语言·数据结构
摸鱼的老谭1 小时前
构建Agent该选Python还是Java ?
java·python·agent
云知谷1 小时前
【HTML】网络数据是如何渲染成HTML网页页面显示的
开发语言·网络·计算机网络·html
lang201509281 小时前
Spring Boot 官方文档精解:构建与依赖管理
java·spring boot·后端
夫唯不争,故无尤也1 小时前
Tomcat 启动后只显示 index.jsp,没有进入你的 Servlet 逻辑
java·servlet·tomcat
zz-zjx1 小时前
Tomcat核心组件全解析
java·tomcat
Deschen1 小时前
设计模式-外观模式
java·设计模式·外观模式