![]() |
<html>
<head>
<title>File Upload</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
檔案上傳
<form name="upload" enctype="multipart/form-data" method="post" action="fileupload_streaming.jsp">
存檔名稱: <input type="text" name="filename" size="50" maxlength="255">
上傳檔案: <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");
// Change save direcoty to another place outside of Tomcat
//String saveDirectory = "C:/upload";
File dir = new File(saveDirectory);
if (!dir.exists()) {
boolean success = dir.mkdir();
if (success) {
System.out.println("Directory: " + saveDirectory + " created");
}
}
out.println("file.encoding=" + System.getProperty("file.encoding") + "<br>");
// Solve Chinese filename problem: use original form encoding
String encoding = "UTF-8";
request.setCharacterEncoding(encoding);
// 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);
String filenameSaved = null;
while (iter.hasNext()) {
FileItemStream item = iter.next();
String name = item.getFieldName();
InputStream stream = item.openStream();
if (item.isFormField()) {
String value = Streams.asString(stream, encoding);
out.println(name + "=" + value + "<br>");
if ("filename".equals(name)) {
filenameSaved = value;
System.out.println(name + "=" + 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 + "<br>");
out.println("fileName=" + fileName + "<br>");
out.println("contentType=" + contentType + "<br>");
if (fileName != null && !"".equals(fileName)) {
fileName = FilenameUtils.getName(fileName);
if (filenameSaved != null && !"".equals(filenameSaved) && !filenameSaved.equals(fileName)) {
fileName = filenameSaved;
}
out.println("fileName saved=" + fileName + "<br>");
File uploadedFile = new File(saveDirectory, fileName);
FileOutputStream uploadedFileStream =
new FileOutputStream(uploadedFile);
Streams.copy(stream, uploadedFileStream, true);
}
}
}
%>
<input type="button" name="prev" value="Go back to previous page" onclick="history.go(-1);"/>
<input type="button" name="prev" value="Go back to previous page" onclick="history.go(-1);"/>
檔案名稱 | fileupload_streaming.jsp |
描述 | fileupload_streaming.jsp with new filename saved processing |
檔案大小 | 4 Kbytes |
下載次數 | 13 次 |
![]() |
檔案名稱 | fileupload_streaming.html |
描述 | fileupload_streaming.html with new filename saved |
檔案大小 | 803 bytes |
下載次數 | 9 次 |
![]() |
檔案名稱 | text.jsp |
描述 | 這是第1頁的,第2頁則是fileupload.jsp |
檔案大小 | 2 Kbytes |
下載次數 | 2 次 |
![]() |
初心者 wrote:上傳已經成功了 感謝!
可是,傳到build裡面,圖片抓不出來,請問該怎麼解決???
|
|
檔案名稱 | 未命名.png |
描述 | 沒有檔案註解存在 |
檔案大小 | 22 Kbytes |
下載次數 | 0 次 |
![]() |
檔案名稱 | fileupload.html |
描述 | 沒有檔案註解存在 |
檔案大小 | 543 bytes |
下載次數 | 0 次 |
![]() |
檔案名稱 | fileupload.jsp |
描述 | 沒有檔案註解存在 |
檔案大小 | 3 Kbytes |
下載次數 | 2 次 |
![]() |
java.lang.UnsupportedClassVersionError: Bad version number in .class file
andowson wrote:dream1978您好:
你的主要問題原因是這一行:
java.lang.UnsupportedClassVersionError: Bad version number in .class file
請檢查Eclipse中的Java compiler設定是哪個版本的JDK