Commons FileUpload
UUID是Universally Unique IDentifier,他是一組128bit的數(shù)字,定義在http://www.ietf.org/internet-drafts/draft- mealling-uuid-urn-03.txt .相信有寫過(guò)Windows程式的人對(duì)他不會(huì)太陌生. 有時(shí)後我們?cè)诜稚⑹降南到y(tǒng)下,希望要產(chǎn)生唯一的一個(gè)ID.如果我們不希望用centralize的方式產(chǎn)生的話,我們可以用UUID來(lái)產(chǎn)生Unique ID,這個(gè)演算法讓我們幾乎不會(huì)產(chǎn)生重複的ID.使用方法也很簡(jiǎn)單:
UUID uuid = UUID.randomUUID();
System.out.printf("UUID : %s", uuid).println();
使用瀏覽器進(jìn)行檔案上傳時(shí),是使用multipart/form-data編碼,然而
Servlet容器並不會(huì)自動(dòng)幫我們處理編碼,而必須由程式設(shè)計(jì)人員自行處理,Jakarta Commons
FileUpload可以讓您輕易的處理檔案上傳事務(wù),您可以在這個(gè)網(wǎng)頁(yè)中找到下載檔案,撰寫本文時(shí)最新的版本是1.0,檔名是commons-
fileupload-1.0.zip:
http://jakarta.apache.org/site/binindex.cgi
將zip檔案解開,將commons-fileupload-1.0.jar放置在WEB-INF/lib下,這邊先提供一個(gè)快速入門的例子,首先撰寫上傳的表單:
<html>
<head>
<title>檔案上傳</title>
<meta http-equiv="Content-Type" content="text/html; charset=big5">
</head>
<body>
<b>檔案上傳</b></font></p>
<form name="UploadForm" enctype="multipart/form-data" method="post" action="upload.jsp">
<input type="file" name="File1" size="20" maxlength="20"> <br>
<input type="file" name="File2" size="20" maxlength="20"> <br>
<input type="submit"value="上傳">
</form>
</body>
</html>
要處理上傳的檔案,首先要設(shè)定一些儲(chǔ)存檔案所需的資訊,然後處理上傳請(qǐng)求:
DiskFileUpload fu = new DiskFileUpload();
// 使用的記憶體容量,超過(guò)先寫入暫存檔
fu.setSizeThreshold(4096);
// 最大上傳檔案容量
fu.setSizeMax(1000000);
// 暫存目錄
fu.setRepositoryPath(application.getRealPath("/"));
List fileItems = fu.parseRequest(request);
上面的程式也可以綜合為下面這行程式:
DiskFileUpload upload = new DiskFileUpload();
List items = upload.parseRequest(request,
yourMaxMemorySize, yourMaxRequestSize, yourTempDirectory);
parserRequest()傳回的List內(nèi)含F(xiàn)ileItem物件,我們可以取出並寫入檔案,例如:
Iterator itr = fileItems.iterator();
while(itr.hasNext()) {
FileItem fi = (FileItem)itr.next();
out.println("\nNAME: "+fi.getName());
out.println("SIZE: "+fi.getSize());
File fNew= new File(application.getRealPath("/"), fi.getName());
fi.write(fNew);
}
上面的程式將會(huì)將檔案寫入Web應(yīng)用程式的根目錄,下面的JSP程式是個(gè)簡(jiǎn)單的檔案上傳範(fàn)例:
<%@ page import="org.apache.commons.fileupload.DiskFileUpload"%>
<%@ page import="org.apache.commons.fileupload.FileItem"%>
<%@ page import="java.util.List"%>
<%@ page import="java.util.Iterator"%>
<%@ page import="java.io.File"%>
<%@page contentType="text/html;charset=Big5"%>
<%
DiskFileUpload fu = new DiskFileUpload();
// 使用的記憶體容量,超過(guò)先寫入暫存檔
fu.setSizeThreshold(4096);
// 最大上傳檔案容量
fu.setSizeMax(1000000);
// 儲(chǔ)存的目錄
fu.setRepositoryPath(application.getRealPath("/") + "temp/");
List fileItems = fu.parseRequest(request);
Iterator itr = fileItems.iterator();
while(itr.hasNext()) {
FileItem fi = (FileItem)itr.next();
out.println("\nNAME: "+fi.getName());
out.println("SIZE: "+fi.getSize());
File fNew= new File(application.getRealPath("/"), fi.getName());
fi.write(fNew);
}
%>
這邊是以寫入檔案為例,如果有要進(jìn)行額外的處理,例如取得串流或位元陣列,則可以使用FileItem的getInputStream()或get()來(lái)分別取得,詳細(xì)的方法可以查詢API文件。
有時(shí)候您會(huì)在表單中包括有檔案描述與檔案上傳欄位,例如:
<form name="UploadForm" enctype="multipart/form-data" method="post" action="upload.jsp">
上傳檔案 1 <input type="file" name="File1" size="20" maxlength="20"> <br>
檔案描述 1 <input type="text" name="File1" size="30" maxlength="50"> <br>
上傳檔案 2<input type="file" name="File2" size="20" maxlength="20"> <br>
檔案描述 2 <input type="text" name="File1" size="30" maxlength="50"> <br>
<input type="submit"value="上傳">
</form>
這時(shí)候在上傳處理時(shí),必須要分別是檔案欄位還是一般的表單欄位,您可以使用FileItem的isFormField()來(lái)進(jìn)行判斷,例如:
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField()) {
// 處理檔案描述欄位
processFormField(item);
} else {
// 處理上傳的檔案
processUploadedFile(item);
}
}
另外上檔檔案瀏覽器傳送檔案名稱時(shí),有的瀏覽器可能會(huì)包括檔案路徑資訊,為了處理這種情況,我們可以事先處理一下這個(gè)可能性,例如在processUploadedFile()方法中這麼實(shí)作:
String fileName = item.getName();
try {
// unix-like 系統(tǒng)
fileName = FileName.substring(FileName.lastIndexOf("/")+1);
// Windows 系統(tǒng)
// fileName = FileName.substring(FileName.lastIndexOf("\\")+1);
} catch (Exception e) {
.....
}
http://www.caterpillar.onlyfun.net/phpBB2/viewtopic.php?t=1315
posted on 2006-03-23 00:11 Vincent.Chen 閱讀(558) 評(píng)論(0) 編輯 收藏 所屬分類: Java