一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

Apache FileUpload的兩種上傳方式介紹及應(yīng)用

環(huán)境
tomcat5.6
commmons-fileupload-1.3.jar
commmons-io-2.4.jar
JSP
編碼:UTF-8
臨時(shí)文件夾:fileupload/tmp相對(duì)于網(wǎng)站根目錄
上傳文件保存位置:fileupload
Traditional API上傳方式
//fileload01.htm
復(fù)制代碼 代碼如下:
<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
復(fù)制代碼 代碼如下:
<%@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
復(fù)制代碼 代碼如下:
<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
復(fù)制代碼 代碼如下:
<%@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上傳速度相對(duì)較快。因?yàn)樗抢脙?nèi)存保存上傳的文件,節(jié)省了傳統(tǒng)API將文件寫入臨時(shí)文件帶來的開銷。
可參考:
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技術(shù)Apache FileUpload的兩種上傳方式介紹及應(yīng)用,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 激情文学综合 | 欧美精品v国产精品v日韩精品 | 久青草国产手机在线视频 | 久久大香萑太香蕉综合网 | 五月天天堂网 | 91麻豆精品国产91久久久久 | 亚洲精品国产成人专区 | 亚洲成精品动漫久久精久 | 玖玖色在线 | 中文一区 | 国产一区二区精品久久91 | 亚洲一区二区精品视频 | 2018狠狠操 | 成年美女黄网站色大免费视频 | 四虎国产永久在线精品免费观看 | 久久久窝窝午夜精品 | 亚洲伊人色 | 亚洲一区二区中文字5566 | 草草草在线观看 | 国产成人精品亚洲日本在线 | 亚洲综合图片小说区热久久 | 国产精品视频福利 | 激情文学首页 | 亚洲综合区小说区激情区噜噜 | 日韩2区| 国产精品亚洲一区二区三区 | 成年美女黄网站色视频大全免费 | 欧美在线色 | 婷婷影院在线综合免费视频 | 免费视频一区二区 | 成人毛片18女人毛片免费 | 亚洲福利在线视频 | 最近免费中文在线视频 | 欧美xxxx三人交性视频 | 在线视频一区二区三区三区不卡 | 欧美白人猛性xxxxx交69 | 欧美黄色大片免费观看 | 亚洲高清中文字幕一区二区三区 | 色视网站 | 欧美大香线蕉线伊人久久 | 国产精品色午夜视频免费看 |