Spring 3.0文件上传

时间:2012-12-12 15:53:42

标签: spring servlets

有人可以在提交表单后发现为什么文件(Http.Part)变量为空?

控制器代码

@RequestMapping(value="/account/update", method=RequestMethod.POST)
public String addImage(Account account, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest, RedirectAttributes redirectAttributes, 
        Locale locale,HttpServletRequest request, @RequestParam(value="file", required=false) Part file){

        if(file==null){
            uiModel.addAttribute("file_error", "File upload failure");

        }

查看

 <spring:url value="/account/update" var="update" />
<form:form modelAttribute="account" action="${update}" method="POST" enctype="multipart/form-data"  >
<c:if test="${file_error != null}">
Errors: ${file_error }
</c:if><br />
<label for="file">
    <spring:message code="account.upload.file"/>
</label>
        <input name="file" type="file"/>
        <input type="submit" value="Save" /> 
</form:form>

1 个答案:

答案 0 :(得分:0)

您是否添加了multipartResolver

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