【发布时间】:2018-08-07 12:48:50
【问题描述】:
我是 Spring Boot 和 Android 开发的新手。我正在尝试设置 android 应用程序(使用改造和 okhttp3),它将图像上传到我的服务器。
服务器代码:
@RestController
public class ImageController {
@RequestMapping(value = "/uploadImage", method =RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public String greeting(@RequestPart(value="desc") RequestBody desc, @RequestPart(value="img") MultipartBody.Part image) {
System.out.println("Works");
return "Works";
}
}
安卓代码:
MultipartBody.Part fileToUpload = MultipartBody.Part.createFormData("file", imageFile.getName(), requestBody);
RequestBody name = RequestBody.create(MediaType.parse("text/plain"), imageFile.getName());
@Multipart
@POST("/uploadImage")
Observable<retrofit2.Response<String>> uploadPhoto(@Part("desc") RequestBody desc,@Part MultipartBody.Part image);
错误:
{"timestamp":1519756785858,"status":415,"error":"Unsupported Media Type","exception":"org.springframework.web.HttpMediaTypeNotSupportedException","message":"Content type 'text/ plain;charset=utf-8' 不支持","path":"/uploadImage"}
谁能告诉我我做错了什么????
【问题讨论】:
-
欢迎来到 Stackoverflow。下次请正确格式化您的代码,以便其他人更好地阅读。这一次我为你做到了。
标签: java android spring-boot retrofit2 okhttp3