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);
//
這個就就是彈出下載對話框的關鍵代碼
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);
//
將文件寫入服務器
}
bos.close();
stream.close();
DataTypeModel typeModel
=
typeDao.getDataTypeById(Integer.parseInt(type_id));
//
判斷是否已經存在
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
)
{
//
將資料信息保存進數據庫
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)
評論(0)
編輯
收藏
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發表評論。
網站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
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
導航
BlogJava
首頁
新隨筆
聯系
聚合
管理
統計
隨筆 - 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中輸入換行、空格等,以正確的格式后臺存儲和前臺顯示
法規和法規和法規和符合人體后對符合人體會讓他的返回任何人的融合和的個人各的人格的若干的負荷的任何
--的發貨的費
3.?re: JPA學習筆記
非常實用
--劉高潮
4.?re: textarea中輸入換行、空格等,以正確的格式后臺存儲和前臺顯示[未登錄]
啊啊啊啊 啊啊啊啊
啊啊啊啊
--1
5.?re: JPA學習筆記
評論內容較長,點擊標題查看
--zuidaima
閱讀排行榜
1.?JPA學習筆記(20244)
2.?jQuery MiniUI學習(轉載)(11720)
3.?獲取八位UUID標識碼(3380)
4.?將私有的jar包導入到maven本地庫(1604)
5.?從url獲取圖片(1430)
評論排行榜
1.?JPA學習筆記(3)
2.?從url獲取圖片(0)
3.?webservice axis2學習(轉載地址)(0)
4.?jQuery MiniUI學習(轉載)(0)
5.?jquery ajax范例(0)
主站蜘蛛池模板:
桦南县
|
常宁市
|
沙雅县
|
芦山县
|
温州市
|
石泉县
|
连云港市
|
琼结县
|
青龙
|
曲阳县
|
新平
|
石阡县
|
上高县
|
嫩江县
|
工布江达县
|
延庆县
|
施甸县
|
陆河县
|
苍南县
|
长顺县
|
通州市
|
迭部县
|
廊坊市
|
漯河市
|
攀枝花市
|
沭阳县
|
浦城县
|
潢川县
|
毕节市
|
葵青区
|
朝阳县
|
滕州市
|
韶山市
|
冕宁县
|
黄平县
|
资中县
|
方城县
|
孟州市
|
江都市
|
鸡西市
|
灵台县
|