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

所属分类: 网络编程 / JSP编程 阅读数: 1373
收藏 0 赞 0 分享
环境
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
更多精彩内容其他人还在看

jsp中使用frameset框架 边框固定不让更改边框的大小

有时候可能要对自己布局好的页面不让用户更改边框的大小,这样我们可以在frame里面添加noresize="noresize"属性就可以实现其中的功能
收藏 0 赞 0 分享

response.getWriter().write()向前台打印信息乱码问题解决

本节主要介绍了response.getWriter().write()向前台打印信息乱码问题解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

jsp页面中如何将时间戳字符串格式化为时间标签

本节主要介绍了jsp页面中如何将时间戳字符串格式化为时间标签,需要的朋友可以参考下
收藏 0 赞 0 分享

获取上一页面的URL和本页的URL的方法

本节主要介绍了获取上一页面的URL和本页的URL的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

window.top[_CACHE]实现多个jsp页面共享一个js对象

两个js页面要共享一个就js对象,想了半天用window.top['_CACHE']来存放这个变量,即可实现,不同Jsp页面直接的对象共享
收藏 0 赞 0 分享

通过过滤器(Filter)解决JSP的Post和Request中文乱码问题

这篇文章主要介绍了jsp中通过过滤器(Filter)解决JSP的Post和Request中文乱码问题的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

JSP页面的动态包含和静态包含示例及介绍

这篇文章主要介绍了JSP页面的动态包含和静态包含示例及介绍,本文讲解了它们的区别并给出了相应例子,需要的朋友可以参考下
收藏 0 赞 0 分享

JSP中实现判断客户端手机类型并跳转到app下载页面

这篇文章主要介绍了JSP中实现判断客户端手机类型并跳转到app下载页面,实现的原理,是检测浏览器的 USER-AGENT 这个header,然后根据正则表达式来确定客户端类型,需要的朋友可以参考下
收藏 0 赞 0 分享

jsp实现点击help打开chm文件

有个javaweb项目,需要在portal上面点击help即可打开“帮助.chm”文件,下面与大家分享下jsp如何打开chm文件
收藏 0 赞 0 分享

JSP自定义分页标签TAG全过程

这篇文章主要介绍了JSP自定义分页标签TAG全过程,比较实用,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多