下载地址: http://www.servlets.com/cos/ 单文件上传JSP <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css"> --> </head> <body> <form action="FileUpload" method="post" enctype="multipart/form-data"> <table border="0"> <tr> <td valign="top"> <strong>Please choose your document:</strong> <br> </td> <td> <input type="file" name="file1"> <br> </td> </tr> <tr> <td> <input type="submit" value="Upload File"> </td> </tr> </table> </form> </body> </html> 处理的servlet package com.icss.servlet; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.oreilly.servlet.MultipartRequest; import java.util.Enumeration; import com.oreilly.servlet.multipart.DefaultFileRenamePolicy; public class FileUpload extends HttpServlet { /** * Constructor of the object. */ private String webTempPath; public FileUpload() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request * the request send by the client to the server * @param response * the response send by the server to the client * @throws ServletException * if an error occurred * @throws IOException * if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to * post. * * @param request * the request send by the client to the server * @param response * the response send by the server to the client * @throws ServletException * if an error occurred * @throws IOException * if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/plain"); request.setCharacterEncoding("GBK"); response.setCharacterEncoding("GBK"); // file limit size of 5 MB MultipartRequest mpr = new MultipartRequest(request, webTempPath, 120 * 1024 * 1024, "gb2312", new DefaultFileRenamePolicy()); // System.out.println("The file length // is::"+mpr.getFile(webTempPath).length()); Enumeration params = mpr.getFileNames(); // mpr.getParameter("name"); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<head>"); out.println("<title>Servlet upload</title>"); out.println("</head>"); out.println("<body>"); for (int i = 1; params.hasMoreElements(); i++) { String src = new String(mpr.getFilesystemName((String) params .nextElement())); // System.out.println(mpr.getFilesystemName((String)params.nextElement()).length()); File file = new File(webTempPath + "/" + src); System.out.println("The file length is::" + file.length()); System.out.println(src); out.println("The name of uploaded file " + i + " is: " + src + "<br><br>"); } out.println("</body>"); out.println("</html>"); } /** * Initialization of the servlet. <br> * * @throws ServletException * if an error occurs */ public void init() throws ServletException { // Put your code here webTempPath = getServletContext().getRealPath("/") + "upload"; } } 多文件上传的JSP <%@ page language="java" import="java.util.*" pageEncoding="gb2312"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'cos2f.jsp' starting page</title> <meta http-equiv="Content-Type" content="text/html;charset=gb2312"> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css"> --> </head> <body> <!-- enctype的值很重要,upload.jsp为处理上传的jsp--> <form name="form1" method="post" enctype="multipart/form-data" action="upload.jsp"> <p> <input name="file1" type="file"> </p> <p> <input name="file2" type="file"> </p> <p> <input name="file3" type="file"> </p> <p> <input type="submit" name="Submit" value="上传"> </p> </form > </body> </html> 处理的JSP <%@ page language="java" import="java.util.*" pageEncoding="gb2312"%> <%@ page import="java.util.Enumeration"%> <%@ page import="com.oreilly.servlet.MultipartRequest" %> <%@ page import="java.io.File" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'upload.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css"> --> </head> <body> <% //文件上传后,保存在d://workspace//upload String saveDirectory = getServletContext().getRealPath("/") + "upload"; //每个文件最大5m,最多3个文件 int maxPostSize = 3 * 5 * 1024 * 1024; //response的编码为"gb2312",同时采用缺省的文件名冲突解决策略,实现上传 MultipartRequest multi = new MultipartRequest(request, saveDirectory, maxPostSize, "gb2312"); //输出反馈信息 Enumeration file = multi.getFileNames(); while (file.hasMoreElements()) { System.out.println("CCC"); String name = (String)file.nextElement(); File f = multi.getFile(name); if(f != null) { String fileName = multi.getFilesystemName(name); String lastFileName = saveDirectory + "//" + fileName; out.println("upload file:"+lastFileName); out.println("<br>"); } } %> </body> </html>