1. 程式人生 > 實用技巧 >SpringMVC - 04上傳檔案

SpringMVC - 04上傳檔案

SpringMVC - 04上傳檔案

(1)檔案上傳客戶端三要素

  • 表單項 type = "file"
  • 表單的提交方式是post
  • 表單的enctype屬性是多部分表單形式,及enctype = "multipart/form-data"
    <form action="${pageContext.request.contextPath}/user/quick22" method="post" enctype="multipart/form-data">
        名稱<input type="text" name="username"><br/>
        檔案
<input type="file" name="uploadFile"><br/> <input type="submit" value="提交"> </form>

(2)檔案上傳原理

> 當form表單修改為多部分表單時,request.getParameter()將失效。

> enctype="application/x-www-form-urlencoded"時,form表單的正文內容格式是:key=value&key=value&key=value。

> 當form表單的enctype取值為Multipart/form-data時,請求正文內容就變成多部分形式。

(3)檔案上傳

(3.1)單檔案上傳

> 匯入fileupload和io座標

        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.3</version>
        </dependency>
        <dependency>
<groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.6</version> </dependency>

>spring-mvc.xml配置檔案上傳解析器

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="UTF-8"/>           // 上傳檔案的編碼型別
        <property name="maxUploadSize" value="5000000"/>           // 上傳檔案總大小
        <property name="maxUploadSizePerFile" value="500000"/>     // 上傳單個檔案的大小
    </bean>

>檔案上傳程式碼

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Upload上傳檔案</title>
</head>
<body>
    <form action="${pageContext.request.contextPath}/user/quick22" method="post" enctype="multipart/form-data">
        名稱<input type="text" name="username"><br/>
        檔案<input type="file" name="uploadFile"><br/>
        <input type="submit" value="提交">
    </form>
</body>
</html>

    @RequestMapping("/quick22")
    @ResponseBody
    public void save22(String username, MultipartFile uploadFile) throws IOException {
        System.out.println(username);
        String originalFilename = uploadFile.getOriginalFilename();
        uploadFile.transferTo(new File("E:\\java\\" + originalFilename));
    }

> 匯入包故障

Caused by: java.lang.ClassNotFoundException: org.apache.commons.fileupload.FileItemFactory ;未正常匯入jar包

(3.2)多檔案上傳,硬編碼

    <form action="${pageContext.request.contextPath}/user/quick23" method="post" enctype="multipart/form-data">
        名稱<input type="text" name="username"><br/>
        檔案<input type="file" name="uploadFile"><br/>
        檔案2<input type="file" name="uploadFile2"><br/>
        <input type="submit" value="提交">
    </form>
    @RequestMapping("/quick22")
    @ResponseBody
    public void save22(String username, MultipartFile uploadFile,MultipartFile uploadFile2) throws IOException {
        System.out.println(username);
        String originalFilename = uploadFile.getOriginalFilename();
        uploadFile.transferTo(new File("E:\\java\\" + originalFilename));
        String originalFilename2 = uploadFile2.getOriginalFilename();
        uploadFile2.transferTo(new File("E:\\java\\" + originalFilename2));
    }

(3.3)多檔案上傳,陣列

    @RequestMapping("/quick23")
    @ResponseBody
    public void save23(String username, MultipartFile[] uploadFile) throws IOException {
        System.out.println(username);
        for (MultipartFile file:uploadFile){
            String originalFilename = file.getOriginalFilename();
            file.transferTo(new File("E:\\java\\" + originalFilename));
        }
    }

    <form action="${pageContext.request.contextPath}/user/quick23" method="post" enctype="multipart/form-data">
        名稱<input type="text" name="username"><br/>
        檔案<input type="file" name="uploadFile"><br/>
        檔案2<input type="file" name="uploadFile"><br/>
        檔案3<input type="file" name="uploadFile"><br/>
        <input type="submit" value="提交">
    </form>