<%@ page contentType="text/html; charset=GBK"%>
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%@ page import="javax.servlet.*"%>
<%@ page import="javax.servlet.http.*"%>
<% request.setCharacterEncoding("utf-8"); %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<%
int MAX_SIZE = 102400 * 102400; //定义上载文件的最大字节
String rootPath; // 创建根路径的保存变量
DataInputStream in = null; //声明文件读入类
FileOutputStream fileOut = null;
String remoteAddr = request.getRemoteAddr(); //取得客户端的网络地址
String serverName = request.getServerName(); //获得服务器的名字
String realPath = request.getRealPath("/");//取得互联网程序的绝对地址
realPath = realPath.substring(0,realPath.lastIndexOf("\\"));
rootPath=realPath+"\\upload\\";
//rootPath = "D:\\jsp666"; //创建文件的保存目录
out.println("上传文件保存目录为" + rootPath);
String contentType = request.getContentType(); //取得客户端上传的数据类型
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;
while (totalBytesRead < formDataLength) { //上传的数据保存在byte数组
byteRead = in.read(dataBytes, totalBytesRead,
formDataLength);
totalBytesRead += byteRead;
}
String file = new String(dataBytes); //根据byte数组创建字符串
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;
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;
int startPos = ((file.substring(0, pos)).getBytes()).length;//取得文件数据的开始的位置
int endPos = ((file.substring(0, boundaryLocation))
.getBytes()).length; //取得文件数据的结束的位置
//out.print(fileName);
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><font color=red size=5>" + saveFile
+ "文件成功上传.</font></p>");
} else {
String content = request.getContentType();
out.println("<p>上传的数据类型不是是multipart/form-data</p>");
}
} catch (Exception ex) {
ex.printStackTrace();
//throw new ServletException(ex.getMessage());
}
%>
</body>
</html>