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);
//
這個就就是彈出下載對話框的關(guān)鍵代碼
response.setHeader(
"
Content-disposition
"
,
"
attachment;filename=
"
+
URLEncoder.encode(data_name,
"
GBK
"
));
int
bytesRead
=
0
;
//
用輸入流進行先讀,然后用輸出流去寫
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());
//
建立一個上傳文件的輸出流
int
bytesRead
=
0
;
byte
[] buffer
=
new
byte
[
8192
];
while
((bytesRead
=
stream.read(buffer,
0
,
8192
))
!=
-
1
)
{
bos.write(buffer,
0
, bytesRead);
//
將文件寫入服務(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
)
{
//
將資料信息保存進數(shù)據(jù)庫
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
閱讀(198)
評論(0)
編輯
收藏
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發(fā)表評論。
網(wǎng)站導(dǎo)航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
Powered by:
BlogJava
Copyright © Mr.lu
<
2025年7月
>
日
一
二
三
四
五
六
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
8
9
導(dǎo)航
BlogJava
首頁
新隨筆
聯(lián)系
聚合
管理
統(tǒng)計
隨筆 - 29
文章 - 25
評論 - 26
引用 - 0
常用鏈接
我的隨筆
我的文章
我的評論
我的參與
最新評論
留言簿
(2)
給我留言
查看公開留言
查看私人留言
隨筆檔案
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)
搜索
最新評論
1.?re: textarea中輸入換行、空格等,以正確的格式后臺存儲和前臺顯示
我了
個去
什么
--淡淡的
2.?re: textarea中輸入換行、空格等,以正確的格式后臺存儲和前臺顯示
法規(guī)和法規(guī)和法規(guī)和符合人體后對符合人體會讓他的返回任何人的融合和的個人各的人格的若干的負(fù)荷的任何
--的發(fā)貨的費
3.?re: JPA學(xué)習(xí)筆記
非常實用
--劉高潮
4.?re: textarea中輸入換行、空格等,以正確的格式后臺存儲和前臺顯示[未登錄]
啊啊啊啊 啊啊啊啊
啊啊啊啊
--1
5.?re: JPA學(xué)習(xí)筆記
評論內(nèi)容較長,點擊標(biāo)題查看
--zuidaima
閱讀排行榜
1.?JPA學(xué)習(xí)筆記(20246)
2.?jQuery MiniUI學(xué)習(xí)(轉(zhuǎn)載)(11722)
3.?獲取八位UUID標(biāo)識碼(3383)
4.?將私有的jar包導(dǎo)入到maven本地庫(1607)
5.?從url獲取圖片(1433)
評論排行榜
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)
主站蜘蛛池模板:
峡江县
|
长阳
|
乌鲁木齐县
|
疏附县
|
安顺市
|
工布江达县
|
西华县
|
永德县
|
云龙县
|
攀枝花市
|
黄龙县
|
桂东县
|
聂荣县
|
岳阳市
|
云龙县
|
会东县
|
彭泽县
|
昔阳县
|
城市
|
克拉玛依市
|
韶关市
|
松江区
|
绥江县
|
休宁县
|
瑞安市
|
綦江县
|
磐安县
|
海丰县
|
米泉市
|
宣化县
|
隆子县
|
遂平县
|
芦溪县
|
中方县
|
万盛区
|
张掖市
|
镇沅
|
南木林县
|
儋州市
|
西乌珠穆沁旗
|
沾化县
|