1. 程式人生 > 其它 >spring mvc環境之上傳檔案設定(五)

spring mvc環境之上傳檔案設定(五)

spring mvc環境之上傳檔案設定

1.匯入pom.xml依賴

2.spring-mvc.xml配置bean

3.測試

1.匯入必要的依賴

    <!-- 上傳元件包 -->
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.3.3</version>
    </dependency>

2.在spring-mvc.xml檔案配置上傳的bean

  (還可以設定更多的引數...)

    <bean id="multipartResolver"
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 設定上傳檔案的最大尺寸為5MB -->
        <property name="maxUploadSize" value="5242880"/>
    </bean>

3.在控制器試試

    @RequestMapping("index06")
    public String index06(@RequestParam(name="file")MultipartFile file, Model model, HttpServletRequest request){

        System.out.println(file.toString());
        model.addAttribute("index02","index02");
        return "index01";
    }
<form action="./index/index06"
method="post" enctype="multipart/form-data" > <input type="file" name="file" /> <input type="submit" value="submit" /> </form>