Vishwas Nagaraja opened SPR-8001 and commented
Basically I am trying to upload a file using spring mvc... but I get a missing parameter exception. [org.springframework.web.bind.MissingServletRequestParameterException: Required MultipartFile parameter 'file' is not present]
On the client side:
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
Resource resource = new ClassPathResource("/appctx/applicationContext.xml");
parts.add("file", resource);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(new MediaType("multipart", "form-data"));
HttpEntity request = new HttpEntity(parts, headers);
ResponseEntity<?> httpResponse = cliRestTemplate.exchange(fullUri("/file/upload"), HttpMethod.POST, request, null);
On the server side:
@RequestMapping(value = "/file/upload", method = RequestMethod.POST)
@ResponseBody
public void upload(@RequestParam(value="file") MultipartFile file, HttpServletResponse response) throws Exception {
String fileName = file.getOriginalFilename();
logger.info("Received file to upload. fileName='{}'", fileName);
String resultUrl = getUrlForContent(fileName, file.getInputStream());
response.setStatus(200);
response.setContentType("text/text");
response.setContentLength(resultUrl.length());
response.getWriter().print(resultUrl);
}
The way to fix this is to define a multipartresolver like this.
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="100000000"/>
</bean>
Since this configuration is is required, it would be great if spring threw an exception if it didn't find the multipart resolver.
Couple of suggestions include: a) Adding an annotation at the controller level that programmers can use to indicate that it can handle multipart request which can trigger the validation. b) Searching for the presence of a MultipartFile type as a parameter to the trigger the validation
Affects: 3.0.5
Referenced from: commits https://github.com/spring-projects/spring-framework/commit/2568a3c2c6af84668f9a4039baf4ba4998a3c54b
1 votes, 2 watchers
Comment From: spring-projects-issues
Mike Whittemore commented
Agreed. I actually had defined the CommonsMultipartResolver without an id like so:
\
Comment From: spring-projects-issues
Rossen Stoyanchev commented
If the method argument is of type MultipartFile and the request is not a MultipartRequest, an IllegalStateException is raised with the more helpful message:
java.lang.IllegalStateException: Current request is not of type [org.springframework.web.multipart.MultipartRequest]: ServletWebRequest: uri=;client=127.0.0.1.
Do you have a MultipartResolver configured?
Comment From: ashok2ashok
The RequestParam value cannot be the same as the parameter field name in the method. But the RequestParam value should match the requested name
These didn't work.
@RequestParam(value = "files") MultipartFile[] files
@RequestPart(value = "files") MultipartFile[] files
But the below options worked.
@RequestParam(value = "files") MultipartFile[] uploadedFiles
@RequestPart(value = "files") MultipartFile[] uploadedFiles