关于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">
<%
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\\"; //创建文件的保存目录
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 =&