Struts2的檔案上傳下載

PY胖胖峰發表於2020-10-29

Struts2檔案上傳概述

Struts2使用Commons-FileUpload上傳框架從HttpServletRequest請求中解析出所有的表單域,包括檔案域和普通表單域。
Commons-FileUpload框架是Apache組織下的jakarta-commons專案的一個子專案,藉助於Commons-IO專案,該框架可以方便地將使用”multipart/form-data“編碼的表單域資料解析出來

Struts2的檔案上傳

struts2上傳檔案基本語法:
使用表單File域來上傳檔案,同時必須設定表單的enctype=“multipart/form-data”以及method=“post”
在動作中宣告以下三個變數:
File XXX:對應上傳檔案內容的變數,與檔案域名稱一致
String XXXFileName:對應上傳檔案的檔名(可選)
String XXXContentType:對應上傳檔案的型別(可選)
在動作中定義對應上述變數的屬性的setter和getter方法

設定上傳檔案的大小和型別

在檔案上傳時可以限制檔案的大小和型別
使用Struts2框架所提供的fileUpload攔截器,通過指定以下兩個屬性可以很容易達到上述要求:
allowedTypes:指定允許上傳的檔案型別
maximumSize:指定允許上傳的檔案大小,
單位為位元組
當上傳檔案不符合要求時,系統自動轉到
input邏輯檢視。

示例:

上傳後這裡會展示一個連結

pom.xml依賴:

<dependencies>
        <dependency>
            <groupId>org.ow2.asm</groupId>
            <artifactId>asm</artifactId>
            <version>7.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.ow2.asm/asm-commons -->
        <dependency>
            <groupId>org.ow2.asm</groupId>
            <artifactId>asm-commons</artifactId>
            <version>7.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.ow2.asm/asm-tree -->
        <dependency>
            <groupId>org.ow2.asm</groupId>
            <artifactId>asm-tree</artifactId>
            <version>7.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.4</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.6</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.8.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.freemarker/freemarker -->
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.28</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.javassist/javassist -->
        <dependency>
            <groupId>org.javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.20.0-GA</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.12.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/ognl/ognl -->
        <dependency>
            <groupId>ognl</groupId>
            <artifactId>ognl</artifactId>
            <version>3.1.26</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-core -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.5.22</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>

相關文章