I am trying to write a spring mvc method that can receive either a multipart/form or transfer-encoding chunked file upload. I can write a separate method to handle each type but I'd like to do it with the same method so i can use the same REST POST uri such as:
http://host:8084/attachments/testupload
Here is my best attempt so far:
@RequestMapping(value = { "/testupload" }, method = RequestMethod.POST, produces =
"application/json")
public @ResponseBody
ResponseEntity<MessageResponseModel> testUpload(
@RequestParam(value = "filedata", required = false) MultipartFile filedata,
final HttpServletRequest request) throws IOException {
InputStream is = null;
if (filedata == null) {
is = request.getInputStream();
}
else {
is = filedata.getInputStream();
}
byte[] bytes = IOUtils.toByteArray(is);
System.out.println("read " + bytes.length + " bytes.");
return new ResponseEntity<MessageResponseModel>(null, null, HttpStatus.OK);
}
Using the above method I can upload a multipart file, but if i upload a chunked file I get an exception from spring that says:
org.springframework.web.multipart.MultipartException: \
The current request is not a multipart request
If I remove the MultipartFile request param it works great for transfer encoding chunked. If I leave it in it works great for MultipartFile uploads. How can I do it with the same method to handle both upload types?
This works fine for chunked:
@RequestMapping(value = { "/testupload" }, method = RequestMethod.POST, produces =
"application/json")
public @ResponseBody
ResponseEntity<MessageResponseModel> testUpload(
final HttpServletRequest request) throws IOException {
InputStream is = null;
is = request.getInputStream();
byte[] bytes = IOUtils.toByteArray(is);
System.out.println("read " + bytes.length + " bytes.");
return new ResponseEntity<MessageResponseModel>(null, null, HttpStatus.OK);
}
and this works great for MultipartFile:
@RequestMapping(value = { "/testupload" }, method = RequestMethod.POST, produces =
"application/json")
public @ResponseBody
ResponseEntity<MessageResponseModel> testUpload(
@RequestParam MultipartFile filedata) throws IOException {
InputStream is = null;
is = filedata.getInputStream();
byte[] bytes = IOUtils.toByteArray(is);
System.out.println("read " + bytes.length + " bytes.");
return new ResponseEntity<MessageResponseModel>(null, null, HttpStatus.OK);
}
It should be possible, does anybody know how to do this?
Thank you, Steve