UploadExample.jsp <%@ page contentType="text/html;charset=gb2312"%>
<%= application.getServerInfo() %> 上传文件程序应用示例
doUpload.jsp <%@ page contentTy pe="tex t/html; charset=GBK" %> <%@ page import="jav a.io.*"%> <%@ page import="jav a.u til.*"%> <%@ page import="jav ax .serv let.*"%> <%@ page import="jav ax .serv let.http.*"%>
u pFile <% //定义上载文件的最大字节 int MAX_SIZE = 102400 * 102400; // 创建根路径的保存变量 String rootPath; //声明文件读入类 DataInpu tStream in = nu ll; FileOu tpu tStream fileOu t = nu ll; //取得客户端的网络地址 String remoteAddr = requ est.getRemoteAddr(); //获得服务器的名字 String serv erName = requ est.getServ erName(); //取得互联网程序的绝对地址 String realPath = request.getRealPath(serverName); realPath = realPath.substring(0,realPath.lastIndexOf("\\")); //创建文件的保存目录 rootPath = realPath + "\\upload\\"; //取得客户端上传的数据类型 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("
上传的文件字节数不可以超过" + MAX_SIZE + "
"); return; } //保存上传文件的数据 byte dataBytes[] = new byte[formDataLength]; int byteRead = 0; int totalBytesRead = 0; //上传的数据保存在 byte 数组 while(totalBytesRead < formDataLength){ byteRead = in.read(dataBytes,totalBytesRe...