天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

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

jsp技術Apache FileUpload的兩種上傳方式介紹及應用,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 性色AV一区二区三区V视界影院 | 贵妇局长的蕾丝乳罩 | 午夜色情影院色a国产 | 日韩欧美精品有码在线播放免费 | 伦 乱真实故事 | 亚洲永久免费视频 | 国产精品爽爽久久久久久无码 | 久久亚洲电影www电影网 | 暖暖的视频完整视频免费韩国 | 国产精品嫩草影院 | 67194线在线精品观看 | 日本高清加勒比 | 久久re视频这里精品免费1 | 欧洲馒头大肥p | 在线看片成人免费视频 | 国产精品资源在线观看网站 | 亚洲 欧美 日韩 精品 自拍 | 亚洲精品资源网在线观看 | 白嫩美女直冒白浆 | 人曽交Z00Z0OA片| 无遮挡h肉3d动漫在线观看 | 国产婷婷午夜无码A片 | 国产色精品久久人妻无码 | 国产精品99久久久久久人韩国 | 国产精品熟女人妻 | 最新国自产拍天天更新 | 一本大道熟女人妻中文字幕在线 | 国产精品人妻系列21P | 大胆国模一区二区三区伊人 | 99精品热视频30在线热视频 | 久久九九精品国产自在现线拍 | 男人狂躁进女人免费视频公交 | 999zyz色资源站在线观看 | 国产精品无码麻豆放荡AV | 99RE6国产精品视频播放 | 视频一区国产在线第一页 | 95国产欧洲精华液 | 国产午夜精品理论片久久影视 | 无码AV精品久久一区二区免费 | 亚洲国产在线视频中文字 | 亚洲va在线va天堂XX xX |