【问题标题】:Spring MVC MultipartFile problemsSpring MVC MultipartFile 问题
【发布时间】:2014-12-11 07:09:29
【问题描述】:

我正在尝试使用将接收的方法运行一个简单的控制器

  • 带值的字符串
  • 一个文件(MultipartFile 对象)

经过一番调查 (Sending Multipart File as POST parameters with RestTemplate requests) 我最终添加到我的 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>

我的应用程序上下文文件

<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>

控制器的样子

@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;
}

到目前为止,我已经能够毫无问题地对控制器运行单元测试,但是在创建真正的 http 请求时我似乎被卡住了。

我曾尝试使用 POSTMAN,但经过一番调查,似乎它没有正确设置 multipart/form-data 标头,我尝试将其删除,但问题仍然存在。

我也尝试过使用 CURL

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

我也尝试过使用 RestTemplate 对象

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;
}

需要明确的是,问题不在于“名称”参数,它可以正常工作,但是 MultipartFile 是空的。

通过调试代码,我设法检查了接收 HttpServletRequest 对象的 MultiPartFilter 类,该对象的“parts”属性在那里已经为空。所以问题似乎与我提出请求的方式有关...似乎我的邮递员/curl/java 尝试失败了...您在我的配置中看到任何不正确的地方吗?

【问题讨论】:

  • 只是一个快速更新,问题与我的配置无关,这很好,但事实上 webapp 使用的是 Struts2 和 SpringMVC,不幸的是,多部分请求是由 Struts2 过滤器处理的,所以我需要创建一个自定义过滤器以排除 Struts2 的过滤器处理某些 url(由 Spring MVC 提供服务的那些)

标签: java spring spring-mvc multipartform-data


【解决方案1】:

我认为您应该将 bean multipartResolver 放入您的 dispathcher-servlet.xml

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

在您上面显示的链接中,他们也使用了这个 bean。

<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>

【讨论】:

  • 谢谢,不幸的是我已经在我的代码中加入了它(忘记在问题中提及它......)还有其他想法吗?
猜你喜欢
  • 2013-06-09
  • 1970-01-01
  • 1970-01-01
  • 2011-11-06
  • 2015-09-02
  • 1970-01-01
  • 2021-03-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多