教您純Jsp的自訂的單個檔案上傳代碼

來源:互聯網
上載者:User
js

  本文是一個純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(remoteAddr);
//獲得伺服器的名字
String serverName = request.getServerName();
//out.print(serverName);
//取得jsp檔案相對與根地址的地址
//out.print(request.getServletPath());
//取得互連網程式的絕對位址
String realPath = request.getRealPath(serverName);
//out.println(realPath);
realPath = realPath.substring
(0,realPath.lastIndexOf("\\"));
//out.print(realPath);
//建立檔案的儲存目錄"\upload"
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>");
}
//檢查上傳檔案的目錄是否存在
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>



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.