本文是一個純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("http://"));
//out.print(realpath);
//創建文件的保存目錄"/upload"
rootpath = realpath + "http://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("http://")
+ 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>
新聞熱點
疑難解答