欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Apache FileUpload的两种上传方式介绍及应用

程序员文章站 2024-02-10 10:52:10
环境: tomcat5.6 commmons-fileupload-1.3.jar commmons-io-2.4.jar jsp 编码:utf-8 临时文件夹:fileu...
环境
tomcat5.6
commmons-fileupload-1.3.jar
commmons-io-2.4.jar
jsp
编码:utf-8
临时文件夹:fileupload/tmp相对于网站根目录
上传文件保存位置:fileupload
traditional api上传方式
//fileload01.htm
复制代码 代码如下:

<meta http-equiv="content-type" content="text/html; charset=utf-8">
<html>
<body>
<form method="post" enctype="multipart/form-data" action="traditionalapi.jsp">
file to upload: <input type="file" name="file" size="40"><br/>
<input type="submit" value="press"> to upload the file!
</form>
</body>
</html>

//traditionalapi.jsp
复制代码 代码如下:

<%@page contenttype="text/html;charset=utf-8" pageencoding="utf-8" language="java"%>
<%@page import="java.io.file"%>
<%@page import="java.util.list"%>
<%@page import="org.apache.commons.fileupload.*"%>
<%@page import="org.apache.commons.fileupload.disk.diskfileitemfactory"%>
<%@page import="org.apache.commons.fileupload.servlet.servletfileupload"%>
<%
request.setcharacterencoding("utf-8");
// file less than 10kb will be store in memory, otherwise in file system.
final int threshold = 10240;
final file tmpdir = new file(getservletcontext().getrealpath("/") + "fileupload" + file.separator + "tmp");
final int maxrequestsize = 1024 * 1024 * 4; // 4mb
// check that we have a file upload request
if(servletfileupload.ismultipartcontent(request))
{
// create a factory for disk-based file items.
fileitemfactory factory = new diskfileitemfactory(threshold, tmpdir);

// create a new file upload handler
servletfileupload upload = new servletfileupload(factory);
// set overall request size constraint.
upload.setsizemax(maxrequestsize);
list<fileitem> items = upload.parserequest(request); // fileuploadexception
for(fileitem item : items)
{
if(item.isformfield()) //regular form field
{
string name = item.getfieldname();
string value = item.getstring();
%>
<h1><%=name%> --> <%=value%></h1>
<%
}
else
{ //file upload
string fieldname = item.getfieldname();
string filename = item.getname();
file uploadedfile = new file(getservletcontext().getrealpath("/") +
"fileupload" + file.separator + fieldname + "_" + filename);
item.write(uploadedfile);
%>
<h1>upload file <%=uploadedfile.getname()%> done!</h1>
<%
}
}
}
%>

streaming api上传方式
//fileupload02.htm
复制代码 代码如下:

<meta http-equiv="content-type" content="text/html; charset=utf-8">
<html>
<body>
<form method="post" enctype="multipart/form-data" action="streamingapi.jsp">
file to upload: <input type="file" name="file" size="40"><br/>
<input type="submit" value="press"> to upload the file!
</form>
</body>
</html>

//streamingapi.jsp
复制代码 代码如下:

<%@page contenttype="text/html;charset=utf-8" pageencoding="utf-8" language="java"%>
<%@page import="java.io.*"%>
<%@page import="java.util.list"%>
<%@page import="org.apache.commons.fileupload.*"%>
<%@page import="org.apache.commons.fileupload.util.streams"%>
<%@page import="org.apache.commons.fileupload.servlet.servletfileupload"%>
<%
request.setcharacterencoding("utf-8");
// check that we have a file upload request
if(servletfileupload.ismultipartcontent(request))
{
// create a new file upload handler
servletfileupload upload = new servletfileupload();

// parse the request
fileitemiterator iter = upload.getitemiterator(request);
while(iter.hasnext())
{
fileitemstream item = iter.next();
string fieldname = item.getfieldname();
inputstream is = item.openstream();
if(item.isformfield()) //regular form field
{
%>
<!-- read a fileitemstream's content into a string. -->
<h1><%=fieldname%> --> <%=streams.asstring(is)%></h1>
<%
}
else
{ //file upload
string filename = item.getname();
file uploadedfile = new file(getservletcontext().getrealpath("/") +
"fileupload" + file.separator + fieldname + "_" + filename);
outputstream os = new fileoutputstream(uploadedfile);
// write file to disk and close outputstream.
streams.copy(is, os, true);
%>
<h1>upload file <%=uploadedfile.getname()%> done!</h1>
<%
}
}
}
%>

traditional api vs streaming api
streaming api上传速度相对较快。因为它是利用内存保存上传的文件,节省了传统api将文件写入临时文件带来的开销。
可参考:
http://*.com/questions/11620432/apache-commons-fileupload-streaming-api
this page describes the traditional api of the commons fileupload library. the traditional api is a convenient approach. however, for ultimate performance, you might prefer the faster streaming api.
http://commons.apache.org/proper/commons-fileupload/using.html