heting
BlogJava
::
首頁
::
新隨筆
::
聯(lián)系
::
聚合
::
管理
::
40 隨筆 :: 9 文章 :: 45 評論 :: 0 Trackbacks
<
2015年3月
>
日
一
二
三
四
五
六
22
23
24
25
26
27
28
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
常用鏈接
我的隨筆
我的評論
我的參與
最新評論
留言簿
(3)
給我留言
查看公開留言
查看私人留言
隨筆分類
c#范例(4)
(rss)
java范例
(rss)
js(3)
(rss)
linux(1)
(rss)
WebSphere(1)
(rss)
數(shù)據(jù)庫(3)
(rss)
隨筆檔案
2010年10月 (1)
2010年8月 (5)
2010年3月 (2)
2009年11月 (2)
2009年9月 (1)
2009年8月 (1)
2009年7月 (2)
2009年5月 (1)
2009年4月 (3)
2009年3月 (4)
2009年2月 (1)
2009年1月 (3)
2008年12月 (2)
2008年11月 (7)
2008年10月 (4)
2008年9月 (1)
文章檔案
2008年9月 (9)
搜索
最新評論
1.?re: struts2+freemarker中防止表單的重復(fù)提交token
@51互聯(lián)云-濟(jì)南程序猿
你什么都不懂,瞎說
--人
2.?re: struts2+freemarker中防止表單的重復(fù)提交token
沒關(guān)系,大家好,什么都沒有
--人
3.?re: struts2+freemarker中防止表單的重復(fù)提交token
套頭
--人
4.?re: struts2+freemarker中防止表單的重復(fù)提交token
評論內(nèi)容較長,點(diǎn)擊標(biāo)題查看
--51互聯(lián)云-濟(jì)南程序猿
5.?re: struts1文件上傳和下載
評論內(nèi)容較長,點(diǎn)擊標(biāo)題查看
--zuidaima
閱讀排行榜
1.?struts1文件上傳和下載(21131)
2.?javax.naming.CommunicationException 的一個相關(guān)異常(已解決)(11551)
3.?自己寫的一個c#winform打印類(8505)
4.?C# 與 C++ 數(shù)據(jù)類型比較及結(jié)構(gòu)體轉(zhuǎn)換 (7217)
5.?WebSphere7.0 上部署struts2 找不到用于處理 JSP 的擴(kuò)展處理器(2983)
評論排行榜
1.?struts1文件上傳和下載(23)
2.?自己寫的一個c#winform打印類(8)
3.?struts2+freemarker中防止表單的重復(fù)提交token(6)
4.?Ireport在瀏覽器中的顯示代碼(2)
5.?EJ3.0將EJB程序和WEb程序發(fā)布到weblogic10.3是出現(xiàn)的錯誤weblogic.wsee.async.AsyncResponseBean(2)
struts1文件上傳和下載
FileAction
package
com.action;
import
org.apache.struts.action.
*
;
import
javax.servlet.http.
*
;
import
com.actionForm.FileActionForm;
import
org.apache.struts.actions.DispatchAction;
import
java.util.Date;
import
java.text.
*
;
import
org.apache.struts.upload.FormFile;
import
java.io.
*
;
import
java.net.URLEncoder;
import
com.dao.
*
;
public
class
FileAction
extends
DispatchAction
{
private
JDBConnection connection
=
new
JDBConnection();
//
以下方法實(shí)現(xiàn)文件的上傳
public
ActionForward upLoadFile(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws
Exception
{
ActionForward forward
=
null
;
Date date
=
new
Date();
FileActionForm fileActionForm
=
(FileActionForm) form;
//
FormFile用于指定存取文件的類型
FormFile file
=
fileActionForm.getFile();
//
獲取當(dāng)前的文件
//
獲得系統(tǒng)的絕對路徑 String dir = servlet.getServletContext().getRealPath("/image");
//
我上傳的文件沒有放在服務(wù)器上。而是存在D:D:\\loadfile\\temp\\
String dir
=
"
D:\\loadfile\\temp\\
"
;
int
i
=
0
;
String type
=
file.getFileName();
while
(i
!=-
1
)
{
//
找到上傳文件的類型的位置,這個地方的是'.'
i
=
type.indexOf(
"
.
"
);
/**/
/*
System.out.println(i);
*/
/**/
/*
截取上傳文件的后綴名,此時得到了文件的類型
*/
type
=
type.substring(i
+
1
);
}
//
限制上傳類型為jpg,txt,rar;
if
(
!
type.equals(
"
jpg
"
)
&&
!
type.equals(
"
txt
"
)
&&
!
type.equals(
"
bmp
"
))
{
//
當(dāng)上傳的類型不為上述類型時,跳轉(zhuǎn)到錯誤頁面。
forward
=
mapping.findForward(
"
error
"
);
}
else
{
//
將上傳時間加入文件名(這個地方的是毫秒數(shù))
String times
=
String.valueOf(date.getTime());
//
組合成 time.type
String fname
=
times
+
"
.
"
+
type;
//
InInputStream是用以從特定的資源讀取字節(jié)的方法。
InputStream streamIn
=
file.getInputStream();
//
創(chuàng)建讀取用戶上傳文件的對象
//
得到是字節(jié)數(shù),即byte,我們可以直接用file.getFileSize(),也可以在創(chuàng)建讀取對象時用streamIn.available();
//
int ok=streamIn.available();
int
ok
=
file.getFileSize();
String strFee
=
null
;
//
這個地方是處理上傳的為M單位計算時,下一個是以kb,在下一個是byte;
if
(ok
>=
1024
*
1024
)
{
float
ok1
=
(((
float
)ok)
/
1024f
/
1024f);
DecimalFormat myformat1
=
new
DecimalFormat(
"
0.00
"
);
strFee
=
myformat1.format(ok1)
+
"
M
"
;
System.out.println(strFee
+
"
M
"
);
}
else
if
(ok
>
1024
&&
ok
<=
1024
*
1024
)
{
double
ok2
=
((
double
)ok)
/
1024
;
DecimalFormat myformat2
=
new
DecimalFormat(
"
0.00
"
);
strFee
=
myformat2.format(ok2)
+
"
kb
"
;
System.out.println(strFee
+
"
kb
"
);
}
else
if
(ok
<
1024
)
{
System.out.println(
"
aaaaaaaaa
"
);
strFee
=
String.valueOf(ok)
+
"
byte
"
;
System.out.println(strFee);
}
System.out.println( streamIn.available()
+
"
文件大小byte
"
);
//
這個是io包下的上傳文件類
File uploadFile
=
new
File(dir);
//
指定上傳文件的位置
if
(
!
uploadFile.exists()
||
uploadFile
==
null
)
{
//
判斷指定路徑dir是否存在,不存在則創(chuàng)建路徑
uploadFile.mkdirs();
}
//
上傳的路徑+文件名
String path
=
uploadFile.getPath()
+
"
\\
"
+
fname;
//
OutputStream用于向某個目標(biāo)寫入字節(jié)的抽象類,這個地方寫入目標(biāo)是path,通過輸出流FileOutputStream去寫
OutputStream streamOut
=
new
FileOutputStream(path);
int
bytesRead
=
0
;
byte
[] buffer
=
new
byte
[
8192
];
//
將數(shù)據(jù)讀入byte數(shù)組的一部分,其中讀入字節(jié)數(shù)的最大值是8192,讀入的字節(jié)將存儲到,buffer[0]到buffer[0+8190-1]的部分中
//
streamIn.read方法返回的是實(shí)際讀取字節(jié)數(shù)目.如果讀到末尾則返回-1.如果bytesRead返回為0則表示沒有讀取任何字節(jié)。
while
((bytesRead
=
streamIn.read(buffer,
0
,
8192
))
!=
-
1
)
{
//
寫入buffer數(shù)組的一部分,從buf[0]開始寫入并寫入bytesRead個字節(jié),這個write方法將發(fā)生阻塞直至字節(jié)寫入完成。
streamOut.write(buffer,
0
, bytesRead);
}
//
關(guān)閉輸出輸入流,銷毀File流。
streamOut.close();
streamIn.close();
file.destroy();
String paths
=
path;
System.out.println(paths);
String fileName
=
Chinese.toChinese(fileActionForm.getFileName());
//
獲取文件的名稱
//
String fileSize = String.valueOf(file.getFileSize());
String fileDate
=
DateFormat.getDateInstance().format(date);
String sql
=
"
insert into tb_file values('
"
+
fileName
+
"
','
"
+
strFee
+
"
','
"
+
fileDate
+
"
','
"
+
paths
+
"
')
"
;
connection.executeUpdate(sql);
connection.closeConnection();
forward
=
mapping.findForward(
"
upLoadFileResult
"
);
}
return
forward;
}
//
實(shí)現(xiàn)文件的下載
public
ActionForward downFile(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws
Exception
{
String path
=
request.getParameter(
"
path
"
);
System.out.println(path
+
"
111
"
);
BufferedInputStream bis
=
null
;
BufferedOutputStream bos
=
null
;
OutputStream fos
=
null
;
InputStream fis
=
null
;
//
如果是從服務(wù)器上取就用這個獲得系統(tǒng)的絕對路徑方法。 String filepath = servlet.getServletContext().getRealPath("/" + path);
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(path,
"
utf-8
"
));
int
bytesRead
=
0
;
//
這個地方的同上傳的一樣。我就不多說了,都是用輸入流進(jìn)行先讀,然后用輸出流去寫,唯一不同的是我用的是緩沖輸入輸出流
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
;
}
}
FileActionForm
package
com.actionForm;
import
org.apache.struts.action.
*
;
import
org.apache.struts.upload.
*
;
public
class
FileActionForm
extends
ActionForm
{
private
String fileName;
//
上傳文件的名稱
private
String fileSize;
//
上傳文件的大小
private
String filePath;
//
上傳文件到服務(wù)器的路徑
private
String fileDate;
//
上傳文件的日期
private
FormFile file;
//
上傳文件
public
String getFileName()
{
return
fileName;
}
public
FormFile getFile()
{
return
file;
}
public
String getFileSize()
{
return
fileSize;
}
public
String getFilePath()
{
return
filePath;
}
public
String getFileDate()
{
return
fileDate;
}
public
void
setFileName(String fileName)
{
this
.fileName
=
fileName;
}
public
void
setFile(FormFile file)
{
this
.file
=
file;
}
public
void
setFileSize(String fileSize)
{
this
.fileSize
=
fileSize;
}
public
void
setFilePath(String filePath)
{
this
.filePath
=
filePath;
}
public
void
setFileDate(String fileDate)
{
this
.fileDate
=
fileDate;
}
}
index.jsp
<
table
width
="264"
height
="81"
border
="0"
align
="center"
cellpadding
="0"
cellspacing
="0"
>
<
tr
>
<
td
width
="115"
rowspan
="4"
align
="center"
><
img
src
="<%=form.getFilePath()%>"
width
="100"
height
="100"
></
td
>
<
td
width
="133"
align
="center"
>
圖片名稱:
<%
=
form.getFileName()
%>
</
td
>
</
tr
>
<
tr
align
="center"
>
<
td
>
圖片大小:
<%
=
form.getFileSize()
%>
</
td
>
</
tr
>
<
tr
align
="center"
>
<
td
>
上傳日期:
<%
=
form.getFileDate()
%>
</
td
>
</
tr
>
<
tr
>
<
td
align
="center"
><
a
href
="fileAction.do?method=downFile&path=<%=form.getFilePath()%>"
><
img
src
="priture/bottond.jpg"
></
a
>
</
td
>
</
tr
>
</
table
>
<
html:form
action
="fileAction.do?method=upLoadFile"
enctype
="multipart/form-data"
onsubmit
="return Mycheck()"
>
<
table
height
="52"
border
="0"
align
="center"
cellpadding
="0"
cellspacing
="0"
>
<
tr
align
="center"
>
<
td
width
="60"
height
="26"
>
圖片名稱:
</
td
>
<
td
width
="160"
>
<
html:text
property
="fileName"
/>
</
td
>
<
td
width
="60"
>
圖片路徑:
</
td
>
<
td
width
="198"
>
<
html:file
property
="file"
/>
</
td
>
</
tr
>
<
tr
align
="right"
>
<
td
height
="26"
colspan
="4"
>
<
html:submit
>
上傳
</
html:submit
>
</
td
>
</
tr
>
</
table
>
</
html:form
>
struts-config.xml
<?
xml version="1.0" encoding="UTF-8"
?>
<!
DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"
>
<
struts-config
>
<
form-beans
>
<
form-bean
name
="fileActionForm"
type
="com.actionForm.FileActionForm"
/>
</
form-beans
>
<
action-mappings
>
<
action
name
="fileActionForm"
parameter
="method"
path
="/fileAction"
scope
="request"
type
="com.action.FileAction"
validate
="true"
>
<
forward
name
="upLoadFileResult"
path
="/result.jsp"
/>
<
forward
name
="error"
path
="/fail.jsp"
></
forward
>
</
action
>
</
action-mappings
>
<
message-resources
parameter
="ApplicationResources"
/>
</
struts-config
>
posted on 2009-03-04 10:36
賀挺
閱讀(21131)
評論(23)
編輯
收藏
評論
#
re: struts1文件上傳和下載[未登錄]
2010-04-08 09:28
qq
very perfit!!!
回復(fù)
更多評論
#
re: struts1文件上傳和下載
2010-06-30 17:03
ai
能給個源碼例子嗎
回復(fù)
更多評論
#
re: struts1文件上傳和下載
2010-10-31 11:15
路過
頂樓主!
回復(fù)
更多評論
#
re: struts1文件上傳和下載
2011-06-29 14:51
naoskang
頂樓主,非常好用
回復(fù)
更多評論
#
re: struts1文件上傳和下載[未登錄]
2011-09-13 09:06
even
思路一下就清晰了
回復(fù)
更多評論
#
re: struts1文件上傳和下載
2011-12-19 20:33
asd
上傳文件時??要求客戶去填寫???文件名????
回復(fù)
更多評論
#
re: struts1文件上傳和下載
2011-12-19 20:34
asd
如果不那樣寫的話。會亂碼OKlahoma???
回復(fù)
更多評論
#
re: struts1文件上傳和下載[未登錄]
2012-01-06 16:58
chen
非常感謝,轉(zhuǎn)了、、、、、、
回復(fù)
更多評論
#
re: struts1文件上傳和下載
2012-03-06 11:10
安萬軍
JDBConnection這個類是那個包的?
回復(fù)
更多評論
#
re: struts1文件上傳和下載
2012-03-06 11:27
安萬軍
@安萬軍
看錯了!
回復(fù)
更多評論
#
re: struts1文件上傳和下載
2012-06-30 21:41
lsc
@安萬軍
我沒看懂這個JDBConnection,有問題,能指點(diǎn)一下嗎
回復(fù)
更多評論
#
re: struts1文件上傳和下載
2012-06-30 22:35
lsc
@安萬軍
是數(shù)據(jù)庫連接嗎?自己寫個類行不行?
回復(fù)
更多評論
#
re: struts1文件上傳和下載
2012-07-31 09:33
sinianxue
這里讓用戶填寫名稱和路徑不合理,一般用戶是不會同意這么做的
回復(fù)
更多評論
#
re: struts1文件上傳和下載[未登錄]
2012-09-05 19:41
test
這種方法上傳1m以上的doc文件會出現(xiàn)錯誤
回復(fù)
更多評論
#
re: struts1文件上傳和下載[未登錄]
2012-11-30 22:35
aaa
不能直接用
回復(fù)
更多評論
#
re: struts1文件上傳和下載
2012-12-13 15:25
lvjun
垃圾
回復(fù)
更多評論
#
re: struts1文件上傳和下載
2013-03-21 11:48
000
000
回復(fù)
更多評論
#
re: struts1文件上傳和下載
2013-06-17 20:50
yangther
asdasd
回復(fù)
更多評論
#
re: struts1文件上傳和下載
2013-06-17 20:50
yangther
Struts上傳
回復(fù)
更多評論
#
re: struts1文件上傳和下載[未登錄]
2013-06-28 15:02
jBoy
下載的是時候,沒有考慮過中文亂碼的問題。
回復(fù)
更多評論
#
re: struts1文件上傳和下載
2013-12-12 14:48
是
頂!!!!!!!!!!!!!!!!!!
回復(fù)
更多評論
#
re: struts1文件上傳和下載[未登錄]
2014-01-22 17:38
ggg
ssgsg
回復(fù)
更多評論
#
re: struts1文件上傳和下載
2015-03-24 18:12
zuidaima
java struts框架demo使用實(shí)例教程源代碼下載地址:
http://zuidaima.com/share/kstruts-p1-s1.htm
回復(fù)
更多評論
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發(fā)表評論。
網(wǎng)站導(dǎo)航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
Powered by:
BlogJava
Copyright © 賀挺
主站蜘蛛池模板:
嘉鱼县
|
庆阳市
|
丰镇市
|
北流市
|
乌海市
|
仁寿县
|
广汉市
|
共和县
|
临沧市
|
平顶山市
|
阿鲁科尔沁旗
|
马龙县
|
丰城市
|
南和县
|
耒阳市
|
曲阜市
|
清苑县
|
蕉岭县
|
西城区
|
开远市
|
读书
|
溧水县
|
巫山县
|
岳普湖县
|
德钦县
|
额敏县
|
论坛
|
车险
|
固阳县
|
股票
|
和硕县
|
鄂温
|
芒康县
|
达孜县
|
犍为县
|
新建县
|
扎兰屯市
|
普兰县
|
屏南县
|
达日县
|
郧西县
|