Greedy的工作室
cuore
BlogJava
首頁
新隨筆
新文章
聯系
聚合
管理
posts - 23,comments - 7,trackbacks - 0
<
2008年11月
>
日
一
二
三
四
五
六
26
27
28
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
30
1
2
3
4
5
6
常用鏈接
我的隨筆
我的評論
我的參與
最新評論
留言簿
(1)
給我留言
查看公開留言
查看私人留言
隨筆分類
CSS(1)
Database(1)
Java技術(9)
JS(2)
Jsp(2)
Struts(3)
Tomcat(1)
隨筆檔案
2009年3月 (1)
2008年11月 (5)
2008年10月 (5)
2008年9月 (2)
2008年7月 (1)
2008年6月 (9)
搜索
最新評論
閱讀排行榜
1.?C++與Java基本數據類型比較(1496)
2.?tomcat更改端口 (1490)
3.?Apache Commons fileUpload實現文件上傳(1437)
4.?禁止復制代碼禁止保持禁止查看源文件的代碼保護和破解(1146)
5.?meta http-equiv 用法簡介(484)
評論排行榜
1.?java 連接數據庫(0)
2.?禁止復制代碼禁止保持禁止查看源文件的代碼保護和破解(0)
3.?文件下載(0)
4.?文件壓縮(0)
5.?C++與Java基本數據類型比較(0)
Apache Commons fileUpload實現文件上傳
Apache
的
commons-fileupload.jar
可方便的實現文件的上傳功能,本文通過實例來介紹如何使用
commons-fileupload.jar
。
將
Apache
的
commons-fileupload.jar
放在應用程序的
WEB-INF\lib
下
,即可使用。下面舉例介紹如何使用它的文件上傳功能。
用的
fileUpload
版本為
1.2
,環境為
Eclipse3.3+MyEclipse6.0
。
FileUpload
是基于
Commons IO
的,所以在進入項目前先確定
Commons IO的
jar
包(本文使用
commons-io-1.3.2.jar
)在
WEB-INF\lib下。
示例1
最簡單的例子,通過
ServletFileUpload
靜態類來解析
Request
,工廠類
FileItemFactory
會對
mulipart
類的表單中的所有字段進行處理,不只是
file
字段。
getName
()得到文件名,
getString
()得到表單數據內容,
isFormField
()可判斷是否為普通的表單項。
demo1.html
<
html
>
<
head
>
<
meta
http-equiv
="Content-Type"
content
="text/html; charset=GB18030"
>
<
title
>
File upload
</
title
>
</
head
>
<
body
>
//必須是multipart的表單數據。
<
form
name
="myform"
action
="demo1.jsp"
method
="post"
enctype
="multipart/form-data"
>
Your name:
<
br
>
<
input
type
="text"
name
="name"
size
="15"
><
br
>
File:
<
br
>
<
input
type
="file"
name
="myfile"
><
br
>
<
br
>
<
input
type
="submit"
name
="submit"
value
="Commit"
>
</
form
>
</
body
>
</
html
>
demo1.jsp
<%
@ page language
=
"
java
"
contentType
=
"
text/html; charset=GB18030
"
pageEncoding
=
"
GB18030
"
%>
<%
@ page import
=
"
org.apache.commons.fileupload.*
"
%>
<%
@ page import
=
"
org.apache.commons.fileupload.servlet.*
"
%>
<%
@ page import
=
"
org.apache.commons.fileupload.disk.*
"
%>
<%
@ page import
=
"
java.util.*
"
%>
<!
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
>
<%
boolean
isMultipart
=
ServletFileUpload.isMultipartContent(request);
//
檢查輸入請求是否為multipart表單數據。
if
(isMultipart
==
true
) {
FileItemFactory factory
=
new
DiskFileItemFactory();
//
為該請求創建一個DiskFileItemFactory對象,通過它來解析請求。執行解析后,所有的表單項目都保存在一個List中。
ServletFileUpload upload
=
new
ServletFileUpload(factory);
List
<
FileItem
>
items
=
upload.parseRequest(request);
Iterator
<
FileItem
>
itr
=
items.iterator();
while
(itr.hasNext()) {
FileItem item
=
(FileItem) itr.next();
//
檢查當前項目是普通表單項目還是上傳文件。
if
(item.isFormField()) {
//
如果是普通表單項目,顯示表單內容。
String
fieldName
=
item.getFieldName();
if
(fieldName.equals(
"
name
"
))
//
對應demo1.html中type
=
"
text
"
name
=
"
name
"
out.print(
"
the field name is
"
+
item.getString());
//
顯示表單內容。
out.print(
"
<br>
"
);
}
else
{
//
如果是上傳文件,顯示文件名。
out.print(
"
the upload file name is
"
+
item.getName());
out.print(
"
<br>
"
);
}
}
}
else
{
out.print(
"
the enctype must be multipart/form-data
"
);
}
%>
<
html
>
<
head
>
<
meta
http-equiv
="Content-Type"
content
="text/html; charset=GB18030"
>
<
title
>
File upload
</
title
>
</
head
>
<
body
>
</
body
>
</
html
>
結果:
the field name isjeff
the upload file name isD:\C語言考試樣題\作業題.rar
示例
2
上傳兩個文件到指定的目錄。
demo2.html
<
html
>
<
head
>
<
meta
http-equiv
="Content-Type"
content
="text/html; charset=GB18030"
>
<
title
>
File upload
</
title
>
</
head
>
<
body
>
<
form
name
="myform"
action
="demo2.jsp"
method
="post"
enctype
="multipart/form-data"
>
File1:
<
br
>
<
input
type
="file"
name
="myfile"
><
br
>
File2:
<
br
>
<
input
type
="file"
name
="myfile"
><
br
>
<
br
>
<
input
type
="submit"
name
="submit"
value
="Commit"
>
</
form
>
</
body
>
</
html
>
demo2.jsp
<%
@ page language
=
"
java
"
contentType
=
"
text/html; charset=GB18030
"
pageEncoding
=
"
GB18030
"
%>
<%
@ page import
=
"
org.apache.commons.fileupload.*
"
%>
<%
@ page import
=
"
org.apache.commons.fileupload.servlet.*
"
%>
<%
@ page import
=
"
org.apache.commons.fileupload.disk.*
"
%>
<%
@ page import
=
"
java.util.*
"
%>
<%
@ page import
=
"
java.io.*
"
%>
<!
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
>
<%
String
uploadPath
=
"
D:\\temp
"
;
boolean
isMultipart
=
ServletFileUpload.isMultipartContent(request);
if
(isMultipart
==
true
){
try{
FileItemFactory factory
=
new
DiskFileItemFactory();
ServletFileUpload upload
=
new
ServletFileUpload(factory);
List
<
FileItem
>
items
=
upload.parseRequest(request);
//
得到所有的文件
Iterator
<
FileItem
>
itr
=
items.iterator();
while
(itr.hasNext()){
//
依次處理每個文件
FileItem item
=
(FileItem)itr.next();
String
fileName
=
item.getName();
//
獲得文件名,包括路徑
if
(fileName!
=
null
){
File fullFile
=
new
File(item.getName());
File savedFile
=
new
File(uploadPath,fullFile.getName());
item.write(savedFile);
}
}
out.print(
"
upload succeed
"
);
}
catch(Exception e){
e.printStackTrace();
}
}
else
{
out.println(
"
the enctype must be multipart/form-data
"
);
}
%>
<
html
>
<
head
>
<
meta
http-equiv
="Content-Type"
content
="text/html; charset=GB18030"
>
<
title
>
File upload
</
title
>
</
head
>
<
body
>
</
body
>
</
html
>
示例
3
上傳一個文件到指定的目錄,并限定文件大小。
demo3.html
<
html
>
<
head
>
<
meta
http-equiv
="Content-Type"
content
="text/html; charset=GB18030"
>
<
title
>
File upload
</
title
>
</
head
>
<
body
>
<
form
name
="myform"
action
="demo3.jsp"
method
="post"
enctype
="multipart/form-data"
>
File:
<
br
>
<
input
type
="file"
name
="myfile"
><
br
>
<
br
>
<
input
type
="submit"
name
="submit"
value
="Commit"
>
</
form
>
</
body
>
</
html
>
demo3.jsp
<%
@ page language
=
"
java
"
contentType
=
"
text/html; charset=GB18030
"
pageEncoding
=
"
GB18030
"
%>
<%
@ page import
=
"
org.apache.commons.fileupload.*
"
%>
<%
@ page import
=
"
org.apache.commons.fileupload.servlet.*
"
%>
<%
@ page import
=
"
org.apache.commons.fileupload.disk.*
"
%>
<%
@ page import
=
"
java.util.*
"
%>
<%
@ page import
=
"
java.io.*
"
%>
<!
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
>
<%
File uploadPath
=
new
File(
"
D:\\temp
"
);
//
上傳文件目錄
if
(!uploadPath.exists()) {
uploadPath.mkdirs();
}
//
臨時文件目錄
File tempPathFile
=
new
File(
"
d:\\temp\\buffer\\
"
);
if
(!tempPathFile.exists()) {
tempPathFile.mkdirs();
}
try {
//
Create a factory
for
disk
-
based file items
DiskFileItemFactory factory
=
new
DiskFileItemFactory();
//
Set
factory constraints
factory.setSizeThreshold(
4096
);
//
設置緩沖區大小,這里是4kb
factory.setRepository(tempPathFile);
//
設置緩沖區目錄
//
Create a
new
file upload handler
ServletFileUpload upload
=
new
ServletFileUpload(factory);
//
Set
overall request size constraint
upload.setSizeMax(
4194304
);
//
設置最大文件尺寸,這里是4MB
List
<
FileItem
>
items
=
upload.parseRequest(request);
//
得到所有的文件
Iterator
<
FileItem
>
i
=
items.iterator();
while
(i.hasNext()) {
FileItem fi
=
(FileItem) i.next();
String
fileName
=
fi.getName();
if
(fileName !
=
null
) {
File fullFile
=
new
File(fi.getName());
File savedFile
=
new
File(uploadPath, fullFile
.getName());
fi.write(savedFile);
}
}
out.print(
"
upload succeed
"
);
} catch (Exception e) {
e.printStackTrace();
}
%>
<
html
>
<
head
>
<
meta
http-equiv
="Content-Type"
content
="text/html; charset=GB18030"
>
<
title
>
File upload
</
title
>
</
head
>
<
body
>
</
body
>
</
html
>
示例
4
利用
Servlet
來實現文件上傳
Upload.java
package
com.zj.sample;
import
java.io.File;
import
java.io.IOException;
import
java.util.Iterator;
import
java.util.List;
import
javax.servlet.ServletException;
import
javax.servlet.http.HttpServlet;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
org.apache.commons.fileupload.FileItem;
import
org.apache.commons.fileupload.disk.DiskFileItemFactory;
import
org.apache.commons.fileupload.servlet.ServletFileUpload;
@SuppressWarnings(
"
serial
"
)
public
class
Upload
extends
HttpServlet
{
private
String uploadPath
=
"
D:\\temp
"
;
//
上傳文件的目錄
private
String tempPath
=
"
d:\\temp\\buffer\\
"
;
//
臨時文件目錄
File tempPathFile;
@SuppressWarnings(
"
unchecked
"
)
public
void
doPost(HttpServletRequest request, HttpServletResponse response)
throws
IOException, ServletException
{
try
{
//
Create a factory for disk-based file items
DiskFileItemFactory factory
=
new
DiskFileItemFactory();
//
Set factory constraints
factory.setSizeThreshold(
4096
);
//
設置緩沖區大小,這里是4kb
factory.setRepository(tempPathFile);
//
設置緩沖區目錄
//
Create a new file upload handler
ServletFileUpload upload
=
new
ServletFileUpload(factory);
//
Set overall request size constraint
upload.setSizeMax(
4194304
);
//
設置最大文件尺寸,這里是4MB
List
<
FileItem
>
items
=
upload.parseRequest(request);
//
得到所有的文件
Iterator
<
FileItem
>
i
=
items.iterator();
while
(i.hasNext())
{
FileItem fi
=
(FileItem) i.next();
String fileName
=
fi.getName();
if
(fileName
!=
null
)
{
File fullFile
=
new
File(fi.getName());
File savedFile
=
new
File(uploadPath, fullFile.getName());
fi.write(savedFile);
}
}
System.out.print(
"
upload succeed
"
);
}
catch
(Exception e)
{
//
可以跳轉出錯頁面
e.printStackTrace();
}
}
public
void
init()
throws
ServletException
{
File uploadFile
=
new
File(uploadPath);
if
(
!
uploadFile.exists())
{
uploadFile.mkdirs();
}
File tempPathFile
=
new
File(tempPath);
if
(
!
tempPathFile.exists())
{
tempPathFile.mkdirs();
}
}
}
demo4.html
<
html
>
<
head
>
<
meta
http-equiv
="Content-Type"
content
="text/html; charset=GB18030"
>
<
title
>
File upload
</
title
>
</
head
>
<
body
>
// action="fileupload"對應web.xml中
<
servlet-mapping
>
中
<
url-pattern
>
的設置.
<
form
name
="myform"
action
="fileupload"
method
="post"
enctype
="multipart/form-data"
>
File:
<
br
>
<
input
type
="file"
name
="myfile"
><
br
>
<
br
>
<
input
type
="submit"
name
="submit"
value
="Commit"
>
</
form
>
</
body
>
</
html
>
web.xml
<
servlet
>
<
servlet-name
>
Upload
</
servlet-name
>
<
servlet-class
>
com.zj.sample.Upload
</
servlet-class
>
</
servlet
>
<
servlet-mapping
>
<
servlet-name
>
Upload
</
servlet-name
>
<
url-pattern
>
/fileupload
</
url-pattern
>
</
servlet-mapping
>
以上轉帖
本文出自 “
子 孑
” 博客,請務必保留此出處
http://zhangjunhd.blog.51cto.com/113473/18331
link
posted on 2008-11-06 12:11
greedy
閱讀(1437)
評論(0)
編輯
收藏
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發表評論。
網站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
Copyright ©2025 greedy Powered By
博客園
模板提供:
滬江博客
主站蜘蛛池模板:
无码人妻丰满熟妇区免费
|
久久夜色精品国产亚洲av
|
亚洲最大免费视频网
|
国产综合免费精品久久久
|
亚洲第一成年网站视频
|
亚洲狠狠ady亚洲精品大秀
|
亚洲精品中文字幕乱码三区
|
亚洲成人一区二区
|
国产精品二区三区免费播放心
|
在线a免费观看最新网站
|
99久久99这里只有免费的精品
|
美女被爆羞羞网站免费
|
亚洲成a人片在线观看天堂无码
|
亚洲精品中文字幕无乱码
|
久久青草亚洲AV无码麻豆
|
亚洲人成网站在线观看青青
|
日本无卡码免费一区二区三区
|
青娱乐免费视频在线观看
|
99xxoo视频在线永久免费观看
|
十八禁在线观看视频播放免费
|
在线播放国产不卡免费视频
|
国产亚洲综合久久
|
色综合久久精品亚洲国产
|
亚洲国产成人久久精品软件
|
亚洲日本VA午夜在线影院
|
亚洲日韩AV一区二区三区四区
|
亚洲中文字幕久久精品蜜桃
|
精品亚洲成在人线AV无码
|
亚洲第一永久在线观看
|
亚洲神级电影国语版
|
亚洲国产视频一区
|
亚洲日本乱码一区二区在线二产线
|
337p日本欧洲亚洲大胆精品555588
|
亚洲大片在线观看
|
亚洲黄色在线网站
|
亚洲制服丝袜一区二区三区
|
久久精品国产亚洲AV忘忧草18
|
亚洲一线产区二线产区区
|
亚洲AV成人片无码网站
|
污网站在线观看免费
|
一个人看的在线免费视频
|