剛剛學了下Struts2,做個文件上傳的小東西
以做一個文件上傳的例子展開
要想用Struts2,首先下個Struts2.0的包
http://people.apache.org/builds/struts/
這里版本可是真不少的,我下的2.1.2的包
也就是struts-2.1.2-all.zip 這頭東西
下完了就可以開始配置運行環境了
新建一個WebProject,名字就叫myStruts2好了
要正常啟動能運行Struts2最少需要5個Struts2包里lib目錄下的5個文件
分別是:
commons-logging-1.0.4.jar
freemarker-2.3.12.jar
ognl-2.6.11.jar
struts2-core-2.1.2.jar
xwork-2.1.1.jar
為了做下面這個例子,還需要拷貝
commons-io-1.3.2.jar
commons-fileupload-1.2.1.jar
這7個文件copy到項目webroot下的WEB-INF下的lib目錄下就行了
準備工作完畢。開工,做這個文件上傳的例子
首先配置WEB-INF下的web.xml文件如下:
?1
<?
xml?version="1.0"?encoding="UTF-8"
?>
?2
<
web-app?
version
="2.4"
?xmlns
="http://java.sun.com/xml/ns/j2ee"
?3
????xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
?4
????xsi:schemaLocation
="http://java.sun.com/xml/ns/j2ee?
?5
????http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
>
?6
?7
????
<
filter
>
?8
????????
<
filter-name
>
struts2
</
filter-name
>
?9
????????
<
filter-class
>
10
????????????org.apache.struts2.dispatcher.FilterDispatcher
11
????????
</
filter-class
>
12
????
</
filter
>
13
????
<
filter-mapping
>
14
????????
<
filter-name
>
struts2
</
filter-name
>
15
????????
<
url-pattern
>
/*
</
url-pattern
>
16
????
</
filter-mapping
>
17
18
????
<
welcome-file-list
>
19
????????
<
welcome-file
>
index.jsp
</
welcome-file
>
20
????
</
welcome-file-list
>
21
22
</
web-app
>
23

?2

?3

?4

?5

?6

?7

?8

?9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

然后修改下index.jsp文件,做一個文件上傳的頁面
?1
<%
@?page?language
=
"
java
"
?pageEncoding
=
"
UTF-8
"
%>
?2
<%
@?taglib?prefix
=
"
s
"
?uri
=
"
/struts-tags
"
%>
?3
?4
<!
DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN"
>
?5
<
html
>
?6
??
<
head
>
?7
????
?8
????
<
title
>
My?JSP?'index.jsp'?starting?page
</
title
>
?9
????
<
meta?
http-equiv
="pragma"
?content
="no-cache"
>
10
????
<
meta?
http-equiv
="cache-control"
?content
="no-cache"
>
11
????
<
meta?
http-equiv
="expires"
?content
="0"
>
????
12
????
<
meta?
http-equiv
="keywords"
?content
="keyword1,keyword2,keyword3"
>
13
????
<
meta?
http-equiv
="description"
?content
="This?is?my?page"
>
14
????
<!--
15
????<link?rel="stylesheet"?type="text/css"?href="styles.css">
16
????
-->
17
??
</
head
>
18
??
19
??
<
body
>
20
????
<
s:form?
action
="upload"
??method
="post"
?enctype
="multipart/form-data"
>
21
????
<
s:file?
name
="pic"
?label
="請選擇文件"
></
s:file
>
22
????
<
s:submit?
value
="提交"
></
s:submit
>
23
????
</
s:form
><
br
>
24
??
</
body
>
25
</
html
>
26


?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

做好這里,然后在src目錄下創建struts.xml文件
?1
<?
xml?version="1.0"?encoding="UTF-8"
?>
?2
<!
DOCTYPE?struts?PUBLIC
?3
????"-//Apache?Software?Foundation//DTD?Struts?Configuration?2.0//EN"
?4
????"http://struts.apache.org/dtds/struts-2.0.dtd"
>
?5
<
struts
>
?6
????
<
constant?
name
="struts.devMode"
?value
="true"
?
/>
?7
????
?8
????
<
package?
name
="default"
?extends
="struts-default"
>
?9
????????
<
action?
name
="upload"
?class
="com.home.jiangfan.Upload"
>
10
????????
<
interceptor-ref?
name
="timer"
></
interceptor-ref
>
11
????????
<
interceptor-ref?
name
="defaultStack"
></
interceptor-ref
>
12
????????
<
result?
name
="success"
>
/result.jsp
</
result
>
13
????????
</
action
>
14
????
</
package
>
15
16
17
????
18
</
struts
>
19
20

?2

?3

?4

?5

?6

?7

?8

?9

10

11

12

13

14

15

16

17

18

19

20

接著創建Upload.java文件,在src里我新建了com.home.jiangfan的包,文件在這個包里
?1
package?com.home.jiangfan;
?2
?3
import?java.io.*;
?4
import?java.util.*;
?5
?6
import?org.apache.struts2.interceptor.ParameterAware;
?7
?8
public?class?Upload?implements?ParameterAware{
?9
????Map?parameters;
10
????File?pic;
11
????String?picContentType;
12
????String?picFileName;
13
????public?File?getPic()?{
14
????????return?pic;
15
????}
16
????public?void?setPic(File?pic)?{
17
????????this.pic?=?pic;
18
????}
19
????public?String?getPicContentType()?{
20
????????return?picContentType;
21
????}
22
????public?void?setPicContentType(String?picContentType)?{
23
????????this.picContentType?=?picContentType;
24
????}
25
????public?String?getPicFileName()?{
26
????????return?picFileName;
27
????}
28
????public?void?setPicFileName(String?picFileName)?{
29
????????this.picFileName?=?picFileName;
30
????}
31
????
32
????public?String?execute(){
33
????????String?path="D:"+File.separator+"copytest"+File.separator;
34
????????path=path+picFileName;
35
????????File?des=new?File(path);
36
????????if(des.exists()){
37
????????????des.delete();
38
????????}
39
????????pic.renameTo(des);
40
????????
41
????????return?"success";
42
????}
43
????
44
????public?void?setParameters(Map?arg0)?{
45
????????//?TODO?Auto-generated?method?stub
46
????????parameters=arg0;
47
????}
48
49
}
50

?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

好了,然后做一個返回的result頁面
?1
<%
@?page?language="java"?pageEncoding="UTF-8"%>
?2
<%
@?taglib?prefix="s"?uri="/struts-tags"%>
?3
?4
<!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN">
?5
<html>
?6
??<head>
?7
????
?8
????<title>My?JSP?'index.jsp'?starting?page</title>
?9
????<meta?http-equiv="pragma"?content="no-cache">
10
????<meta?http-equiv="cache-control"?content="no-cache">
11
????<meta?http-equiv="expires"?content="0">????
12
????<meta?http-equiv="keywords"?content="keyword1,keyword2,keyword3">
13
????<meta?http-equiv="description"?content="This?is?my?page">
14
????<!--
15
????<link?rel="stylesheet"?type="text/css"?href="styles.css">
16
????-->
17
??</head>
18
??
19
??<body>
20
??文件名<s:property?value="picFileName"/>
21
??文件類型<s:property?value="picContentType"/>
22
??</body>
23
</html>
24



?2



?3

?4

?5

?6

?7

?8

?9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

其中為了修改文件上傳里,文件size的限制,需要在src目錄下添加一個struts.properties文件:
struts.multipart.maxSize=1024000000
這里我設置的大小是1G(這個總夠了吧!)
配合這個使用的方式,還需要在struts.xml里的Action標簽中間添加一段:
<interceptor-ref name="defaultStack">
??<param name="fileUpload.maximumSize">1024000000</param>
??</interceptor-ref>
實際上是修改fileUpload這個攔截器中的maximumSize參數值
搞定~~
睡覺~~了~~