我在spring boot中有这个端点。
@PostMapping("/uploadPhoto")
public String uploadPhoto(@RequestParam String email, @RequestPart(name = "img") MultipartFile file) {
studentService.updateImageLink(email, file);
return "OK";
}
和服务端点在改造
@POST("uploadPhoto")
@Multipart
suspend fun uploadPhoto(@Part("email") email: String, @Part img: MultipartBody.Part): String
我是这样发送我的文件的
val file = File(getPath(selectedImage!!));
val requestBody = RequestBody.create(MediaType.parse("*/*"), file);
val fileToUpload = MultipartBody.Part.createFormData("img", file.name, requestBody);
viewModel.uploadPhoto("bekjan.omirzak98@gmail.com", fileToUpload);
我正在翻新。HttpException: HTTP 400。这个问题是来自服务器端还是客户端?
您应该将原始类型参数作为RequestBody
而不是String发送。
@Multipart
@POST("uploadPhoto")
Call<Void> uploadPhoto(@Part("email") RequestBody email,
@Part MultipartBody.Part image);
,在你的请求方法中,你应该像这样插入你的参数:
public void uploadProfileImage(String email, File imageFile) {
RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), imageFile);
MultipartBody.Part imageBody = MultipartBody.Part.createFormData("image", imageFile.getName(), requestFile);
RequestBody emailBody = RequestBody.create(MediaType.parse("multipart/form-data"), email);
getRetrofitClient().uploadPhoto(emailBody, imageBody);
}