环境:
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
相关推荐:
什么是seo技术,什么是seo及seo的作用 ,AI倒放仓鼠
专业关键词助力SEO优化,让你的内容脱颖而出,东营响应式网站优化
AI免费文章生成器:轻松创作高质量内容的终极工具
SEO这样做,轻松引爆流量,助力企业成长,无极标准网站优化好处
360刷排名工具选哪家?揭秘2025年最强排名优化工具!,ai写作网站哪个好一点
在线AI文章生成器开启智能创作新时代
亚马逊产品seo什么意思,亚马逊平台产品专业术语 ,小新同学ai
seo网页优化什么意思,seo网站优化必知的10个问答,问吧,【解决】百度不知道 ,ai识别点读机
seo需要会什么技术呢,seo需要会什么技术呢知乎 ,红色框ai
360排名优化价格:打造高效网络营销的制胜法宝,ai换脸刘浩存自wei
SEO与SEM策略:提升网站流量与品牌曝光的双剑合璧,ai补图
ChatGPT恢复正常使用时间,提升你的工作与生活效率,联想电脑AI0510
ChatGPT破解:人工智能未来的无限可能,污污AI解说
什么是SEO可以自学吗,seo零基础可以自学吗 ,ai1紫
SEO详解:如何优化你的网站提升排名,获得更多流量,伊春湖南网站优化推广
好用的AI写作软件,让创作更高效
seo网站反链是什么,网站反链怎么做 ,ai插画头像卡通
SEO深度解析:如何通过深度优化提升网站排名,带来流量和转化,咸宁网站建设大概费用
怎么用AI生成文章免费版,高效创作从此开始!
seo矩阵运营中心是什么,seo矩阵运营中心是什么意思啊 ,北京ai特效
互联网快排:助力企业网站流量爆发的秘密武器,西安网站建设找资源公司
ChatGPTDNS出问题?如何快速解决并保障网络畅通,墨镜ai照片
ChatGPT免费订阅的使用限制:其潜力与挑战,ai辅助线无法对齐画板
SEO作用:提升网站流量与品牌曝光的秘密武器,美容网站联盟平台推广
在线AI文章:为您打造全新内容创作体验
seo诊断什么意思,seo诊断a5 ,约瑟夫ai
什么是seo运营,什么是seo营销 ,ai绘画沙地
seo软文有什么作用,seo文案是什么 ,斐乐Ai测评问题
GPT-3模型下载:开启智能时代的无限可能,ai走入
为什么“360收录”是你网站推广的必备利器,seo亚马逊
SEO王:掌控搜索引擎优化的至高法则,助力企业飞速腾飞,网站动作优化在哪里下载
SEO优化关键词品牌:如何通过精准的关键词提升品牌价值,ai免费智能写作论文
SEO立刻:快速提升网站排名,带你走在搜索引擎前沿,网站建设怎么推广好做
SEO那么重要,企业如何通过它脱颖而出?,自媒体推广营销的概念
代哥SEO-让您的网站迅速登顶搜索引擎的秘密武器,济南关键词的排名优化
ChatGPT全球宕机:人工智能的崩塌与未来的挑战,中国ai和美国ai教父
什么是seo的豆子,何为seo ,ai圆复制
SEO人工优化-让你的网页轻松登上搜索引擎首页,ai主母
什么是seo公司口碑,seo品牌 ,ai大模型训练是什么意思
seo重点工作是什么,seo重点工作是什么意思 ,棋谱检查ai
AI助手不需要登陆-畅享便捷生活,随时随地高效工作,ai客服 对话
SEO抓取:让您的网站从零到一的秘诀,佛山网站建设哪家效果好
SEO功能:助力网站流量提升与品牌曝光,天津公司网站建设与维护
OpenAI推出的GPT-4Turbo大幅降低了AI应用成本,推动了AI技术的普及化,ai*版
seo需要了解什么,seo需要学些什么内容 ,ovo ai
SEO优化是做什么的?让你的网站流量飙升的秘密武器,乳腺癌ai作用机理
ChatGPT的诞生,预示着人工智能大规模应用的时代已经来临,ai此生不渝
seo要学什么技术,seo要学什么技术好 ,ai.fale
SEO快速提升SEO排名的有效策略:让你的网页飞跃搜索引擎,ai调色食物
ChatGPT破解版电脑:如何获得更强大的AI助手,提升工作与学习效率,ai预设动画