{"id":384,"date":"2023-02-25T08:40:22","date_gmt":"2023-02-25T00:40:22","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=384"},"modified":"2023-04-30T14:38:58","modified_gmt":"2023-04-30T06:38:58","slug":"retrofit2-learning-6-upload-a-single-file","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/retrofit2-learning-6-upload-a-single-file\/","title":{"rendered":"Retrofit2\u5b66\u4e60\u4e4b\u516d\uff1a\u4e0a\u4f20\u5355\u4e2a\u6587\u4ef6"},"content":{"rendered":"<p>Retrofit2 \u4e0a\u4f20\u9700\u8981\u7528\u5230 Multipart\uff0c\u4f7f\u7528 MultipartBody.Part \u6807\u8bc6\u6587\u4ef6\u4f53\u3002<\/p>\n<h2>\u81ea\u5b9a\u4e49Headers<\/h2>\n<p><!-- more --><\/p>\n<h3>\u521b\u5efa\u4e1a\u52a1\u8bf7\u6c42\u63a5\u53e3<\/h3>\n<pre><code class=\"language-java\">public interface Api {\n\n    \/**\n     * \u4e0a\u4f20\u5355\u4e2a\u6587\u4ef6\n     * @param description\n     * @param file\n     * @return\n     *\/\n    @Multipart\n    @POST(&quot;upload&quot;)\n    Call&lt;ResponseBody&gt; uploadFile(@Part(&quot;description&quot;) RequestBody description, @Part MultipartBody.Part file);\n\n}<\/code><\/pre>\n<h3>\u521b\u5efa\u4e00\u4e2aRetrofit\u7684\u5b9e\u4f8b\uff0c\u7136\u540e\u5229\u7528Retrofit\u5b9e\u4f8b\u521b\u5efa\u63a5\u53e3\u5bf9\u8c61\u548c\u8c03\u7528\u63a5\u53e3\u65b9\u6cd5<\/h3>\n<pre><code class=\"language-java\">public static final String MULTIPART_FORM_DATA = &quot;multipart\/form-data&quot;;\n\n@NonNull\nprivate RequestBody prepareFromPart(String description) {\n    return RequestBody.create(MediaType.parse(MULTIPART_FORM_DATA), description);\n}\n\n\/**\n * \u4e00\u4e2a\u57df\u5bf9\u5e94\u4e00\u4e2a\u6587\u4ef6\n * @param partName\n * @param filePath\n * @return\n *\/\nprivate MultipartBody.Part prepareFilePart(String partName, String filePath) {\n    File file = new File(filePath);\n    \/\/ \u4e3afile\u5efa\u7acbRequestBody\u5b9e\u4f8b\n    RequestBody requestFile = RequestBody.create(MediaType.parse(MULTIPART_FORM_DATA), file);\n    \/\/ MultipartBody.Part\u501f\u52a9\u6587\u4ef6\u540d\u5b8c\u6210\u6700\u7ec8\u7684\u4e0a\u4f20\n    return MultipartBody.Part.createFormData(partName, file.getName(), requestFile);\n}\n\npublic void uploadFile(View view) {\n    String filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + &quot;generated.apk&quot;;\n\n    Retrofit retrofit = new Retrofit.Builder()\n            .baseUrl(SERVER_ADDRESS)\n            .build();\n\n    api = retrofit.create(Api.class);\n\n    \/\/ \u6dfb\u52a0Form\u7684part\n    RequestBody description = prepareFromPart(&quot;hello, this is description speaking&quot;);\n\n    \/\/ \u521b\u5efa\u6587\u4ef6\u7684part (photo, video, ...)\n    MultipartBody.Part fileBody = prepareFilePart(&quot;file&quot;, filePath);\n\n    api.uploadFile(description, fileBody).enqueue(new Callback&lt;ResponseBody&gt;() {\n        @Override\n        public void onResponse(Call&lt;ResponseBody&gt; call, Response&lt;ResponseBody&gt; response) {\n            \/\/\u5728UI\u4e3b\u7ebf\u7a0b\u8fd0\u884c\n            if (response.isSuccessful()) {\n                Log.i(TAG, &quot;\u4e0a\u4f20\u6210\u529f&quot;);\n            }\n        }\n\n        @Override\n        public void onFailure(Call&lt;ResponseBody&gt; call, Throwable t) {\n            Log.i(TAG, &quot;\u4e0a\u4f20\u5931\u8d25: &quot; + t.getLocalizedMessage());\n        }\n    });\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Retrofit2 \u4e0a\u4f20\u9700\u8981\u7528\u5230 Multipart\uff0c\u4f7f\u7528 MultipartBody.Part \u6807\u8bc6\u6587\u4ef6\u4f53\u3002 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[130],"tags":[],"class_list":["post-384","post","type-post","status-publish","format-standard","hentry","category-retrofit"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/384","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=384"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/384\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=384"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=384"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=384"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}