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
閱讀(255)
評論(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
主站蜘蛛池模板:
丽江市
|
太保市
|
府谷县
|
滁州市
|
宁夏
|
建水县
|
孟津县
|
同德县
|
河西区
|
鹤峰县
|
祁连县
|
包头市
|
鄂托克前旗
|
报价
|
东乌
|
梁平县
|
鄯善县
|
东莞市
|
鄱阳县
|
河北区
|
平武县
|
鄢陵县
|
赣州市
|
大厂
|
建德市
|
卓尼县
|
灌云县
|
湾仔区
|
宝应县
|
凤山县
|
宁明县
|
新平
|
太仓市
|
应城市
|
北碚区
|
湘潭县
|
巫山县
|
河池市
|
汉川市
|
利辛县
|
浮山县
|