加入收藏 | 设为首页 | 会员中心 | 我要投稿 聊城站长网 (https://www.0635zz.com/)- 智能语音交互、行业智能、AI应用、云计算、5G!
当前位置: 首页 > 教程 > 正文

教您纯Jsp的自定义的单个文件上载代码示例

发布时间:2023-05-16 13:27:35 所属栏目:教程 来源:
导读:本文是一个纯jsp的自定义的单个文件上载代码:

<%@ page contenttype="text/html; charset=gbk" %>

<%@ page import="java.io.*"%>

<%@ page import="java.util.*"%>

<%@ page import="javax.servlet
本文是一个纯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>
 
 

(编辑:聊城站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!