在使用檔案上傳元件 Commons FileUpload 與 Struts 時,需注意以下關鍵問題:
org.apache.struts.action.Action;且在配置檔中不要為該 Action 配置 ActionForm,否則透過以下代碼取得的列表將為 null:
DiskFileUpload fu = new DiskFileUpload(); // 設定最大檔案尺寸,此處為 4MB fu.setSizeMax(41943040); // 設定緩衝區大小,此處為 4KB fu.setSizeThreshold(2048); // 設定暫存目錄 fu.setRepositoryPath(tempPath); // 取得所有檔案 List fileItems = fu.parseRequest(request); Iterator i = fileItems.iterator();只要不關聯 ActionForm 即可正常運作。