redang bay
posts - 68, comments - 19, trackbacks - 0, articles - 1
导航
首页
新随笔
联系
管理
<
2012年2月
>
日
一
二
三
四
五
六
29
30
31
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
1
2
3
4
5
6
7
8
9
10
留言簿
给我留言
查看公开留言
查看私人留言
随笔分类
.net(10)
Ajax
Hibernate(3)
HTML
JavaEE(9)
Javascript(2)
JavaSE(7)
linux(10)
MyEclipse
Oracle(6)
PL SQL for Oracle
Spring
Struts1
Struts2(2)
UML
XML
我的小说
随笔
随笔档案
2018年11月 (3)
2018年10月 (2)
2018年9月 (12)
2018年8月 (4)
2018年4月 (5)
2017年9月 (3)
2017年7月 (1)
2017年5月 (1)
2012年3月 (2)
2012年2月 (3)
2012年1月 (1)
2011年8月 (1)
2011年7月 (1)
2011年2月 (7)
2011年1月 (6)
2010年12月 (4)
2010年10月 (10)
2010年9月 (2)
搜索
最新评论
1. re: 简单工厂类
1.通过参数
2.获取父类对象
3.调用方法
--viery
2. re: jstl循环
/><script>alert('aa')<script>
--aaaaaaaaaaaaaa
3. re: jstl循环
<script>alert('aa')<script>
--aaaaaaaaaaaaaa
4. re: Java三大框架Struts、Hibernate和Spring的技术起源和由来 [未登录]
thx
--欧阳
5. re: 原创-小型oa系统
你好,你左边的导航是怎么做的?是用jquery easyui 吗?@vle
--horrsion
评论排行榜
1. 基于SSH+pager-taglib的分页封装(5)
2. 原创-小型oa系统(5)
3. Hibernate中的几种经典对象模型和关系模型的映射(1)(2)
4. jstl循环(2)
5. commons-fileupload-1.2 实现文件上传 (1)
Struts2的上传
Posted on 2012-02-28 11:24
viery
阅读(248)
评论(0)
编辑
收藏
所属分类:
Struts2
Struts2自带上传下载的实现:
1.编写输入页面和结果页面
input.jsp
<%
@ page language
=
"
java
"
import
=
"
java.util.*
"
pageEncoding
=
"
GB18030
"
%>
<%
@ taglib prefix
=
"
s
"
uri
=
"
/struts-tags
"
%>
<%
String
path
=
request.getContextPath();
String
basePath
=
request.getScheme()
+
"
://
"
+
request.getServerName()
+
"
:
"
+
request.getServerPort()
+
path
+
"
/
"
;
%>
<!
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
>
<
html
>
<
head
>
<
base
href
="<%=basePath%>"
>
<
title
>
My JSP 'index.jsp' starting page
</
title
>
<
meta
http-equiv
="pragma"
content
="no-cache"
>
<
meta
http-equiv
="cache-control"
content
="no-cache"
>
<
meta
http-equiv
="expires"
content
="0"
>
<
meta
http-equiv
="keywords"
content
="keyword1,keyword2,keyword3"
>
<
meta
http-equiv
="description"
content
="This is my page"
>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<
script
language
="JavaScript"
>
function
addComponent()
{
var
td
=
document.getElementById(
"
files
"
);
var
br
=
document.createElement(
"
<br>
"
);
var
input
=
document.createElement(
"
input
"
);
var
button
=
document.createElement(
"
input
"
);
input.name
=
"
uploads
"
;
input.type
=
"
file
"
;
button.type
=
"
button
"
;
button.value
=
"
删除
"
;
button.onclick
=
function
()
{
td.removeChild(br);
td.removeChild(input);
td.removeChild(button);
}
td.appendChild(br);
td.appendChild(input);
td.appendChild(button);
}
</
script
>
</
head
>
<
body
>
<
input
type
="button"
onclick
="addComponent();"
value
="添加文件"
/>
<
br
/>
<
form
onsubmit
="return true;"
action
="upload"
method
="post"
enctype
="multipart/form-data"
>
<
table
>
<
tr
>
<
td
id
="files"
>
<
input
type
='file'
name
='uploads'
/>
</
td
>
</
tr
>
</
table
>
<
input
type
="submit"
value
="上传"
/>
</
form
>
<
s:fielderror
/>
</
body
>
</
html
>
js用于自动生成和控制删除上传控件的数量。
result.jsp
<body>
This a struts page. <br>
</body>
2.配置struts.xml
<?
xml version="1.0" encoding="UTF-8"
?>
<!
DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"
>
<
struts
>
<
constant
name
="struts.custom.i18n.resources"
value
="message"
></
constant
>
<
constant
name
="struts.i18n.encoding"
value
="gbk"
/>
<
constant
name
="struts.multipart.saveDir"
value
="f:\"
/>
<
constant
name
="struts.multipart.maxSize"
value
="2097152000"
/>
<
package
name
="JsUpload"
extends
="struts-default"
>
<
action
name
="upload"
class
="org.vle.action.UploadAction"
>
<
result
>
/result.jsp
</
result
>
<
result
name
="input"
>
/upload.jsp
</
result
>
<
interceptor-ref
name
="fileUpload"
>
<
param
name
="maximumSize"
>
10240000
</
param
>
<
param
name
="allowedTypes"
>
application/vnd.ms-powerpoint
</
param
>
</
interceptor-ref
>
<
interceptor-ref
name
="defaultStack"
></
interceptor-ref
>
</
action
>
</
package
>
</
struts
>
3实现action
package
org.vle.action;
import
java.io.File;
import
java.io.FileInputStream;
import
java.io.FileOutputStream;
import
java.io.InputStream;
import
java.io.OutputStream;
import
java.util.Iterator;
import
java.util.List;
import
org.apache.struts2.ServletActionContext;
import
com.opensymphony.xwork2.Action;
import
com.opensymphony.xwork2.ActionSupport;
public
class
UploadAction
extends
ActionSupport
{
private
List
<
File
>
uploads;
private
List
<
String
>
uploadsFileName;
private
List
<
String
>
uploadsContentType;
public
List
<
String
>
getUploadsContentType()
{
return
uploadsContentType;
}
public
void
setUploadsContentType(List
<
String
>
uploadsContentType)
{
this
.uploadsContentType
=
uploadsContentType;
}
public
List
<
File
>
getUploads()
{
return
uploads;
}
public
void
setUploads(List
<
File
>
uploads)
{
this
.uploads
=
uploads;
}
public
List
<
String
>
getUploadsFileName()
{
return
uploadsFileName;
}
public
void
setUploadsFileName(List
<
String
>
uploadsFileName)
{
this
.uploadsFileName
=
uploadsFileName;
}
@Override
public
String execute()
throws
Exception
{
for
(
int
i
=
0
;i
<
uploads.size();i
++
)
{
InputStream is
=
new
FileInputStream(uploads.get(i));
String name
=
uploadsFileName.get(i);
String type
=
uploadsContentType.get(i);
System.out.println(type);
String path
=
ServletActionContext.getRequest().getRealPath(
"
/temp
"
);
File f
=
new
File(path,name);
File temp
=
new
File(path);
if
(
!
temp.exists())
{
temp.mkdirs();
}
OutputStream os
=
new
FileOutputStream(f);
byte
[] b
=
new
byte
[
1024
];
int
len;
while
((len
=
is.read(b))
>
0
)
{
os.write(b,
0
, len);
}
os.close();
is.close();
}
return
Action.SUCCESS;
}
}
新用户注册
刷新评论列表
只有注册用户
登录
后才能发表评论。
网站导航:
博客园
IT新闻
Chat2DB
C++博客
博问
管理
相关文章:
Struts2整合jfreechart
Struts2的上传
Powered by:
BlogJava
Copyright © viery