Java天地
Struts文件上傳和下載
jsp
<%
@ page language
=
"
java
"
contentType
=
"
text/html; charset=GBK
"
%>
<
html
>
<
head
>
<
meta
http-equiv
="Content-Type"
content
="text/html; charset=gbk"
/>
<
title
></
title
>
</
head
>
<
body
>
<
br
>
<
html:form
action
="/datamanage.data.upload.do"
method
="post"
enctype
="multipart/form-data"
target
="_parent"
>
<
table
width
="45%"
border
="0"
align
="center"
cellpadding
="0"
cellspacing
="0"
class
="table_box"
>
<
tr
>
<
td
align
="center"
class
="title_box"
colspan
="2"
>
文件上傳
</
td
>
</
tr
>
<
tr
>
<
td
>
文件:
</
td
>
<
td
>
<
html:file
property
="files"
styleClass
="text_2003"
/>
</
td
>
</
tr
>
<
tr
>
<
td
>
描述:
</
td
>
<
td
>
<
textarea
rows
="3"
cols
="40"
class
="text_2003"
name
="data_desc"
></
textarea
>
</
td
>
</
tr
>
<
tr
>
<
td
class
="bottom_box"
colspan
="2"
align
="center"
>
<
input
type
="button"
value
="上傳"
styleClass
="button_2003"
onclick
="checkForm();"
/>
<
input
type
="button"
onclick
="cancle();"
value
="取消"
styleClass
="button_2003"
/>
</
td
>
</
tr
>
</
html:form
>
</
body
>
</
html
>
DataUploadForm
package
eoms.datamanage.struts.form;
import
eoms.common.base.BaseForm;
import
org.apache.struts.upload.FormFile;
public
class
DataUploadForm
extends
BaseForm
{
private
FormFile files;
private
String type_id;
private
String data_path;
private
String data_desc;
public
String getType_id()
{
return
type_id;
}
public
void
setType_id(String type_id)
{
this
.type_id
=
type_id;
}
public
FormFile getFiles()
{
return
files;
}
public
void
setFiles(FormFile file)
{
this
.files
=
file;
}
public
String getData_path()
{
return
data_path;
}
public
void
setData_path(String data_path)
{
this
.data_path
=
data_path;
}
public
String getData_desc()
{
return
data_desc;
}
public
void
setData_desc(String data_desc)
{
this
.data_desc
=
data_desc;
}
}
文件下載Action
package
eoms.datamanage.struts.action;
import
eoms.common.base.BaseAction;
import
eoms.datamanage.dao.DataTypeDAO;
import
org.apache.struts.action.ActionForm;
import
org.apache.struts.action.ActionForward;
import
org.apache.struts.action.ActionMapping;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
java.io.
*
;
import
java.net.URLEncoder;
public
class
DataDownAction
extends
BaseAction
{
public
ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
throws
Exception
{
DataTypeDAO typeDao
=
(DataTypeDAO)
this
.getBean(
"
DataTypeDAO
"
);
String type_id
=
request.getParameter(
"
type_id
"
);
String data_name
=
request.getParameter(
"
data_name
"
);
String path
=
typeDao.getDataTypeById(Integer.parseInt(type_id)).getData_path()
+
"
/
"
+
data_name;
BufferedInputStream bis
=
null
;
BufferedOutputStream bos
=
null
;
OutputStream fos
=
null
;
InputStream fis
=
null
;
String filepath
=
path;
System.out.println(
"
文件路徑
"
+
filepath);
File uploadFile
=
new
File(filepath);
fis
=
new
FileInputStream(uploadFile);
bis
=
new
BufferedInputStream(fis);
fos
=
response.getOutputStream();
bos
=
new
BufferedOutputStream(fos);
//
這個(gè)就就是彈出下載對(duì)話框的關(guān)鍵代碼
response.setHeader(
"
Content-disposition
"
,
"
attachment;filename=
"
+
URLEncoder.encode(data_name,
"
GBK
"
));
int
bytesRead
=
0
;
//
用輸入流進(jìn)行先讀,然后用輸出流去寫(xiě)
byte
[] buffer
=
new
byte
[
8192
];
while
((bytesRead
=
bis.read(buffer,
0
,
8192
))
!=
-
1
)
{
bos.write(buffer,
0
, bytesRead);
}
bos.flush();
fis.close();
bis.close();
fos.close();
bos.close();
return
null
;
}
}
文件上傳Action
package
eoms.datamanage.struts.action;
import
com.metarnet.security.model.User;
import
eoms.common.Constants;
import
eoms.common.base.BaseAction;
import
eoms.datamanage.dao.DataDAO;
import
eoms.datamanage.dao.DataTypeDAO;
import
eoms.datamanage.model.DataModel;
import
eoms.datamanage.model.DataTypeModel;
import
eoms.datamanage.struts.form.DataUploadForm;
import
org.apache.struts.action.ActionForm;
import
org.apache.struts.action.ActionForward;
import
org.apache.struts.action.ActionMapping;
import
org.apache.struts.upload.FormFile;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
java.io.FileOutputStream;
import
java.io.InputStream;
import
java.io.OutputStream;
import
java.util.HashMap;
import
java.util.Map;
public
class
DataUploadAction
extends
BaseAction
{
public
ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
throws
Exception
{
User user
=
(User) request.getSession().getAttribute(
Constants.SESSION_USER_KEY);
DataTypeDAO typeDao
=
(DataTypeDAO)
this
.getBean(
"
DataTypeDAO
"
);
DataDAO dataDao
=
(DataDAO)
this
.getBean(
"
DataDAO
"
);
DataUploadForm theForm
=
(DataUploadForm) form;
FormFile file
=
theForm.getFiles();
//
取得上傳的文件
String filePath
=
theForm.getData_path();
//
上傳到指定的文件夾中
String fileDesc
=
theForm.getData_desc();
String type_id
=
theForm.getType_id();
try
{
InputStream stream
=
file.getInputStream();
//
把文件讀入
OutputStream bos
=
new
FileOutputStream(filePath
+
"
/
"
+
file.getFileName());
//
建立一個(gè)上傳文件的輸出流
int
bytesRead
=
0
;
byte
[] buffer
=
new
byte
[
8192
];
while
((bytesRead
=
stream.read(buffer,
0
,
8192
))
!=
-
1
)
{
bos.write(buffer,
0
, bytesRead);
//
將文件寫(xiě)入服務(wù)器
}
bos.close();
stream.close();
DataTypeModel typeModel
=
typeDao.getDataTypeById(Integer.parseInt(type_id));
//
判斷是否已經(jīng)存在
Map map
=
new
HashMap();
map.put(
"
type_code
"
, typeModel.getData_type_code());
map.put(
"
data_name
"
, file.getFileName());
DataModel data
=
dataDao.getDataByTypeAndName(map);
if
(data
==
null
)
{
//
將資料信息保存進(jìn)數(shù)據(jù)庫(kù)
DataModel model
=
new
DataModel();
model.setData_type_code(typeModel.getData_type_code());
model.setData_name(file.getFileName());
model.setData_type(typeModel.getData_type_name());
model.setData_desc(fileDesc);
model.setData_author(user.getUserId());
dataDao.addNewData(model);
}
}
catch
(Exception e)
{
System.err.print(e);
}
request.setAttribute(
"
type_id
"
, type_id);
return
mapping.findForward(
"
success
"
);
}
}
posted on 2011-03-25 16:39
Mr.lu
閱讀(196)
評(píng)論(0)
編輯
收藏
新用戶注冊(cè)
刷新評(píng)論列表
只有注冊(cè)用戶
登錄
后才能發(fā)表評(píng)論。
網(wǎng)站導(dǎo)航:
博客園
IT新聞
Chat2DB
C++博客
博問(wèn)
管理
Powered by:
BlogJava
Copyright © Mr.lu
<
2025年5月
>
日
一
二
三
四
五
六
27
28
29
30
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
1
2
3
4
5
6
7
導(dǎo)航
BlogJava
首頁(yè)
新隨筆
聯(lián)系
聚合
管理
統(tǒng)計(jì)
隨筆 - 29
文章 - 25
評(píng)論 - 26
引用 - 0
常用鏈接
我的隨筆
我的文章
我的評(píng)論
我的參與
最新評(píng)論
留言簿
(2)
給我留言
查看公開(kāi)留言
查看私人留言
隨筆檔案
2016年1月 (1)
2015年12月 (2)
2015年4月 (1)
2015年3月 (1)
2014年12月 (1)
2013年12月 (1)
2013年8月 (7)
2013年5月 (1)
2013年4月 (4)
2013年3月 (4)
2012年11月 (1)
2012年10月 (4)
2012年9月 (1)
文章檔案
2016年2月 (1)
2014年12月 (1)
2014年5月 (1)
2014年4月 (1)
2013年4月 (1)
2013年3月 (1)
2012年9月 (5)
2012年6月 (1)
2011年6月 (2)
2011年4月 (5)
2011年3月 (6)
搜索
最新評(píng)論
1.?re: textarea中輸入換行、空格等,以正確的格式后臺(tái)存儲(chǔ)和前臺(tái)顯示
我了
個(gè)去
什么
--淡淡的
2.?re: textarea中輸入換行、空格等,以正確的格式后臺(tái)存儲(chǔ)和前臺(tái)顯示
法規(guī)和法規(guī)和法規(guī)和符合人體后對(duì)符合人體會(huì)讓他的返回任何人的融合和的個(gè)人各的人格的若干的負(fù)荷的任何
--的發(fā)貨的費(fèi)
3.?re: JPA學(xué)習(xí)筆記
非常實(shí)用
--劉高潮
4.?re: textarea中輸入換行、空格等,以正確的格式后臺(tái)存儲(chǔ)和前臺(tái)顯示[未登錄](méi)
啊啊啊啊 啊啊啊啊
啊啊啊啊
--1
5.?re: JPA學(xué)習(xí)筆記
評(píng)論內(nèi)容較長(zhǎng),點(diǎn)擊標(biāo)題查看
--zuidaima
閱讀排行榜
1.?JPA學(xué)習(xí)筆記(20245)
2.?jQuery MiniUI學(xué)習(xí)(轉(zhuǎn)載)(11720)
3.?獲取八位UUID標(biāo)識(shí)碼(3380)
4.?將私有的jar包導(dǎo)入到maven本地庫(kù)(1604)
5.?從url獲取圖片(1430)
評(píng)論排行榜
1.?JPA學(xué)習(xí)筆記(3)
2.?從url獲取圖片(0)
3.?webservice axis2學(xué)習(xí)(轉(zhuǎn)載地址)(0)
4.?jQuery MiniUI學(xué)習(xí)(轉(zhuǎn)載)(0)
5.?jquery ajax范例(0)
主站蜘蛛池模板:
华坪县
|
宁陕县
|
柞水县
|
海安县
|
茌平县
|
西乡县
|
石柱
|
东城区
|
泗洪县
|
泸州市
|
叶城县
|
铜川市
|
年辖:市辖区
|
张家港市
|
周口市
|
洞头县
|
焦作市
|
汉沽区
|
云南省
|
浦江县
|
旬阳县
|
宿迁市
|
天峻县
|
瑞昌市
|
左贡县
|
游戏
|
云霄县
|
邓州市
|
手游
|
和林格尔县
|
蒲城县
|
乌鲁木齐市
|
沙坪坝区
|
九龙城区
|
吕梁市
|
汽车
|
鹤庆县
|
阿坝县
|
剑川县
|
龙山县
|
余干县
|