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

Struts 1.x 系列教程(8):單檔案上傳完整實作與配置

     編輯:Java 相關知識

前言

Struts 透過 <html:file> 標籤與 FormFile 介面,將檔案上傳封裝為標準 ActionForm 屬性處理流程。本文六步驟完成單檔上傳功能,含儲存路徑配置與大小限制。

六步驟實作

1. JSP 上傳頁面

<%@ taglib uri="https://struts.apache.org/tags-html" prefix="html" %>
<html:form enctype="multipart/form-data" action="uploadFile">
    <html:file property="myFile" /><br>
    <html:submit value="上傳" />
</html:form>

關鍵:enctype="multipart/form-data" 必須設定,否則瀏覽器不會送出檔案內容。

2. FormBean 定義

package actionform;
import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;

public class UploadForm extends ActionForm {
    private FormFile myFile;
    public FormFile getMyFile() { return myFile; }
    public void setMyFile(FormFile myFile) { this.myFile = myFile; }
}

3. Action 類別處理上傳

package action;
import actionform.UploadForm;
import org.apache.struts.upload.FormFile;
import java.io.*;

public class UploadAction extends Action {
    protected void saveFile(FormFile formFile) throws Exception {
        String path = getServlet().getServletConfig().getInitParameter("uploadPath");
        InputStream in = formFile.getInputStream();
        FileOutputStream out = new FileOutputStream(path + formFile.getFileName());
        byte[] buffer = new byte[8192];
        int len;
        while ((len = in.read(buffer)) > 0) out.write(buffer, 0, len);
        out.close();
        formFile.destroy(); // 釋放暫存檔資源
    }

    public ActionForward execute(ActionMapping mapping, ActionForm form,
                                 HttpServletRequest request, HttpServletResponse response) {
        UploadForm uf = (UploadForm) form;
        try {
            response.setCharacterEncoding("GBK");
            saveFile(uf.getMyFile());
            response.getWriter().println("上傳檔案成功.");
        } catch (Exception e) {
            response.getWriter().println(e.getMessage());
        }
        return null;
    }
}

FormFile 主要方法:getInputStream()、getFileData()、getFileName()、getFileSize()、destroy()。

4. struts-config.xml 配置

<form-bean name="uploadForm" type="actionform.UploadForm" />
<action name="uploadForm" path="/uploadFile" scope="request" type="action.UploadAction" />

5. web.xml 設定儲存路徑

<servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
        <param-name>uploadPath</param-name>
        <param-value>D:\upload\</param-value>  <!-- 必須以反斜線結尾 -->
    </init-param>
</servlet>

6. 限制上傳大小

<controller maxFileSize="2M" />
  • 單位支援 K、M、G(如 2K、5G),僅接受整數。
  • 超過大小會拋出例外,可在 ActionForm.validate() 或全域例外處理攔截。

測試

訪問 https://localhost:8080/samples/uploadFile.jsp 選擇檔案上傳,成功後顯示訊息,檔案出現在 D:\upload\ 目錄。

總結

Struts 檔案上傳核心三要素:enctype="multipart/form-data"、FormFile 屬性、maxFileSize 控制器參數。掌握此流程,多檔上傳(教程 9)即為索引屬性與前端動態欄位的自然延伸。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved