首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Struts 2 文件上傳下載完整實戰指南

     編輯:Java 相關知識

文件上傳核心機制

Struts 2 內置 FileUploadInterceptor(屬於 defaultStack)支援基於表單的文件上傳。上傳文件先存臨時目錄,Action 需處理或移動到永久目錄,否則可能丟失。

關鍵配置要點

  • 表單 enctype="multipart/form-data"(必須)
  • 文件字段名(如 myFile)對應 Action 的三個屬性:
    File myFile(文件對象)、String myFileContentType(MIME 類型)、String myFileFileName(原始文件名)
  • struts.multipart.maxSize 限制最大上傳大小(默認 2MB)

完整上傳實戰

JSP 頁面

<form action="Upload.action" method="post" enctype="multipart/form-data">
  <label for="myFile">你要上傳的文件</label>
  <input type="file" name="myFile" /><br>
  <input type="submit" value="上傳"/>
</form>

Action 類

public class FileUploadAction extends ActionSupport {
    private File myFile;
    private String myFileContentType;
    private String myFileFileName;
    private String destPath = "e:/upload/";

    // getter/setter 省略...

    @Override
    public String execute() throws Exception {
        try {
            System.out.println("Src File name: " + myFile);
            System.out.println("我的文件名: " + myFileFileName);
            System.out.println("我的文件類型: " + myFileContentType);
            File destFile = new File(destPath, myFileFileName);
            FileUtils.copyFile(myFile, destFile); // 需 commons-io
        } catch (IOException e) {
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS;
    }
}

struts.xml 配置

<constant name="struts.multipart.maxSize" value="10701096" /> <!-- 10MB -->
<package name="upload" namespace="/" extends="struts-default">
  <action name="Upload" class="com.oumyye.FileUpload.FileUploadAction">
    <result name="success">/success.jsp</result>
    <result name="input">/index.jsp</result>
  </action>
  <!-- 下載配置見下 -->
</package>

文件下載實戰

JSP 頁面

<h2>文件下載內容:</h2><br/>
Dream.jpg: <a href="FileDownload.action">點擊下載</a><br/>

Action 類(關鍵:返回 InputStream)

public class FileDownload extends ActionSupport {
    private String fileName;
    public String getFileName() { return fileName; }
    public void setFileName(String fileName) { this.fileName = fileName; }

    public InputStream getDownloadFile() throws Exception {
        this.fileName = "hello.jpg"; // 實際可從參數獲取
        return ServletActionContext.getServletContext()
            .getResourceAsStream("upload/" + this.fileName);
    }

    @Override public String execute() { return SUCCESS; }
}

struts.xml 配置(type="stream")

<action name="FileDownload" class="com.oumyye.action.FileDownload">
  <result name="success" type="stream">
    <param name="contentType">text/plain</param>
    <param name="contentDisposition">attachment;fileName="${fileName}"</param>
    <param name="inputName">downloadFile</param> <!-- 對應 getDownloadFile() -->
    <param name="bufferSize">1024</param>
  </result>
</action>

常見下載錯誤排查

Can not find a java.io.InputStream with the name [downloadFile] in the invocation stack.

原因 1:文件路徑錯誤,getResourceAsStream() 返回 null。打印流對象驗證。
原因 2:Action 缺少 getDownloadFile() 方法(需與 inputName 參數對應)。

web.xml 基礎配置

<filter>
  <filter-name>struts2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>*.action</url-pattern>
</filter-mapping>

總結

  • 上傳:表單 enctype + 三屬性約定 + FileUploadInterceptor + FileUtils.copyFile 持久化。
  • 下載:Action 提供 InputStream + type="stream" Result + contentDisposition 觸發瀏覽器下載。
  • 注意臨時文件清理、大小限制、路徑安全、中文文件名編碼問題。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved