[英]Multipart form request throws: Required MultipartFile parameter 'image' is not present
I try to upload an image to a server using the postman. 我尝试使用邮递员将图像上传到服务器。 I am using spring to make the rest api as the followings:
我正在使用spring使其余api如下:
@RequestMapping(value = "/uploadPrescription", method =RequestMethod.POST)
public ResponseEntity<ResponseSuccessData> uploadPatientPrescription(
@RequestBody @RequestParam("image") MultipartFile image)
throws IOException {
But it throws an error: 但这会引发错误:
org.springframework.web.bind.MissingServletRequestParameterException:
Required MultipartFile parameter 'image' is not present
As you can see in the postman that key name is 'image' and in rest api is also @RequestParam("image"). 正如您在邮递员中看到的那样,键名是“ image”,其余api中的键名也是@RequestParam(“ image”)。
Setting value in content type - Content-type = multipart/form-data,boundaries='--abc' 在内容类型中设置值-Content-type = multipart / form-data,boundaries ='-abc'
This is my spring config for multipart - 这是我的多部分春季配置-
@Bean
public CommonsMultipartResolver multipartResolver() {
CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
//commonsMultipartResolver.setMaxUploadSize(-1);
return commonsMultipartResolver;
}
What could be the problem? 可能是什么问题呢?
@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);
}
}
Add these line in spring.xml 在spring.xml中添加这些行
<!-- 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>
In addition to Abhijit Chowdhury answer, if you are using spring security still you can remove Content-Type
and just keep your token in the header, no need to remove everything from the header. 除了Abhijit Chowdhury的答案,如果您使用的是Spring Security,您仍然可以删除
Content-Type
并将令牌保留在标题中,而无需从标题中删除所有内容。
Also, it is important to restart postman. 另外,重新启动邮递员也很重要。
1.Remove header section in POSTMAN. 1.删除POSTMAN中的标题部分。
2.In your API: 2.在您的API中:
@RequestMapping(value = "/uploadPrescription", method =RequestMethod.POST)
public ResponseEntity<ResponseSuccessData> uploadPatientPrescription(
@RequestBody @RequestParam("image") MultipartFile image)
throws IOException {}
add the following: 添加以下内容:
consumes = MediaType.MULTIPART_FORM_DATA_VALUE
so it becomes: 变成:
@RequestMapping(value = "/uploadPrescription", method =RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<ResponseSuccessData> uploadPatientPrescription(
@RequestBody @RequestParam("image") MultipartFile image)
throws IOException {}
Replace @RequestBody @RequestParam("image")
to just @RequestBody("image")
. 将
@RequestBody @RequestParam("image")
替换为@RequestBody("image")
。 The first statement is not valid, see - Spring uploading files . 第一条语句无效,请参阅-Spring上载文件 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.