② com.oreilly.servlet,可以在http://www.servlets.com/cos/下载
DiskFileUpload已经在javadoc下已经被标记为过期的方法,建议用ServletFileUpload代替,
而PortletFileUpload需要配合portlet-api来使用,所以这里我们只介绍ServletFileUpload,并且这个也是最常用的。
com.oreilly.servlet也提供了三种文件上传的处理方式:MultipartWrapper、MultipartRequest和MultipartParser三种方式,我们暂时称上面三种文件上传方式分别为:ServletFileUpload方式(MultipartTestServlet)、
MultipartRequest方式(MultipartTestServlet2)、MultipartParser方式(MultipartTestServlet3)
代码如下:
上传页面(test.html):
<%@ page language="java" import="java.util.*" contentType="text/html;charset=gbk" pageEncoding="gbk"%> <html> <body> <form action="MultipartTestServlet" enctype="multipart/form-data" method="post"> <input type="text" name="username" /><br /> <input type="file" name="myfile" /><br/> <input type="file" name="myfile" /><br/> <input type="submit" /> </form> <br/><br/><br/><br/> <form action="MultipartTestServlet2" enctype="multipart/form-data" method="post"> <input type="text" name="username" /><br /> <input type="file" name="myfile" /><br/> <input type="file" name="myfile" /><br/> <input type="submit" /> </form> <br/><br/><br/><br/> <form action="MultipartTestServlet3" enctype="multipart/form-data" method="post"> <input type="text" name="username" /><br /> <input type="file" name="myfile" /><br/> <input type="file" name="myfile" /><br/> <input type="submit" /> </form> </body> </html>ServletFileUpload方式(MultipartTestServlet.java)
package com.bug.servlet; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUpload; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.RequestContext; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.commons.fileupload.servlet.ServletRequestContext; public class MultipartTestServlet extends HttpServlet { public MultipartTestServlet() { super(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("gbk"); RequestContext requestContext = new ServletRequestContext(request); if(FileUpload.isMultipartContent(requestContext)){ DiskFileItemFactory factory = new DiskFileItemFactory(); factory.setRepository(new File("c:/tmp/")); ServletFileUpload upload = new ServletFileUpload(factory); //upload.setHeaderEncoding("gbk"); upload.setSizeMax(2000000); List items = new ArrayList(); try { items = upload.parseRequest(request); } catch (FileUploadException e1) { System.out.println("文件上传发生错误" + e1.getMessage()); } Iterator it = items.iterator(); while(it.hasNext()){ FileItem fileItem = (FileItem) it.next(); if(fileItem.isFormField()){ // 检查当前项目是普通表单域还是上传文件。 System.out.println(fileItem.getFieldName() + " " + fileItem.getName() + " " + new String(fileItem.getString().getBytes("iso8859-1"), "gbk")); }else{ System.out.println(fileItem.getFieldName() + " " + fileItem.getName() + " " + fileItem.isInMemory() + " " + fileItem.getContentType() + " " + fileItem.getSize()); if(fileItem.getName()!=null && fileItem.getSize()!=0){ File fullFile = new File(fileItem.getName()); File newFile = new File("c:/temp/" + fullFile.getName()); try { fileItem.write(newFile); } catch (Exception e) { e.printStackTrace(); } }else{ System.out.println("文件没有选择 或 文件内容为空"); } } } } } }
package com.bug.servlet; import java.io.IOException; import java.util.Enumeration; 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 com.oreilly.servlet.multipart.DefaultFileRenamePolicy; public class MultipartTestServlet2 extends HttpServlet { public MultipartTestServlet2() { super(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //request.setCharacterEncoding("gbk"); 不起作用 System.out.println("start "); MultipartRequest multi = new MultipartRequest(request, "c:/tmp/", 2*1024*1024, "gbk", new DefaultFileRenamePolicy()); System.out.println("start "); Enumeration filesName = multi.getFileNames(); Enumeration paramsName = multi.getParameterNames(); while(paramsName.hasMoreElements()){ String paramName = (String) paramsName.nextElement(); System.out.println(multi.getParameter(paramName)); } while(filesName.hasMoreElements()){ String fileName = (String) filesName.nextElement(); System.out.println(multi.getFilesystemName(fileName) + " " + multi.getOriginalFileName(fileName) + " " + multi.getContentType(fileName) + " "); if(multi.getFilesystemName(fileName)!=null && !multi.getFilesystemName(fileName).equals("")) System.out.println(multi.getFile(fileName).toURI()); } } }
package com.bug.servlet; import java.io.File; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.oreilly.servlet.multipart.FilePart; import com.oreilly.servlet.multipart.MultipartParser; import com.oreilly.servlet.multipart.ParamPart; import com.oreilly.servlet.multipart.Part; public class MultipartTestServlet3 extends HttpServlet { public MultipartTestServlet3() { super(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { MultipartParser mp = new MultipartParser(request, 2*1024*1024, false, false, "gbk"); Part part; while ((part = mp.readNextPart()) != null) { String name = part.getName(); if (part.isParam()) { ParamPart paramPart = (ParamPart) part; String value = paramPart.getStringValue(); System.out.println("param: name=" + name + "; value=" + value); } else if (part.isFile()) { // it's a file part FilePart filePart = (FilePart) part; String fileName = filePart.getFileName(); if (fileName != null) { long size = filePart.writeTo(new File("c:/tmp/")); System.out.println("file: name=" + name + "; fileName=" + fileName + ", filePath=" + filePart.getFilePath() + ", contentType=" + filePart.getContentType() + ", size=" + size); } else { System.out.println("file: name=" + name + "; EMPTY"); } System.out.flush(); } } } }
<servlet> <servlet-name>MultipartTestServlet</servlet-name> <servlet-class>com.bug.servlet.MultipartTestServlet</servlet-class> </servlet> <servlet> <servlet-name>MultipartTestServlet2</servlet-name> <servlet-class>com.bug.servlet.MultipartTestServlet2</servlet-class> </servlet> <servlet> <servlet-name>MultipartTestServlet3</servlet-name> <servlet-class>com.bug.servlet.MultipartTestServlet3</servlet-class> </servlet> <servlet-mapping> <servlet-name>MultipartTestServlet</servlet-name> <url-pattern>/MultipartTestServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>MultipartTestServlet2</servlet-name> <url-pattern>/MultipartTestServlet2</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>MultipartTestServlet3</servlet-name> <url-pattern>/MultipartTestServlet3</url-pattern> </servlet-mapping>