1. 程式人生 > >關於SpringMvc中檔案上傳相關

關於SpringMvc中檔案上傳相關

2)StandardServletMultipartResolver:依賴於Servlet3.0對multipart請求的支援

一般來說,在這兩者之間,StandardServletMultipartResolver可能回事優選的方案。它使用Servlet所提供的功能支援,並不需要依賴其他專案。但那是如果想要部署在Servlet3.0之前的容器內,那麼可能就需要CommonsMultipartResolver了

3、接收multipartFile

1)使用Spring提供的MultipartFile介面

2)可以在Servlet3.0的容器中使用Part介面

Xml中的配置:
<!--defaultEncoding請求編碼,需與檔案編碼一致;maxUploadSize上傳大小限制 預設為1位元組Byte ,uploadTempDir上傳檔案時,檔案臨時儲存的目錄,傳完自動刪除,不存在該目錄會自動建立,不指定會使用預設 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="UTF-8"/>
        <property name="maxUploadSize" value="52428800"/>
  </bean>
  
  介面中的程式碼:
  
   @RequestMapping(value = "/uploadfile", method = RequestMethod.POST)
    @ResponseBody
    public AjaxModel uploadFileHandler(@RequestParam("file") MultipartFile file, @RequestParam("imageurl") String imageUrl) {
        LOGGER.info("===>>>file :{}", file);
        if (file != null) {
            LOGGER.info("=====>>>file的大小:{},fileName:{}", file.getSize(), file.getName());