JSP文件上传

无组件文件上传
如果要把一个文件从客户端上传到服务器端,需要在客户端和服务器端建立一个通道传递文件的字节流,并在服务器上进行上传操作。这里通常要用到两个JSP页面,第一个JSP页面用于选择要上传的文件,第二个JSP页面用于客户端获取该文件里面的信息,并把这些信息以客户端相同的格式保存在服务器端,即上传文件的功能实现页面。
index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="upFile.jsp" name="one" method="post" enctype="multipart/form-data">
<p align="center">
文件上传
<input type="File" name="fileupload" value="upload">
<br>
<input type="submit" value="上传">
<input type="reset" value="取消">
</form>
</body>
</html>

upFile.jsp

<%@page import="java.io.DataInputStream"%>
<%@page import="java.io.File"%>
<%@page import="java.io.FileOutputStream"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
int MAX_SIZE = 102400*102400;//定义上传文件的最大字节
String rootPath; //创建根路径的保存变量
DataInputStream in = null; //声明文件读入类
FileOutputStream fileOut = null;
String remoteAddr = request.getRemoteAddr();//获取客户端的网络地址
System.out.println("remoteAddr"+remoteAddr);
String serverName = request.getServerName();//获取服务器的名字
System.out.println("serverName"+serverName);
String realPath = request.getRealPath(serverName);//获得互联网程序的绝对地址
realPath = realPath.substring(0,realPath.lastIndexOf("\\"));
rootPath = realPath+"\\upload\\";//创建文件订的保存目录
System.out.println("rootPath"+rootPath);
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;//	取得文件数据的结束位置
		File checkFile = new File(fileName);
		if(checkFile.exists()){
			out.print("<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 align='center'><font color=red size=5>"+saveFile+"文件上传成功</font></p>");
	}
	else{
		String content = request.getContentType();
		out.println("<p>上传的数据类型不是multipart/form-data</p>");
	}
}catch(Exception ex){
	throw new ServletException(ex.getMessage());
}
%>
<a href="upload.jsp">继续上传文件</a>a>
</body>
</html>
posted @ 2019-04-20 10:59  Philtell  阅读(136)  评论(0编辑  收藏  举报