搜索
简帛阁>技术文章>Apache FileUpload的两种上传方式介绍及应用

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

环境
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://stackoverflow.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
环境:tomcat56commmonsfileupload13jarcommmonsio24jarJSP编码:UTF8临时文件夹:fileupload/tmp相对于网站根目录上传文件保存位置:file
文件上传方法主要目前有两个常用,一个是SmartUpload,一个是ApacheCommonsfileupload我们这里主要介绍下第二个用法,首先要上传文件,注意几个问题:1form表单内
本文通过实例来介绍如何使用commonsfileuploadjar,Apachecommonsfileuploadjar可方便实现文件上传功能,具体内容如下将Apachecommonsfile
话不多说了,直奔主题了。需要两个jar包:commonsfileuploadjarCommonsIOjar包(本文使用commonsio24jar)利用Servlet来实现文件上传。package
QueryUploadify+ApacheFileupload异步上传文件示例1、可以限制上传文件大小和类型,理论上任何类型文件都可以上传(自己根据api配置即可);2、后台使用Apachecom
一使用apachefileupload处理文件上传框架:是指将用户经常处理业务进行一个代码封装。让用户可以方便调用。目前文件上传(框架)组件:ApachefileuploadOrialiy–CO
后台代码:publicstringconnstrserver12813113;databasetest;uidsa;pwdpass;protectedvoidPage_Load(objectsende
FileUpload是国外一个纯javascript写大文件上传组件,该组件支持分片上传,断点续传,多文件等功能。下面就为大家分享FileUpload上传组件自定义模板(FineUploaderBa
S代码如下:functionCheckFile(obj){vararray=newArray('gif','jpeg','png','jpg');//可以上传文件类型if(objvalue==''
Java进阶之FileUpload完成上传实例FileUpload是Apachecommons下面一个子项目,用来实现Java项目下文件上传功能,常见文件上传还有SmartUpload,Ser