struts 單檔案上傳

2021-06-04 11:59:02 字數 1205 閱讀 3042

原文**:

struts檔案上傳

1.建立乙個工程:

建立乙個jsp頁面內容如下:

2.建立乙個formbean繼承actionform

其中有個private formfile file ;屬性。formfile類的全名為:org.apache.struts.upload.formfile

3.建立乙個uploadaction繼承自action

然後重寫action的execute()方法:

**如下:

4.建立fileutil工具類,裡面實現上傳的檔案的方法:

關鍵**如下:

public class fileutil

file.createnewfile();

} catch (ioexception e)

return file;

}/**

* 獲得隨機數

* @return

*/private  static int getrandomnumber()     

/*** 分割檔名 如a.txt 返回 a

* @param filename

* @return

*/private static string getfilenameprefix(string filename)

/*** 獲得檔案字尾

* @param filename

* @return

*/private static string getfilenamesuffix(string filename)          

/*** 上傳檔案

* @param file

* @param dir

* @return

*/public static string uploadfile(formfile file,string dir)

out.flush();

} catch (filenotfoundexception e) catch (ioexception e)

finally

catch (ioexception e)

try catch (ioexception e)

}return null;}}

Struts2單檔案上傳

1 commons fileupload 1.2.2.jar 2 commons io 2.0.1.jar 1 表單必須使用post方式提交 2 表單編碼型別 enctype multipart form data 3 file標籤中,name屬性必須要寫 label 上傳檔案 name uploa...

Struts 檔案上傳

回顧 struts提交的檔案上傳元件來上傳檔案 前台 後台 fileitemfactory fileitem的核心工廠 servletfileupload servlet中檔案上傳的核心類 fileitem 封裝了上傳的表單檔案項資訊總之,檔案上傳比較麻煩!檔案上傳demopublic class ...

struts 檔案上傳

實現單個檔案上傳 步驟1.匯入jar包 commons fileupload x.x.x.jar commons io x.x.x.jar 步驟2 jsp頁面 enctype multipart form data upload lable 選擇檔案 步驟3 action頁面 package org...