Struts2單檔案上傳

2021-07-24 08:35:27 字數 1432 閱讀 3894

(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="upload">

s:file>

附html**:

action="upload.action"

theme="******"

namespace="/"

method="post"

enctype="multipart/form-data">

label="上傳檔案:"

name="upload">

s:file>

value="提交上傳">

s:submit>

s:form>

1.三個成員變數的命名是有規範的:

(1)private file upload裡的的upoload一樣要和你上傳提交頁面裡的name屬性值對應一致

(2)檔案型別和檔名命名分別為:頁面name屬性值+contenttype,頁面name屬性值+filename

即action中應該這樣定義:

private file upload;                    //命名應該和上傳頁面name屬性值保持一致

private string uploadcontenttype; //屬性值+contenttype

private string uploadfilename; //屬性值+fielname

2.提供setter方法,以便接收對應的資料

3.定義目標檔案存放處,用commons-io-2.0.1.jar包提供的fileutils工具類把上傳檔案複製到指定位置

附action的**:

public

class

uploadaction

extends

actionsupport

public

void

setuploadcontenttype(string uploadcontenttype)

public

void

setuploadfilename(string uploadfilename)

public string upload() catch (ioexception e)

return success;

}}

Struts2的檔案上傳

看到有人在問struts2的檔案上傳,想起自己買的李剛的 struts2權威指南 書中已有很詳細的介紹,只是自己一下子記不起來了,真的很遺憾,說明自己學得還不夠紮實,所以自己整理了一下發上來。下面是上傳的 檔案上傳頁面 檔案上傳頁面中,包含兩個表單域,檔案標題和檔案瀏覽域 當然,為了能完成檔案上傳,...

Struts2 多檔案上傳

在 struts.xml 中 控制最大快取值 success.jsp 在 uploadaction2 中 public class uploadaction2 extends actionsupport implements serializable private file image 對應的就是...

Struts2實現上傳檔案

需用到兩個包 commons io 2.0.1.jar commons fileupload 1.3.jar form表單必須加enctype multipart form data 屬性 struts2核心配置檔案 upload.jsp 後台實現上傳模組 這三個屬性必須有 接收前台傳來的檔案 pr...