2

Need a spring rest service which needs to take input as JSON content and Multiple mutipart files.

1 Answer 1

2

Below is the approach I followed to handle JSON content + multiple multiparts in spring MVC controller

BackEnd Implementation :

@RequestMapping(value = "/upload", method = RequestMethod.POST, consumes =    {"multipart/form-data"})
public
@ResponseBody
List<String> handleFileUpload(MultipartHttpServletRequest multipartHttpServletRequest) {
InputStream jsonSteam = multipartHttpServletRequest.getFile("json").getInputStream();
InputStream fileStream1 = multipartHttpServletRequest.getFile("file1").getInputStream();
InputStream fileStream2 = multipartHttpServletRequest.getFile("file2").getInputStream();
}

Front Implementation :

Request payload :

------WebKitFormBoundaryhKn3wrSAw57pRAso
Content-Disposition: form-data; name="file1"; filename="deleme_bkup.sql"
Content-Type: text/x-sql


------WebKitFormBoundaryhKn3wrSAw57pRAso
Content-Disposition: form-data; name="file2"; filename="source.sql"
Content-Type: text/x-sql


------WebKitFormBoundaryhKn3wrSAw57pRAso
Content-Disposition: form-data; name="json"; filename="blob"
Content-Type: application/json


------WebKitFormBoundaryhKn3wrSAw57pRAso--
Response Headersview source

RequestHeaders :

Accept:application/json, text/plain, */*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:no-cache
Connection:keep-alive
Content-Length:5533
Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryhKn3wrSAw57pRAso
Host:localhost:8080
Origin:http://localhost:8080
Pragma:no-cache
Sign up to request clarification or add additional context in comments.

1 Comment

just add more example. String myParam = multipartHttpServletRequest.getParameter("myParam");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.