長話短說,不深刻解釋了,官方給的上傳案例代碼:html
- private static final String IMGUR_CLIENT_ID = "...";
- private static final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");
-
- private final OkHttpClient client = new OkHttpClient();
-
- public void run() throws Exception {
-
- RequestBody requestBody = new MultipartBuilder()
- .type(MultipartBuilder.FORM)
- .addPart(
- Headers.of("Content-Disposition", "form-data; name=\"title\""),
- RequestBody.create(null, "Square Logo"))
- .addPart(
- Headers.of("Content-Disposition", "form-data; name=\"image\""),
- RequestBody.create(MEDIA_TYPE_PNG, new File("website/static/logo-square.png")))
- .build();
-
- Request request = new Request.Builder()
- .header("Authorization", "Client-ID " + IMGUR_CLIENT_ID)
- .url("https://api.imgur.com/3/image")
- .post(requestBody)
- .build();
-
- Response response = client.newCall(request).execute();
- if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
-
- System.out.println(response.body().string());
- }
上面這個代碼上傳文件在Java服務器端用spring boot的 MultiPart解析不到上傳的文件,用Python Django,問題亦然。java
找到了一個泡網同窗的代碼:python
作了改動1:android
- RequestBody requestBody = new MultipartBuilder()
- .type(MultipartBuilder.FORM)
- .addPart(
- Headers.of("Content-Disposition", "form-data; name=\"UserId\""),
- RequestBody.create(null, UserId))
- .addPart(
- Headers.of("Content-Disposition", "form-data; name=\"SecurityCode\""),
- RequestBody.create(null, SecurityCode))
- .addPart(
- Headers.of("Content-Disposition", "form-data; name=\"File\"; filename =\"avatar_upload.jpg\""),
- RequestBody.create(MediaType.parse("image/jpeg"), file))
-
-
- .build();
改成以上代碼後,Django請求解析能夠找到文件了,Spring boot的multipart任然找不到文件信息。git
改動1的改動點是:文件的Header信息裏面必須有上傳文件的名字,固然這個名字實際上是隨便起的,通常後綴保持正確便可。github
這個時候不得再也不參考泡網同窗的代碼,作了修改2:web
- RequestBody requestBody = new MultipartBuilder()
- .type(MultipartBuilder.FORM)
- .addPart(
- Headers.of("Content-Disposition", "form-data; name=\"UserId\""),
- RequestBody.create(null, UserId))
- .addPart(
- Headers.of("Content-Disposition", "form-data; name=\"SecurityCode\""),
- RequestBody.create(null, SecurityCode))
- .addPart(
- Headers.of("Content-Disposition", "form-data; name=\"File\"; filename =\"avatar_upload.jpg\""),
- RequestBody.create(MediaType.parse("application/octet-stream"), file))
-
- .build();
這回Spring Boot multipart 依舊找不到文件信息了,這回的改動點是把mime類型由圖片類型"image/jpeg"改爲了附件類型:spring
- "application/octet-stream"
最後借鑑StackOverFlow的某貼的代碼:http://stackoverflow.com/questions/30707681/multipart-file-upload-with-okhttp-spring,api
- RequestBody body = new MultipartBuilder()
- .addFormDataPart("qqfile", filename, RequestBody.create(MediaType.parse("media/type"), new File(filename)))
- .type(MultipartBuilder.FORM)
- .build();
-
- Request request = new Request.Builder()
- .url("/path/to/your/upload")
- .post(body)
- .build();
-
- OkHttpClient client = new OkHttpClient();
- Response response = client.newCall(request).execute();
由於這個帖子的topic就是解決Spring MultiPart的問題,最後終於能夠上傳了,修改點在於完全修改了添加文件使用的函數接口,我在build gradle裏面已經準備用Apache HttpClient了。服務器
OkHttp有點坑爹啊。
參考:
https://github.com/square/okhttp/wiki/Recipes
http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0824/3355.html