国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

教您純Jsp的自定義的單個文件上載代碼

2024-09-05 00:20:23
字體:
來源:轉載
供稿:網友

  本文是一個純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>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 革吉县| 乐陵市| 大埔县| 来宾市| 双辽市| 会同县| 佛山市| 阿拉善左旗| 灵武市| 修水县| 客服| 建瓯市| 赤城县| 义马市| 清远市| 苍山县| 衡山县| 汉寿县| 东平县| 昆山市| 宁化县| 徐汇区| 贺州市| 扎鲁特旗| 中宁县| 正定县| 右玉县| 布拖县| 历史| 博爱县| 滕州市| 宁明县| 石城县| 巴林左旗| 多伦县| 安塞县| 东平县| 泸溪县| 神池县| 嘉祥县| 邵武市|