如果我使用Postman文件上传没有问题。但是,当安卓尝试上传文件到服务器时,该方法会抛出NPE异常,因为MultipartFile文件为空。
安卓
RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), file);
Call<MediaResponseModel> call = service.uploadImage(requestBody);
call.enqueue(new RetrofitCallback<>(callback));
@POST("/uploadImage")
Call<MediaResponseModel> uploadImage(@Part("file") RequestBody file);
服务器
@RequestMapping(value = "/uploadImage", method = RequestMethod.POST)
@ResponseBody
public ResponseEntity<?> imageUpload(@RequestHeader(value = "id") String sessionId,
@RequestParam (value = "file") MultipartFile file)
ApplicationContext
<bean id="jsonConverter"
class="org.springframework.http.converter.json.GsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
<mvc:annotation-driven >
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.GsonHttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>