uploadExample.jsp 调用 doUpload.jsp,需要两个文件一起
1、uploadExample.jsp
<%@ page contentType="text/html;charset=gb2312"%><html><title><%= application.getServerInfo() %></title><body>上传文件程序应用示例<form action="doUpload.jsp" method="post" enctype="multipart/form-data"><%-- 类型enctype用multipart/form-data,这样可以把文件中的数据作为流式数据上传,不管是什么文件类型,均可上传。--%>请选择要上传的文件<input type="file" name="upfile" size="50"><input type="submit" value="提交"></form></body></html>2、doUpload.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(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("<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(saveFile + "文件成功上载.</p>");}else{String content = request.getContentType();out.println("<p>上传的数据类型不是multipart/form-data</p>");}}catch(Exception ex){throw new ServletException(ex.getMessage());}%></body></html>文件下载功能,参考文章:http://blog.csdn.net/guomutian911/article/details/59211606。有问题,可联系:玉林师范学院,郭立,Q:316190672
新闻热点
疑难解答