框架學習之Struts2 第四節 檔案上傳

2022-02-20 06:44:53 字數 2655 閱讀 9367

1. 上傳單個檔案的步驟

第一步:引入jar包,後者jar包是很重要的,利用它的fileutils處理上傳的檔案

第二步:把form表單中的 enctype 設定為:multipart/form-data

表單中包含乙個

第三步:在action類中新增一些屬性

注意:後台的file物件的名稱要和前台的input的name相同,這樣struts2就可以利用反射技術呼叫set方法給file賦值了

也就是說,action類中的file就變成了前台上傳的檔案了

測試:

在原專案中新增乙個頁面 file.jsp

主要內容:

<

body

>

<

s:form

enctype

="multipart/form-data"

method

="post"

action

="file"

namespace

="/yinger"

>

file to upload:

<

br>

<

input

type

="file"

name

="file"

/><

br>

<

input

type

="submit"

name

="submit"

value

="提交"

/><

br>

s:form

>

body

>

新增乙個action

<

action

name

="file"

class

="com.yinger.helloworld"

method

="file"

>

<

result

name

="file"

>

/message.jsp

result

>

action

>

action類的方法:

private

file file;

private

string filefilename;

private

string filecontenttype;

public

string file()

throws

exception

fileutils.copyfile(file,

newfile(newfile, filefilename));

//然後複製檔案

message="

檔案上傳成功!";

message+="

檔名稱:"+

filefilename;

message+="

檔案型別:"+

filecontenttype;

return

"file";

}public

file getfile()

public

void

setfile(file file)

public

string getfilefilename()

public

void

setfilefilename(string filefilename)

public

string getfilecontenttype()

public

void

setfilecontenttype(string filecontenttype)

測試結果:

輸入:http://localhost:8080/struts2test/file.jsp

結果:

檢視檔案:

已經上傳到了指定的資料夾了!

還有乙個問題就是,注意上傳的檔案大小的限制,這個是可以設定的

value就是設定檔案的大小,注意它是所有檔案大小的總和,預設是 2m

2.上傳多個檔案的步驟

它的步驟和單個檔案上傳其實差不多,只不過file變成了檔案的陣列而已,儲存時迴圈一下就好了

注意,jsp中定義的type為file的input的name要相同

多檔案上傳方法類似,再次略過了。。。

python學習 GUI第四節

今天用gui內容寫乙個加法計算器 需要三個對話方塊及乙個按鈕 from tkinter import master tk v1 strin ar v2 strin ar v3 strin ar def text content 測試輸入是否為數字,若不是數字則不能錄入 return content....

ObjC第四節 繼承

繼 承 1 繼承 1.1 nsobject,根類,oc建立的類都繼承自根類,位於類層次結構的頂層,沒有父類 1.2 父類的例項變數和方法都可以被隱式的繼承過來成為子類的一部分,子類就可以直接訪問這些例項變數和方法 1.3 alloc和init是繼承自nsobject的方法 1.4 繼承的概念呈單鏈繼...

第四節 條件語句

if語句 if v 100else if v 50elseif語句不需要括號 用法高階 const filename abc.txt if contents,error ioutil.readfile filename error nilelse注意 contents和error變數都是在if 塊中...