Vitalii Shkurenko
Vitalii Shkurenko

Reputation: 69

Upload MultiPart file from Android to server(SpringMVC)

If, I'm using Postmam file upload without problems. But, when anroid tries upload file in server, method throws NPE, becouse MultipartFile file = null.

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);

Server

@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>

Upvotes: 3

Views: 2803

Answers (2)

user5488709
user5488709

Reputation:

It's difficult to do it on android using HTTP, there are some third party libraries you can use that.

  1. ion
  2. retrofit

Both are good libraries, I have used both in my projects.

Upvotes: 1

vikram thakur
vikram thakur

Reputation: 27

You can use Retrofit network library , it is the fastest one available

It supports multipart files and its implementation is very simple

http://square.github.io/retrofit/

Upvotes: 1

Related Questions