struts 2 框架為處理文件上傳提供了內(nèi)置支持,它使用“在 html 中基于表單的文件上傳”。當(dāng)上傳一個(gè)文件時(shí),它通常會(huì)被存儲(chǔ)在一個(gè)臨時(shí)目錄中,而且它們應(yīng)該由 action 類進(jìn)行處理或移動(dòng)到一個(gè)永久的目錄,用來確保數(shù)據(jù)不丟失。服務(wù)器在恰當(dāng)?shù)奈恢每赡苡幸粋€(gè)安全策略,它會(huì)禁止你寫到除了臨時(shí)目錄以外的目錄,而且這個(gè)目錄屬于你的web應(yīng)用應(yīng)用程序。
通過預(yù)定義的名為文件上傳的攔截器,struts 的文件上傳是可能的,這個(gè)攔截器在 org.apache.struts2.interceptor.fileuploadinterceptor 類是可用的,而且是 defaultstack 的一部分。
創(chuàng)建視圖文件
讓我們開始創(chuàng)建需要瀏覽和上傳選定的文件的視圖。因此,讓我們創(chuàng)建一個(gè)帶有簡(jiǎn)單的 html 上傳表單的 index.jsp,它允許用戶上傳文件:(表單的編碼類型設(shè)置為multipart/form-data)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<%-- created by intellij idea. user: yzjxiaoyue date: 2017 / 7 / 28 time: 19 : 11 to change this template use file | settings | file templates. --%> <%@ page language= "java" contenttype= "text/html; charset=utf-8" pageencoding= "utf-8" %> <%@ taglib prefix= "s" uri= "/struts-tags" %> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd" > <html> <head> <title>file upload</title> </head> <body> <form action= "upload" method= "post" enctype= "multipart/form-data" > <label for = "myfile" >upload your file</label> <input type= "file" name= "myfile" id= "myfile" /> <input type= "submit" value= "upload" /> </form> </body> </html> |
之后創(chuàng)建success.jsp頁面:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<%-- created by intellij idea. user: yzjxiaoyue date: 2017 / 7 / 28 time: 19 : 14 to change this template use file | settings | file templates. --%> <%@ page contenttype= "text/html; charset=utf-8" %> <%@ taglib prefix= "s" uri= "/struts-tags" %> <html> <head> <title>file upload success</title> </head> <body> you have successfully uploaded <s:property value= "myfilefilename" /> </body> </html> |
創(chuàng)建error.jsp頁面
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<%-- created by intellij idea. user: yzjxiaoyue date: 2017 / 7 / 28 time: 20 : 05 to change this template use file | settings | file templates. --%> <%@ page contenttype= "text/html; charset=utf-8" %> <%@ taglib prefix= "s" uri= "/struts-tags" %> <html> <head> <title>file upload error</title> </head> <body> there has been an error in uploading the file. </body> </html> |
創(chuàng)建 action 類
接下來讓我們創(chuàng)建一個(gè)稱為 uploadfile.java 的 java 類,它負(fù)責(zé)上傳文件,并且把這個(gè)文件存儲(chǔ)在一個(gè)安全的位置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
package com.action; import com.opensymphony.xwork2.actionsupport; import org.apache.commons.io.fileutils; import java.io.file; import java.io.ioexception; public class uploadfile extends actionsupport{ private file myfile; public file getmyfile() { return myfile; } public void setmyfile(file myfile) { this .myfile = myfile; } private string myfilecontenttype; private string myfilefilename; private string destpath; public string execute() { /* copy file to a safe location */ destpath = "e:\\program files\\apache-tomcat-9.0.0\\apache-tomcat-9.0.0.m22\\work\\" ; try { system.out.println( "src file name: " + myfile); system.out.println( "dst file name: " + myfilefilename); file destfile = new file(destpath, myfilefilename); fileutils.copyfile(myfile, destfile); } catch (ioexception e){ e.printstacktrace(); return error; } return success; } public string getmyfilecontenttype() { return myfilecontenttype; } public void setmyfilecontenttype(string myfilecontenttype) { this .myfilecontenttype = myfilecontenttype; } public string getmyfilefilename() { return myfilefilename; } public void setmyfilefilename(string myfilefilename) { this .myfilefilename = myfilefilename; } } |
配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<?xml version= "1.0" encoding= "utf-8" ?> <!doctype struts public "-//apache software foundation//dtd struts configuration 2.3//en" "http://struts.apache.org/dtds/struts-2.3.dtd" > <struts> <constant name= "struts.devmode" value= "true" /> <constant name= "struts.multipart.maxsize" value= "10000000" /> <constant name= "struts.multipart.savedir" value= "/tmp" /> <constant name= "struts.custom.i18n.resources" value= "struts" ></constant> < package name= "default" namespace= "/" extends = "struts-default" > <action name= "upload" class = "com.action.uploadfile" > <!--<interceptor-ref name= "basicstack" />--> <interceptor-ref name= "defaultstack" /> <interceptor-ref name= "fileupload" > <param name= "allowedtypes" >image/jpeg,image/jpg,image/gif</param> </interceptor-ref> <result name= "success" >/success.jsp</result> <result name= "error" >/error.jsp</result> </action> </ package > </struts> |
界面截圖
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/yzj_xiaoyue/article/details/76284279