我正在尝试编写一个spring mvc方法,它可以接收multipart/form或transfer-encoding chunked文件上传.我可以编写一个单独的方法来处理每种类型,但我想用相同的方法来做,所以我可以使用相同的REST POST uri,例如:
http://host:8084/attachments/testupload
Run Code Online (Sandbox Code Playgroud)
到目前为止,这是我最好的尝试:
@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);
}
Run Code Online (Sandbox Code Playgroud)
使用上面的方法我可以上传一个多部分文件,但是如果我上传一个分块文件,我会从spring获得一个例外:
org.springframework.web.multipart.MultipartException: \
The current request is not …
Run Code Online (Sandbox Code Playgroud)