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基本数据类型比较(1488)
2. tomcat更改端口 (1485)
3. Apache Commons fileUpload实现文件上传(1430)
4. 禁止复制代码禁止保持禁止查看源文件的代码保护和破解(1137)
5. meta http-equiv 用法简介(478)
评论排行榜
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
阅读(1430)
评论(0)
编辑
收藏
新用户注册
刷新评论列表
只有注册用户
登录
后才能发表评论。
网站导航:
博客园
IT新闻
知识库
C++博客
博问
管理