刚刚学了下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参数值
搞定~~
睡觉~~了~~