Spring SpringMVC文件上傳錯誤(二)

續上文,


場景:

不上傳文件提交表單。


簡述一下:表單請求的兩種方式

  1. 同步提交
  2. 異步提交

筆者之前寫過的測試Demo:

HTML:

<form action="upload.do" method="post" enctype="multipart/form-data">  
    <input type="file" name="fileName" />
    <input type="submit" value="Submit" />
</form>  

控制器:

@RequestMapping(value = "/upload.do", method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> add(@RequestParam(value="fileName") MultipartFile file){
    //...
    return null;
}

錯誤信息:

11:03:07.147 [http-nio-8080-exec-4] DEBUG o.s.w.s.m.a.AnnotationMethodHandlerExceptionResolver - Resolving exception from handler [com.xFee.serverAdmin.controller.ApkInfoController@25a5db]: org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'java.lang.String' to required type 'org.springframework.web.multipart.MultipartFile'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.springframework.web.multipart.MultipartFile]: no matching editors or conversion strategy found
11:03:07.147 [http-nio-8080-exec-4] DEBUG o.s.w.s.m.a.ResponseStatusExceptionResolver - Resolving exception from handler [com.xFee.serverAdmin.controller.ApkInfoController@25a5db]: org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'java.lang.String' to required type 'org.springframework.web.multipart.MultipartFile'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.springframework.web.multipart.MultipartFile]: no matching editors or conversion strategy found
11:03:07.147 [http-nio-8080-exec-4] DEBUG o.s.w.s.m.s.DefaultHandlerExceptionResolver - Resolving exception from handler [com.xFee.serverAdmin.controller.ApkInfoController@25a5db]: org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'java.lang.String' to required type 'org.springframework.web.multipart.MultipartFile'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.springframework.web.multipart.MultipartFile]: no matching editors or conversion strategy found
11:03:07.147 [http-nio-8080-exec-4] DEBUG o.s.web.servlet.DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'springServlet': assuming HandlerAdapter completed request handling
11:03:07.147 [http-nio-8080-exec-4] DEBUG o.s.web.servlet.DispatcherServlet - Successfully completed request

筆者忽略了,在同步提交的時候,Spring把空上傳是做null來處理的,而在異步提交的時候,Spring把文件域的值當作空字符串看待的。在Spring做請求轉換的時候(request–>MultipartHttpServletRequest)底層的TypeConverter接口實現對null""是做了不同操作的。源碼就不貼了,有興趣的夥伴可以自行從:org.springframework.web.multipart.commons.CommonsMultipartResolver
這個類跟下去,就會發現差異。

解決方案

在配置文件中加入以下配置:

    <bean id="conversionService"
        class="org.springframework.context.support.ConversionServiceFactoryBean">
        <property name="converters">
            <list>
                <bean class="com.xFee.serverAdmin.utils.SpringMVCFileConverter"></bean>
            </list>
        </property>
    </bean>
    <mvc:annotation-driven conversion-service="conversionService" />

然後在list下的bean節點的class屬性的路徑下創建一個SpringMVCFileConverter

public class SpringMVCFileConverter implements Converter<String, MultipartFile>  {

    public SpringMVCFileConverter() {
        super();
    }

    @Override
    public MultipartFile convert(String source) {
        return null;
    }
}

注:注意在引入上面配置文件的時候不要忘記引入其定義及約束文件。

學生淺薄,望衆師指點

wengang.liu 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章