<html>
<head>
<title>File Upload</title>
<meta http-equiv="Content-Type" content="text/html; charset=big5" />
</head>
<body>
檔案上傳
<form name="upload" enctype="multipart/form-data" method="post" action="fileupload.jsp">
上傳檔案: <input type="file" name="file" size="20" maxlength="20" />
檔案說明: <input type="text" name="filedesc" size="30" maxlength="50" />
<input type="submit"value="上傳" /> <input type="reset" value="清除" />
</form>
</body>
</html>
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page import="java.io.File"%>
<%@ page import="java.util.Iterator"%>
<%@ 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"%>
<%@ page import="org.apache.commons.io.FilenameUtils"%>
<%
String saveDirectory = application.getRealPath("/upload");
// Check that we have a file upload request
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
out.println("isMultipart="+isMultipart+"<br>");
// Create a factory for disk-based file items
FileItemFactory factory = new DiskFileItemFactory();
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
//Create a progress listener
ProgressListener progressListener = new ProgressListener(){
private long megaBytes = -1;
public void update(long pBytesRead, long pContentLength, int pItems) {
long mBytes = pBytesRead / 1000000;
if (megaBytes == mBytes) {
return;
}
megaBytes = mBytes;
System.out.println("We are currently reading item " + pItems);
if (pContentLength == -1) {
System.out.println("So far, " + pBytesRead + " bytes have been read.");
} else {
System.out.println("So far, " + pBytesRead + " of " + pContentLength
+ " bytes have been read.");
}
}
};
upload.setProgressListener(progressListener);
// Parse the request
List /* FileItem */ items = upload.parseRequest(request);
// Process the uploaded items
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField()) {
// Process a regular form field
//processFormField(item);
String name = item.getFieldName();
String value = item.getString();
value = new String(value.getBytes("UTF-8"), "ISO-8859-1");
out.println(name + "=" + value+"<br>");
} else {
// Process a file upload
//processUploadedFile(item);
String fieldName = item.getFieldName();
String fileName = item.getName();
String contentType = item.getContentType();
boolean isInMemory = item.isInMemory();
long sizeInBytes = item.getSize();
out.println("fieldName="+fieldName+"<br>");
out.println("fileName="+fileName+"<br>");
out.println("contentType="+contentType+"<br>");
out.println("isInMemory="+isInMemory+"<br>");
out.println("sizeInBytes="+sizeInBytes+"<br>");
if (fileName != null && !"".equals(fileName)) {
fileName= FilenameUtils.getName(fileName);
out.println("fileName saved="+fileName+"<br>");
File uploadedFile = new File(saveDirectory, fileName);
item.write(uploadedFile);
}
}
}
%>
檔案名稱 | fileupload.jsp |
描述 | 網頁上傳處理程式 |
檔案大小 | 3 Kbytes |
下載次數 | 450 次 |
下載 |
檔案名稱 | fileupload.html |
描述 | 上傳表單網頁 |
檔案大小 | 556 bytes |
下載次數 | 393 次 |
下載 |
<html>
<head>
<title>File Upload</title>
<meta http-equiv="Content-Type" content="text/html; charset=big5">
</head>
<body>
檔案上傳
<form name="upload" enctype="multipart/form-data" method="post" action="fileupload_streaming.jsp">
上傳檔案: <input type="file" name="file" size="20" maxlength="20">
檔案說明: <input type="text" name="filedesc" size="30" maxlength="50">
<input type="submit"value="上傳"> <input type="reset" value="清除">
</form>
</body>
</html>
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page import="java.io.*"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@ page import="org.apache.commons.fileupload.util.Streams"%>
<%@ page import="org.apache.commons.io.FilenameUtils"%>
<%
String saveDirectory = application.getRealPath("/upload");
// Check that we have a file upload request
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
out.println("isMultipart="+isMultipart+"<br>");
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload();
//Create a progress listener
ProgressListener progressListener = new ProgressListener(){
private long megaBytes = -1;
public void update(long pBytesRead, long pContentLength, int pItems) {
long mBytes = pBytesRead / 1000000;
if (megaBytes == mBytes) {
return;
}
megaBytes = mBytes;
System.out.println("We are currently reading item " + pItems);
if (pContentLength == -1) {
System.out.println("So far, " + pBytesRead + " bytes have been read.");
} else {
System.out.println("So far, " + pBytesRead + " of " + pContentLength
+ " bytes have been read.");
}
}
};
upload.setProgressListener(progressListener);
// Parse the request
FileItemIterator iter = upload.getItemIterator(request);
while (iter.hasNext()) {
FileItemStream item = iter.next();
String name = item.getFieldName();
InputStream stream = item.openStream();
if (item.isFormField()) {
String value = Streams.asString(stream);
out.println(name + "=" + value+"<br>");
} else {
System.out.println("File field " + name + " with file name "
+ item.getName() + " detected.");
// Process the input stream
String fieldName = item.getFieldName();
String fileName = item.getName();
String contentType = item.getContentType();
out.println("fieldName="+fieldName+"<br>");
out.println("fileName="+fileName+"<br>");
out.println("contentType="+contentType+"<br>");
if (fileName != null && !"".equals(fileName)) {
fileName= FilenameUtils.getName(fileName);
out.println("fileName saved="+fileName+"<br>");
File uploadedFile = new File(saveDirectory, fileName);
FileOutputStream uploadedFileStream =
new FileOutputStream(uploadedFile);
Streams.copy(stream, uploadedFileStream, true);
}
}
}
%>
檔案名稱 | fileupload_streaming.html |
描述 | 上傳表單網頁 |
檔案大小 | 543 bytes |
下載次數 | 157 次 |
下載 |
檔案名稱 | fileupload_streaming.jsp |
描述 | 網頁上傳處理程式 |
檔案大小 | 3 Kbytes |
下載次數 | 172 次 |
下載 |
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>File Upload</title>
<style type="text/css">
<!--
.topic {color: #FF0000; font-size: 1.5em; font-weight: bold}
-->
</style>
</head>
<body>
<p class="topic">檔案上傳
<form name="upload" enctype="multipart/form-data" method="post" action="fileupload_control.jsp">
上傳檔案: <input type="file" name="file" size="20" maxlength="20" />
檔案說明: <input type="text" name="filedesc" size="30" maxlength="50" />
<input type="submit"value="上傳" /> <input type="reset" value="清除" />
</form>
</body>
</html>
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page import="java.io.File"%>
<%@ page import="java.util.Iterator"%>
<%@ 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"%>
<%@ page import="org.apache.commons.io.FilenameUtils"%>
<%!
int yourMaxMemorySize = 1024 * 1024 * 1024;
File yourTempDirectory = new File("/tmp");
int yourMaxRequestSize = 100 * 1024 * 1024;
boolean writeToFile = true;
String allowedFileTypes = ".txt .pdf .doc .ppt .xls .csv .dbf .gif .jpg .jpeg .png .swf .htm .html .zip .rar";
%>
<%
String saveDirectory = application.getRealPath("/upload");
// Check that we have a file upload request
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
out.println("isMultipart=" + isMultipart + "<br>");
// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory(yourMaxMemorySize, yourTempDirectory);
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// Set overall request size constraint
upload.setSizeMax(yourMaxRequestSize);
//Create a progress listener
ProgressListener progressListener = new ProgressListener() {
private long megaBytes = -1;
public void update(long pBytesRead, long pContentLength, int pItems) {
long mBytes = pBytesRead / 1000000;
if (megaBytes == mBytes) {
return;
}
megaBytes = mBytes;
System.out.println("We are currently reading item " + pItems);
if (pContentLength == -1) {
System.out.println("So far, " + pBytesRead + " bytes have been read.");
} else {
System.out.println("So far, " + pBytesRead + " of " + pContentLength + " bytes have been read.");
}
}
};
upload.setProgressListener(progressListener);
try {
// Parse the request
List /* FileItem */items = upload.parseRequest(request);
// Process the uploaded items
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField()) {
// Process a regular form field
//processFormField(item);
String name = item.getFieldName();
String value = item.getString("UTF-8");
out.println(name + "=" + value + "<br />");
} else {
// Process a file upload
//processUploadedFile(item);
String fieldName = item.getFieldName();
String fileName = item.getName();
String contentType = item.getContentType();
boolean isInMemory = item.isInMemory();
long sizeInBytes = item.getSize();
out.println("fieldName=" + fieldName + "<br />");
out.println("fileName=" + fileName + "<br />");
out.println("contentType=" + contentType + "<br />");
out.println("isInMemory=" + isInMemory + "<br />");
out.println("sizeInBytes=" + sizeInBytes + "<br />");
if (fileName != null && !"".equals(fileName)) {
if (writeToFile) {
fileName = FilenameUtils.getName(fileName);
out.println("fileName to be saved=" + fileName + "<br />");
String extension = FilenameUtils.getExtension(fileName);
if (allowedFileTypes.indexOf(extension.toLowerCase()) != -1) {
File uploadedFile = new File(saveDirectory, fileName);
item.write(uploadedFile);
} else {
out.println("上傳的檔案不能是" + extension + "<br />");
}
} else {
//InputStream uploadedStream = item.getInputStream();
//...
//uploadedStream.close();
// Process a file upload in memory
byte[] data = item.get();
out.println("data size=" + data.length + "<br />");
}
}
}
}
} catch (FileUploadBase.SizeLimitExceededException ex1) {
out.println("上傳檔案超過最大檔案允許大小" + yourMaxRequestSize / (1024 * 1024) + "MB !");
}
%>
<web-app>
...
<listener>
<listener-class>
org.apache.commons.fileupload.servlet.FileCleanerCleanup
</listener-class>
</listener>
...
</web-app>
檔案名稱 | fileupload_control.html |
描述 | 檔案上傳表單 |
檔案大小 | 862 bytes |
下載次數 | 112 次 |
下載 |
檔案名稱 | fileupload_control.jsp |
描述 | 檔案上傳處理程式 |
檔案大小 | 4 Kbytes |
下載次數 | 124 次 |
下載 |
String fileDesc = null;
...
if (item.isFormField()) {
// Process a regular form field
//processFormField(item);
String name = item.getFieldName();
String value = item.getString("UTF-8");
out.println(name + "=" + value+"
");
if ("filedesc".equals(name)) {
fileDesc = value;
}
} else {
...
// Delete a file
public static boolean deleteFile(String filename) {
File f = new File(filename);
boolean result = false;
if (f.exists()) {
result = f.delete();
if (result != true) {
System.out.println("Delete file failed: " + f.getName());
}
}
return result;
}
<html>
<head>
<title>File Upload</title>
<meta http-equiv="Content-Type" content="text/html; charset=big5">
</head>
<body>
檔案上傳
<form name="upload" enctype="multipart/form-data" method="post" action="fileupload_streaming.jsp">
上傳檔案: <input type="file" name="file" size="20" maxlength="20">
檔案說明: <input type="text" name="filedesc" size="30" maxlength="50">
上傳檔案: <input type="file" name="file" size="20" maxlength="20">
檔案說明: <input type="text" name="filedesc" size="30" maxlength="50">
<input type="submit"value="上傳"> <input type="reset" value="清除">
</form>
</body>
</html>
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@ page import="org.apache.commons.fileupload.util.Streams"%>
<%@ page import="org.apache.commons.io.FilenameUtils"%>
<%
String saveDirectory = application.getRealPath("/upload");
// Check that we have a file upload request
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
out.println("isMultipart="+isMultipart+"<br>");
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload();
//Create a progress listener
ProgressListener progressListener = new ProgressListener(){
private long megaBytes = -1;
public void update(long pBytesRead, long pContentLength, int pItems) {
long mBytes = pBytesRead / 1000000;
if (megaBytes == mBytes) {
return;
}
megaBytes = mBytes;
System.out.println("We are currently reading item " + pItems);
if (pContentLength == -1) {
System.out.println("So far, " + pBytesRead + " bytes have been read.");
} else {
System.out.println("So far, " + pBytesRead + " of " + pContentLength
+ " bytes have been read.");
}
}
};
upload.setProgressListener(progressListener);
// Parse the request
List<String> fileDescList = new ArrayList<String>();
List<String> fileNameList = new ArrayList<String>();
FileItemIterator iter = upload.getItemIterator(request);
while (iter.hasNext()) {
FileItemStream item = iter.next();
String name = item.getFieldName();
InputStream stream = item.openStream();
if (item.isFormField()) {
String value = Streams.asString(stream);
out.println(name + "=" + value+"
");
if ("filedesc".equals(name)) {
fileDescList.add(value);
}
} else {
System.out.println("File field " + name + " with file name "
+ item.getName() + " detected.");
// Process the input stream
String fieldName = item.getFieldName();
String fileName = item.getName();
String contentType = item.getContentType();
out.println("fieldName="+fieldName+"
");
out.println("fileName="+fileName+"
");
out.println("contentType="+contentType+"
");
if (fileName != null && !"".equals(fileName)) {
fileName= FilenameUtils.getName(fileName);
out.println("fileName saved="+fileName+"
");
fileNameList.add(fileName);
File uploadedFile = new File(saveDirectory, fileName);
FileOutputStream uploadedFileStream =
new FileOutputStream(uploadedFile);
Streams.copy(stream, uploadedFileStream, true);
}
}
}
for (int i = 0; i < fileNameList.size(); i++) {
String fileDesc = fileDescList.get(i);
String fileName = fileNameList.get(i);
out.println(fileName+":"+fileDesc+"
");
// save to database for future use
}
%>
檔案名稱 | fileupload_streaming.jsp |
描述 | Multiple file upload fields handler |
檔案大小 | 3 Kbytes |
下載次數 | 74 次 |
下載 |
檔案名稱 | fileupload_streaming.html |
描述 | Multiple file upload fields form |
檔案大小 | 699 bytes |
下載次數 | 93 次 |
下載 |
victer0327 wrote:請問板大,檔案上傳時的說明(filedesc)輸入中文會出現亂碼,這該如何解決呢??
String value = Streams.asString(stream);
String encoding = "UTF-8";
String value = Streams.asString(stream, encoding);
A-man wrote:
在網上找了一下, 找不到明確的方法,
所以想問問 安裝函式庫 該怎安裝 ?
我正使用eclipse + tomcat 4.1
在Apache Commons的網站我下載了
commons-io-1.4-src.zip & commons-fileupload-1.2.1-src.zip
解壓後 發現內有很多不同的檔案,
我是應該把以下兩個目錄個的檔案, 全部複製到我eclipse 的Project 內 ?
commons-fileupload-1.2.1-src\src\java\org
和
commons-io-1.4-src\src\java\org\
謝謝你 ^^
檔案名稱 | problem.JPG |
描述 | 沒有檔案註解存在 |
檔案大小 | 144 Kbytes |
下載次數 | 33 次 |
下載 |