上傳文件選取頁面:upload.html
<html>
<head>
<title>文件上傳</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</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="submit"value="上傳">
</form>
</body>
</html>
上傳處理:upload.jsp
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%@ page import="javax.servlet.*"%>
<%@ page import="javax.servlet.http.*"%>
<html>
<head>
<title> upFile </title>
</head>
<body bgcolor="#ffffff">
<center>
<%
//定義上載文件的最大字節
int MAX_SIZE = 102400 * 102400;
// 創建根路徑的保存變量
String rootPath;
//聲明文件讀入類
DataInputStream in = null;
FileOutputStream fileOut = null;
//取得客戶端的網絡地址
String remoteAddr = request.getRemoteAddr();
out.print("request.getRemoteAddr()"+remoteAddr+"<br>");
//獲得服務器的名字
String serverName = request.getServerName();
out.print("request.getServerName()="+serverName+"<br>");
//取得jsp文件相對與根地址的地址
out.print("request.getServletPath()="+request.getServletPath()+"<br>");
//取得互聯網程序的絕對地址
String realPath = request.getRealPath(serverName);
out.println("request.getRealPath(serverName)="+realPath+"<br>");
out.println("OK<br>");
realPath = realPath.substring(0,realPath.lastIndexOf("\\"));
//創建文件的保存目錄
rootPath = realPath + "\\upload\\";
//out.println(rootPath);
//取得客戶端上傳的數據類型
String contentType = request.getContentType();
//out.println("<p>客戶端上傳的數據類型 = " + contentType + "</p>");
try{
if(contentType.indexOf("multipart/form-data") >= 0){
//讀入上傳的數據
in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
if(formDataLength > MAX_SIZE){
out.println("<P>上傳的文件字節數不可以超過" + MAX_SIZE + "</p>");
return;
}
//保存上傳文件的數據
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
//上傳的數據保存在byte數組
while(totalBytesRead < formDataLength){
byteRead = in.read(dataBytes,totalBytesRead,formDataLength);
totalBytesRead += byteRead;
}
//根據byte數組創建字符串
String file = new String(dataBytes);
//out.println(file);
//取得上傳的數據的文件名
String saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0,saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
//取得數據的分隔字符串
String boundary = contentType.substring(lastIndex + 1,contentType.length());
//創建保存路徑的文件名
String fileName = rootPath + saveFile;
//out.print(fileName);
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n",pos) + 1;
pos = file.indexOf("\n",pos) + 1;
pos = file.indexOf("\n",pos) + 1;
int boundaryLocation = file.indexOf(boundary,pos) - 4;
//out.println(boundaryLocation);
//取得文件數據的開始的位置
int startPos = ((file.substring(0,pos)).getBytes()).length;
//out.println(startPos);
//取得文件數據的結束的位置
int endPos = ((file.substring(0,boundaryLocation)).getBytes()).length;
//out.println(endPos);
//檢查上載文件是否存在
File checkFile = new File(fileName);
if(checkFile.exists()){
out.println("<p>" + saveFile + "文件已經存在.</p>");
return;
}
//檢查上載文件的目錄是否存在
File fileDir = new File(rootPath);
if(!fileDir.exists()){
fileDir.mkdirs();
}
//創建文件的寫出類
fileOut = new FileOutputStream(fileName);
//保存文件的數據
fileOut.write(dataBytes,startPos,(endPos - startPos));
fileOut.close();
out.println("<P>" + saveFile + "文件成功上載.</p>");
}else{
String content = request.getContentType();
out.println("<p>上傳的數據類型不是是multipart/form-data</p>");
}
}catch(Exception ex){
throw new ServletException(ex.getMessage());
}
%>
</center>
</body>
</html>