Multipart form request throws: Required MultipartFile parameter 'image' is not present
我尝试使用邮递员将图像上载到服务器。我使用Spring使其余的API如下所示:
。
1 2 3 4 | @RequestMapping(value ="/uploadPrescription", method =RequestMethod.POST) public ResponseEntity<ResponseSuccessData> uploadPatientPrescription( @RequestBody @RequestParam("image") MultipartFile image) throws IOException { |
但它抛出了一个错误:
1 2 | org.springframework.web.bind.MissingServletRequestParameterException: Required MultipartFile parameter 'image' is not present |
号
正如您在邮差中看到的,密钥名是"image",而在RESTAPI中,密钥名也是@requestparam("image")。
在content-type-content-type=multipart/form数据中设置值,bounds=--abc'
这是我的multipart的Spring配置-
1 2 3 4 5 6 7 8 | @Bean public CommonsMultipartResolver multipartResolver() { CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(); //commonsMultipartResolver.setMaxUploadSize(-1); return commonsMultipartResolver; } |
有什么问题?
请删除标题部分
氧化镁
请删除邮差标题部分的-content-type:multipart/form-data;boundary='abc'设置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | @RestController public class UserOfferController { // upload image @RequestMapping(value ="/uploadimage", method = RequestMethod.POST) public ResponseEntity<ResponseObjectBean> uploadFile(@RequestParam("uploadedFile") MultipartFile file) { int statusCode; String msg; Object data = null; long maxsize = configuredValue.getFileMaxAcceptedSize(); if (!file.isEmpty()) { String name = file.getOriginalFilename(); String imagePath ="path to save your image"; try { byte[] bytes = file.getBytes(); BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(imagePath))); stream.write(bytes); statusCode = 200; msg ="DONE"; data = true; } catch (Exception e) { e.printStackTrace(); statusCode = 500; msg ="FAIL"; data = false; } } else { statusCode = 500; msg ="FAIL"; data = false; } responseData.setStatusCode(statusCode); responseData.setStatusMsg(msg); responseData.setData(data); return new ResponseEntity<ResponseObjectBean>(responseData, HttpStatus.OK); } } |
在spring.xml中添加这些行
1 2 3 4 5 6 7 8 | <!-- mutipart upload configuration --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- max upload size in bytes --> <property name="maxUploadSize" value="1024" /> <!-- max size of file in memory (in bytes) --> <property name="maxInMemorySize" value="2048" /> </bean> |
号
。
除了Abhijit Chowdhury的答案外,如果您仍在使用Spring Security,那么您可以删除
此外,重新启动邮递员也很重要。
1.删除邮差中的标题部分。
2.在您的API中:
1 2 3 4 | @RequestMapping(value ="/uploadPrescription", method =RequestMethod.POST) public ResponseEntity<ResponseSuccessData> uploadPatientPrescription( @RequestBody @RequestParam("image") MultipartFile image) throws IOException {} |
添加以下内容:
1 | consumes = MediaType.MULTIPART_FORM_DATA_VALUE |
。
所以它变成:
1 2 3 4 | @RequestMapping(value ="/uploadPrescription", method =RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public ResponseEntity<ResponseSuccessData> uploadPatientPrescription( @RequestBody @RequestParam("image") MultipartFile image) throws IOException {} |
将