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

Struts 檔案上傳實作(二):ActionForm 定義多檔案上傳欄位

     編輯:Java 相關知識

承接上篇,本文定義 MaintainForm 繼承 ActionForm,封裝三個 FormFile 屬性對應前端 ``,並示範如何整合業務物件 Trainplan 實現表單資料與檔案並行處理。

ActionForm 完整程式碼

package tester.business.maintain;

import org.apache.struts.action.*;
import javax.servlet.http.*;
import org.apache.struts.upload.*;

public class MaintainForm extends ActionForm {
    // 業務資料委託給 Trainplan
    private Trainplan trainPlan = new Trainplan();

    // 三個檔案上傳欄位
    private FormFile theFile1;
    private FormFile theFile2;
    private FormFile theFile3;

    // Trainplan 代理存取器
    public String getP_title() { return trainPlan.getP_title(); }
    public void setP_title(String p_title) { trainPlan.setP_title(p_title); }
    public Trainplan getTrainPlan() { return trainPlan; }
    public void setTrainPlan(Trainplan tp) { this.trainPlan = tp; }

    // FormFile 存取器
    public FormFile getTheFile1() { return theFile1; }
    public void setTheFile1(FormFile f) { this.theFile1 = f; }
    public FormFile getTheFile2() { return theFile2; }
    public void setTheFile2(FormFile f) { this.theFile2 = f; }
    public FormFile getTheFile3() { return theFile3; }
    public void setTheFile3(FormFile f) { this.theFile3 = f; }

    @Override public ActionErrors validate(ActionMapping mapping, HttpServletRequest req) {
        return null; // 實際專案加入檔案大小、類型、必填驗證
    }
    @Override public void reset(ActionMapping mapping, HttpServletRequest req) {
        // 重置檔案欄位
        theFile1 = theFile2 = theFile3 = null;
    }
}

前端 JSP 對應

<html:form action="/maintain" enctype="multipart/form-data">
  <html:text property="trainPlan.p_title"/>
  <html:file property="theFile1"/>
  <html:file property="theFile2"/>
  <html:file property="theFile3"/>
  <html:submit/>
</html:form>

Action 中處理檔案

public ActionForward execute(ActionMapping mapping, ActionForm form,
                             HttpServletRequest req, HttpServletResponse res) {
    MaintainForm mf = (MaintainForm) form;
    // 1. 取得業務資料
    Trainplan tp = mf.getTrainPlan();
    // 2. 處理每個上傳檔
    for (FormFile ff : new FormFile[]{mf.getTheFile1(), mf.getTheFile2(), mf.getTheFile3()}) {
        if (ff != null && ff.getFileSize() > 0) {
            String fileName = ff.getFileName();
            InputStream is = ff.getInputStream();
            // 儲存至磁碟/資料庫/雲端儲存
            // ...
            ff.destroy(); // 釋放暫存檔
        }
    }
    return mapping.findForward("success");
}

struts-config.xml 關鍵設定

<controller maxFileSize="10M" tempDir="/tmp/struts-upload"/>
<action path="/maintain"
        type="tester.business.maintain.MaintainAction"
        name="maintainForm"
        scope="request"
        input="/maintain.jsp">
  <forward name="success" path="/list.do"/>
</action>

注意事項

  • enctype="multipart/form-data" 必須設定,否則 FormFile 為 null。
  • controller maxFileSize 限制單檔上限,超過拋 IOException。
  • 暫存目錄 tempDir 需具備寫入權限。
  • 處理完畢務必呼叫 FormFile.destroy() 釋放磁碟空間。
  • Struts 1.x 已 EOL,新專案建議改用 Spring MVC MultipartFile 或 Servlet 3.0 @MultipartConfig。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved