{"id":1385,"date":"2021-07-11T15:55:40","date_gmt":"2021-07-11T07:55:40","guid":{"rendered":"https:\/\/badgameshow.com\/fly\/?p=1385"},"modified":"2021-07-11T15:56:53","modified_gmt":"2021-07-11T07:56:53","slug":"util-%e4%b8%8b%e8%bc%89%e6%aa%94%e6%a1%88%e5%88%b0%e6%9c%ac%e5%9c%b0%e7%ab%af","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/fly\/util-%e4%b8%8b%e8%bc%89%e6%aa%94%e6%a1%88%e5%88%b0%e6%9c%ac%e5%9c%b0%e7%ab%af\/","title":{"rendered":"Util \u4e0b\u8f09\u6a94\u6848\u5230\u672c\u5730\u7aef"},"content":{"rendered":"<h4>1.\u5c0e\u5165Okhttp3<\/h4>\n<pre><code class=\"language-Gradle line-numbers\">implementation 'com.squareup.okhttp3:okhttp:4.9.0'\n<\/code><\/pre>\n<h4>2.DownloadUtil<\/h4>\n<pre><code class=\"language-Java line-numbers\">import okhttp3.Call;\nimport okhttp3.Callback;\nimport okhttp3.OkHttpClient;\nimport okhttp3.Request;\nimport okhttp3.Response;\n\npublic class DownloadUtil {\n    private static DownloadUtil downloadUtil;\n    private final OkHttpClient okHttpClient;\n\n    public static DownloadUtil get() {\n        if (downloadUtil == null) {\n            downloadUtil = new DownloadUtil();\n        }\n        return downloadUtil;\n    }\n\n    private DownloadUtil() {\n        okHttpClient = new OkHttpClient();\n    }\n\n    \/**\n     * @param url          \u4e0b\u8f7d\u8fde\u63a5\n     * @param destFileDir  \u4e0b\u8f7d\u7684\u6587\u4ef6\u50a8\u5b58\u76ee\u5f55\n     * @param destFileName \u4e0b\u8f7d\u6587\u4ef6\u540d\u79f0\n     * @param listener     \u4e0b\u8f7d\u76d1\u542c\n     *\/\n    public void download(final String url, final String destFileDir, final String destFileName, final OnDownloadListener listener) {\n        Request request = new Request.Builder().url(url).build();\n        okHttpClient.newCall(request).enqueue(new Callback() {\n            @Override\n            public void onFailure(Call call, IOException e) {\n                \/\/ \u4e0b\u8f7d\u5931\u8d25\u76d1\u542c\u56de\u8c03\n                listener.onDownloadFailed(e);\n            }\n\n            @Override\n            public void onResponse(Call call, Response response) {\n                InputStream is = null;\n                byte[] buf = new byte[2048];\n                int len = 0;\n                FileOutputStream fos = null;\n                \/\/ \u50a8\u5b58\u4e0b\u8f7d\u6587\u4ef6\u7684\u76ee\u5f55\n                File dir = new File(destFileDir);\n                if (!dir.exists()) {\n                    dir.mkdirs();\n                }\n                File file = new File(dir, destFileName);\n                try {\n                    is = response.body().byteStream();\n                    long total = response.body().contentLength();\n                    fos = new FileOutputStream(file);\n                    long sum = 0;\n                    while ((len = is.read(buf)) != -1) {\n                        fos.write(buf, 0, len);\n                        sum += len;\n                        int progress = (int) (sum * 1.0f \/ total * 100);\n                        \/\/ \u4e0b\u8f7d\u4e2d\u66f4\u65b0\u8fdb\u5ea6\u6761\n                        listener.onDownloading(progress);\n                    }\n                    fos.flush();\n                    \/\/ \u4e0b\u8f7d\u5b8c\u6210\n                    listener.onDownloadSuccess(file);\n                } catch (Exception e) {\n                    listener.onDownloadFailed(e);\n                } finally {\n                    try {\n                        if (is != null)\n                            is.close();\n                    } catch (IOException e) {\n                    }\n                    try {\n                        if (fos != null)\n                            fos.close();\n                    } catch (IOException e) {\n                    }\n                }\n            }\n        });\n    }\n\n    public interface OnDownloadListener {\n        \/**\n         * @param file \u4e0b\u8f7d\u6210\u529f\u540e\u7684\u6587\u4ef6\n         *\/\n        void onDownloadSuccess(File file);\n\n        \/**\n         * @param progress \u4e0b\u8f7d\u8fdb\u5ea6\n         *\/\n        void onDownloading(int progress);\n\n        \/**\n         * @param e \u4e0b\u8f7d\u5f02\u5e38\u4fe1\u606f\n         *\/\n        void onDownloadFailed(Exception e);\n    }\n}\n<\/code><\/pre>\n\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>1.\u5c0e\u5165Okhttp3 implementation &#8216;com.squareup.okhttp3:okhttp &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[9],"tags":[191,14,153],"class_list":["post-1385","post","type-post","status-publish","format-standard","hentry","category-util","tag-download","tag-java","tag-storage"],"_links":{"self":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/1385","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/comments?post=1385"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/1385\/revisions"}],"predecessor-version":[{"id":1386,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/1385\/revisions\/1386"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/media?parent=1385"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/categories?post=1385"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/tags?post=1385"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}