?? file.jsp
字號:
<%@ page contentType="text/html;charset=GB2312" language="java" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@ page import="org.apache.commons.fileupload.*" %>
<%
// 申明將上傳文件放到服務器的 / .... /upload 目錄中
String saveDirectory = "c:\\log\\";
// 申明臨時目錄
String tmpDirectory = "c:\\";
// 申明限制上傳文件總大小為, 單位為 byte, -1 表示無限制
int maxPostSize = 1024 * 1024;
%>
<%
// 申明儲存敘述上傳文件內(nèi)容的變量
String FileDescription = null;
// 申明儲存上傳文件名稱的變量
String FileName = null;
// 申明儲存上傳文件大小的變量
long FileSize = 0;
// 申明儲存上傳文件類型的變量
String ContentType = null;
// 計算上傳文件之個數(shù)
int count = 0 ;
%>
<%
DiskFileUpload upload = new DiskFileUpload();
// 設置內(nèi)存存放數(shù)據(jù)的大小, 超過則寫入文件, 有設定臨時目錄, 臨時文件置于臨時目錄下
upload.setSizeThreshold(4096);
// 設置總上傳大小限制
upload.setSizeMax(maxPostSize);
// 設置臨時目錄
upload.setRepositoryPath(tmpDirectory);
/* FileItem */
List items = upload.parseRequest(request);
%>
<body>
<%
Iterator iter = items.iterator();
int tmp = 0;
FileItem tmpItem = null;
while (iter.hasNext())
{
tmp++;
FileItem item = (FileItem) iter.next();
if (item.isFormField()) {
// 如果是一般欄位, 取得文件敘述
FileDescription = item.getString();
} else {
// 否則取得文件信息
FileName = item.getName();
// 因為不同的瀏覽器會造成傳遞 path + filename, 有些則只有 filename
try {
// for wintel platform
FileName = FileName.substring(FileName.lastIndexOf("\\")+1);
// for unix-like platform
FileName = FileName.substring(FileName.lastIndexOf("/")+1);
} catch ( Exception ex ) {
out.println(ex);
}
ContentType = item.getContentType();
FileSize = item.getSize();
tmpItem = item;
}
// 因為一個文件都是兩個欄位, 每讀取兩個欄位處理一次
if (tmp == 2 && FileSize != 0)
{
count ++;
%>
<font color="red">你上傳的第<%= count %>個的文件:</font><br>
文件名稱為:<%= FileName %><br>
文件大小為:<%= FileSize %> Bytes<br>
文件類型為:<%= ContentType %><br>
文件的敘述:<%= FileDescription %><br><br>
<%
// 將文件寫入存檔目錄
try {
out.println(FileName);
File uploadedFile = new File(saveDirectory + FileName);
tmpItem.write(uploadedFile);
} catch ( Exception ex ) {
out.println(ex);
}
tmp = 0;
} else if (tmp == 2 && FileSize == 0) {
tmp = 0;
} // end if
} // end while
%>
您總共上傳<font color="red"><%= count %></font>個文件
</body>
</html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -