2015-10-26 58 views
2

如果我正在使用Postmam文件上傳沒有問題。但是,當anroid嘗試在服務器上傳文件時,方法會拋出NPE,因爲MultipartFile file = null。將Android上的MultiPart文件上傳到服務器(SpringMVC)

的Android

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> 

回答

1

這是很難做到的它在android上使用HTTP,有一些第三方庫可以使用它。

  1. ion
  2. enter link description here

兩個都是很好的圖書館,我在項目中使用這兩種。

+0

我不知道這個答案是如何被接受;這根本不能回答這個問題。 – forresthopkinsa