简体   繁体   中英

Spring MVC MultipartFile problems

I am trying to run a simple controller with a method that will receive

  • A String with a value
  • A file (MultipartFile object)

After some investigation ( Sending Multipart File as POST parameters with RestTemplate requests ) I ended up adding into my web.xml

<filter>
    <filter-name>multipartFilter</filter-name>
    <filter-class>org.springframework.web.multipart.support.MultipartFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>multipartFilter</filter-name>
    <url-pattern>/REST/*</url-pattern>
</filter-mapping>

My application context file

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize">
        <value>104857600</value>
    </property>
    <property name="maxInMemorySize">
        <value>4096</value>
    </property>
</bean>

The controller looks like

@Controller
@RequestMapping("/image")
public class ImageController  extends RestApiController {

private static final Logger log = LoggerFactory.getLogger(ImageController.class);

@RequestMapping(value="/simple", method = RequestMethod.POST, consumes="multipart/form-data")
public @ResponseBody boolean save(
        @RequestParam(value = "file", required = false) MultipartFile file,
        @RequestParam(value = "name", required = false) String name) {

        //Some code here
    return true;
}

So far I have been able to run unit tests against the controller with no problem, but I seem to be stuck when it comes to create a real http request.

I have tried with POSTMAN, but after some investigation seems that it does not set the multipart/form-data header properly, I have tried removing it, and the problem persists.

I have also tried with CURL

curl http://127.0.0.1:8080/content/REST/image/simple -F "file=@/home/jmoriano/Pictures/simple.jpeg" -F "name=someName" -v 

I have also tried with a RestTemplate object

public Boolean update() {
    RestTemplate restTemplate = new RestTemplate();
    try {
        FormHttpMessageConverter formConverter = new FormHttpMessageConverter();
        formConverter.setCharset(Charset.forName("UTF8"));
        restTemplate.getMessageConverters().add(formConverter);
        restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
        HttpHeaders httpHeaders = new HttpHeaders();         httpHeaders.setAccept(Collections.singletonList(MediaType.parseMediaType("application/json")));
        MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
        parts.add("file", new FileSystemResource("/home/jmoriano/Pictures/simple.jpeg"));
        parts.add("name", "name");
        return restTemplate.postForObject("http://127.0.0.1:8080/content/REST/image/simple", parts, Boolean.class);
    } catch (Exception e) {
        e.printStackTrace();
        log.error("Ouch!", e);
    }
    return false;
}

Just to be clear, the problem is not in the "name" parameter, that one works fine, however the MultipartFile is empty.

By debugging the code, I managed to check the MultiPartFilter class which receives an HttpServletRequest object, such object has its "parts" attribute already empty there. So the problem seems to be related with the way I am making the request... seems that my postman/curl/java attempts have failed... do you see anything incorrect in my config?

I think you should put the bean multipartResolver to your dispathcher-servlet.xml

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />

in the link you show above they use this bean, too.

<bean id="multipartResolver"  
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
    <property name="maxUploadSize">  
        <value>104857600</value>  
    </property>  
    <property name="maxInMemorySize">  
        <value>4096</value>  
    </property>      
</bean>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM