在文件上载期间获取MissingServletRequestParameterException

时间:2017-07-10 09:57:34

标签: spring file multipart

我的控制器看起来像这样

@RequestMapping (value = { "/link_multiple" } , method = 
RequestMethod.POST)
public ModelAndView linkMultipleVideo(
    @RequestParam ("csv_file") final MultipartFile file) {

这是我的freemarker模板

<input type="file" class="csv_file" name="csv_file"  id="csv_file" accept=".csv"/>
    <a class="file-link" href="javascript:void(0);">Choose File</a>
    <span class='input-file-select'></span>
  </div>

我还添加了Multipart Resolver配置

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

还在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>/*</url-pattern>
</filter-mapping>

以下是我得到的例外情况。

org.springframework.web.bind.MissingServletRequestParameterException: Required MultipartFile parameter 'csv_file' is not present
[tomcat:launch]     at org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.handleMissingValue(RequestParamMethodArgumentResolver.java:255)
[tomcat:launch]     at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:95)
[tomcat:launch]     at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:79)
[tomcat:launch]     at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:157)
[tomcat:launch]     at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:124)
[tomcat:launch]     at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)

1 个答案:

答案 0 :(得分:0)

我通过从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>/*</url-pattern>
</filter-mapping>