问题
I try to upload an image to a server using the postman. I am using spring to make the rest api as the followings:
@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").
Setting value in content type - 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?
回答1:
Please remove the header section
Please remove the - Content-Type : multipart/form-data;boundary='abc' setting in the header part of postman
回答2:
@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
<!-- 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>
回答3:
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.
Also, it is important to restart postman.
回答4:
1.Remove header section in POSTMAN.
2.In your 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 {}
回答5:
Replace @RequestBody @RequestParam("image") to just @RequestBody("image").
The first statement is not valid, see - Spring uploading files.
来源:https://stackoverflow.com/questions/39022087/multipart-form-request-throws-required-multipartfile-parameter-image-is-not-p