JAVA学习提高之 FileUpload组件实现多文件上传(JSP+SERVLET)实现方法介绍



JAVA学习提高之---- FileUpload组件实现多文件上传(JSP+SERVLET)实现方法介绍。

引用: http://luoke920.javaeye.com/blog/271257
相关文章也可以看一下: http://www.jspcn.net/htmlnews/2005011.html http://www.jspcn.net/htmlnews/2005024.html http://www.jspcn.net/htmlnews/20050115.html 一文中使用FileUpload组件实现多文件的上传。。。 经个人测试,组件本身使用上没有问题,不过因为考虑客户端和服务器有可能是linux主机的原因,在具体调用方面我做了一些改进。。。 本教程以Apache组织的commons项目中的FileUpload项目做为jsp的文件上传组件,FileUpload项目完全尊守RFC1867规范中 关于在HTTP request 中通过Post方法提交文件的规范,该项目性能稳定快速,易于部署和使用. 本次教程以前端jsp + 后端 servlet的方式上传文件,你也可以完全在jsp中实现而不用servlet. 在开始之前你要准备以下几个东西: 1. commons-FileUpload 1.2.1 包 下载地址:http://jakarta.apache.org/commons/fileupload/ 具体地址:http://commons.apache.org/downloads/download_fileupload.cgi 下载Binary下的zip即可。 2. commons-IO 1.4 包 下载地址:http://jakarta.apache.org/commons/io/ 具体地址:http://commons.apache.org/downloads/download_io.cgi 下载Binary下的zip即可。 3. Commons-BeanUtils 1.8 包 下载地址:http://jakarta.apache.org/commons/beanutils/ 具体地址:http://commons.apache.org/downloads/download_beanutils.cgi 下载Binary下的zip即可。 好了下载完成后,解压得到5个jar包:分别是: commons-beanutils-1.8.0.jar commons-beanutils-bean-collections-1.8.0.jar commons-beanutils-core-1.8.0.jar commons-fileupload-1.2.1.jar commons-io-1.4.jar
配置好开发环境后:如果没配置好可以看: http://blog.csdn.net/luweifeng1983/archive/2008/12/23/3590726.aspx 用的是Eclipse3.4 + tomcat5.5并已有tomcat插件一起的。(jdk1.3或1.5的都可,已测试) 新建一个项目名叫Upload 建立好后将类文件输出目录改为:Upload/WebContent/WEB-INF/classes 接下来将上面5个jar包拷贝到WEB-INF/lib目录。。 !!说到这里要注意一个问题,那就是lib下的jar文件和eclipse工程下的jar放置的区别,一般你要使用你的代码拷到别的电脑上也能运行就把所有的jar都先拷到lib下,然后在eclipse的build path下点add jars将lib中的jar加进来就行了。。 下面就分别建一个jsp和一个servlet。。 如下:upload.jsp

  1. <%@ page language=”java” contentType=”text/html; charset=UTF-8″
  2.     pageEncoding=”UTF-8″%>
  3. <!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>
  4. <html>
  5. <head>
  6. <meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. <form name=”upform” action=”UploadServlet” method=”POST” enctype=”multipart/form-data”>
  11. <input type =”file” name=”file1″ id=”file1″/><br/>
  12. <input type =”file” name=”file2″ id=”file2″/><br/>
  13. <input type =”file” name=”file3″ id=”file3″/><br/>
  14. <input type=”submit” value=”Submit” /><br/>
  15. <input type=”reset” />
  16. </form>
  17. </body>
  18. </html>

UploadServlet.java

  1. package com.gobusiness.eus.servlet;
  2. import java.io.BufferedInputStream;
  3. import java.io.BufferedOutputStream;
  4. import java.io.File;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import javax.servlet.ServletException;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10. import org.apache.commons.fileupload.FileItemIterator;
  11. import org.apache.commons.fileupload.FileItemStream;
  12. import org.apache.commons.fileupload.disk.DiskFileItemFactory;
  13. import org.apache.commons.fileupload.servlet.ServletFileUpload;
  14. import org.apache.commons.fileupload.util.Streams;
  15. /**
  16. * Servlet implementation class for Servlet: UploadServlet
  17. *
  18. */
  19. public class UploadServlet extends javax.servlet.http.HttpServlet implements
  20.         javax.servlet.Servlet {
  21.     File tmpDir = null;// 初始化上传文件的临时存放目录
  22.     File saveDir = null;// 初始化上传文件后的保存目录
  23.     public UploadServlet() {
  24.         super();
  25.     }
  26.     protected void doGet(HttpServletRequest request,
  27.             HttpServletResponse response) throws ServletException, IOException {
  28.         doPost(request, response);
  29.     }
  30.     protected void doPost(HttpServletRequest request,
  31.             HttpServletResponse response) throws ServletException, IOException {
  32.         try {
  33.             if (ServletFileUpload.isMultipartContent(request)) {
  34.                 DiskFileItemFactory dff = new DiskFileItemFactory();// 创建该对象
  35.                 dff.setRepository(tmpDir);// 指定上传文件的临时目录
  36.                 dff.setSizeThreshold(1024000);// 指定在内存中缓存数据大小,单位为byte
  37.                 ServletFileUpload sfu = new ServletFileUpload(dff);// 创建该对象
  38.                 sfu.setFileSizeMax(5000000);// 指定单个上传文件的最大尺寸
  39.                 sfu.setSizeMax(10000000);// 指定一次上传多个文件的总尺寸
  40.                 FileItemIterator fii = sfu.getItemIterator(request);// 解析request
  41.                                                                     // 请求,并返回FileItemIterator集合
  42.                 while (fii.hasNext()) {
  43.                     FileItemStream fis = fii.next();// 从集合中获得一个文件流
  44.                     if (!fis.isFormField() && fis.getName().length() > 0) {// 过滤掉表单中非文件域
  45.                         String fileName = fis.getName().substring(
  46.                                 fis.getName().lastIndexOf(“//”));// 获得上传文件的文件名
  47.                         BufferedInputStream in = new BufferedInputStream(fis
  48.                                 .openStream());// 获得文件输入流
  49.                         BufferedOutputStream out = new BufferedOutputStream(
  50.                                 new FileOutputStream(new File(saveDir
  51.                                         + fileName)));// 获得文件输出流
  52.                         Streams.copy(in, out, true);// 开始把文件写到你指定的上传文件夹
  53.                     }
  54.                 }
  55.                 response.getWriter().println(“File upload successfully!!!”);// 终于成功了,还不到你的上传文件中看看,你要的东西都到齐了吗
  56.             }
  57.         } catch (Exception e) {
  58.             e.printStackTrace();
  59.         }
  60.     }
  61.     public void init() throws ServletException {
  62.         /* 对上传文件夹和临时文件夹进行初始化 */
  63.         super.init();
  64.         String tmpPath = “c://tmpdir”;
  65.         String savePath = “c://updir”;
  66.         tmpDir = new File(tmpPath);
  67.         saveDir = new File(savePath);
  68.         if (!tmpDir.isDirectory())
  69.             tmpDir.mkdir();
  70.         if (!saveDir.isDirectory())
  71.             saveDir.mkdir();
  72.     }
  73. }

web.xml

  1. <?xml version=”1.0″ encoding=”UTF-8″?>
  2. <web-app id=”WebApp_ID” version=”2.4″ xmlns=”http://java.sun.com/xml/ns/j2ee” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd”>
  3.     <display-name>
  4.     Upload</display-name>
  5.     <servlet>
  6.         <description>
  7.         </description>
  8.         <display-name>
  9.         UploadServlet</display-name>
  10.         <servlet-name>UploadServlet</servlet-name>
  11.         <servlet-class>
  12.         com.gobusiness.eus.servlet.UploadServlet</servlet-class>
  13.     </servlet>
  14.     <servlet-mapping>
  15.         <servlet-name>UploadServlet</servlet-name>
  16.         <url-pattern>/UploadServlet</url-pattern>
  17.     </servlet-mapping>
  18.     <welcome-file-list>
  19.         <welcome-file>index.html</welcome-file>
  20.         <welcome-file>index.htm</welcome-file>
  21.         <welcome-file>index.jsp</welcome-file>
  22.         <welcome-file>default.html</welcome-file>
  23.         <welcome-file>default.htm</welcome-file>
  24.         <welcome-file>default.jsp</welcome-file>
  25.     </welcome-file-list>
  26. </web-app>

好了,文件建立好后,在eclipse下直接部署再启动Tomcat访问upload.jsp再上传文件(这里可以一次上传三个文件),当然可以使用js实现提交更多的文件上传。。。 当然你也可以把你的upload项目整个拷贝到tomcat的webapps目录下,启动tomcat.然后 打开IE浏览器在地址栏中输入http://localhost:8080/upload/upload.jsp 如:我要上传的文件位于:D:/import file下,文件名分别为: CNIL0437.TXT GIDL0437.TXT MSGL0437.TXT 上传成功后,这三个文件将被上传到了c:/updir下面。。。,如果在c:/updir下面找到了这三个文件说明上传已经成功了。。 好了,上面的步骤其实http://luoke920.javaeye.com/blog/271257已经讲得详细了,只是我把自己的部署、测试加进去了而以。那么我现在要对它进行一些改进。。。 首先第一:

  1. #  public void init() throws ServletException {
  2. #         /* 对上传文件夹和临时文件夹进行初始化 */
  3. #         super.init();
  4. #         String tmpPath = “c://tmpdir”;
  5. #         String savePath = “c://updir”;
  6. #         tmpDir = new File(tmpPath);
  7. #         saveDir = new File(savePath);
  8. #         if (!tmpDir.isDirectory())
  9. #             tmpDir.mkdir();
  10. #         if (!saveDir.isDirectory())
  11. #             saveDir.mkdir();
  12. #
  13. #     }

即它创建tempdir和updir是在servlet的init方法里面创建的,我们知道init方法只在servlet第一次被加载的时候执行,那么第一次创建成功后,如果后来的程序对这个目录做了修改或删除,那么从些以后就再也不能创建这两个目录了。。。 所以对创建目录的工作应该放到post方法里面。。。
第二:我们应该注意到程序在服务器上创建目录然后把文件上传到这两个目录(先放到temdir再到updir),如果我的服务器是linux主机怎么办,能用?

  1.    1. String tmpPath = “c://tmpdir”;
  2.    2. String savePath = “c://updir”;

显然不能,所以创建目录时要使用File.separator. 不过如果实在不想用File.separaotr,至少也要用/updir,而不是使用//,因为//在linux下是肯定没用的。。
第三:下面语句用于获取上传文件的文件名,即D:/import file/CNIL0437.TXT,也就是说要获得CNIL0437.TXT

  1. String fileName = fis.getName().substring(
  2.          fis.getName().lastIndexOf(“//”));// 获得上传文件的文件名

但上面的语句实际上只针对window客户端才有用,因为实际上上面的语句如果客户端使用的是window系统,上传文件时fis.getName()返回的是D://import file//CNIL0437.TXT 而这里也有一个要注意的地方就是即使在windows的客户端得到的fileName也是/CNIL0437.TXT而不是CNIL0437.TXT,这里因为 举例:”abchang”.substring(2) = “chang”.也就是substring(n)实际上是取第n+1到最后的字符。。所以这里应该改为

  1. String fileName = fis.getName().substring(
  2.                         fis.getName().lastIndexOf(“//”)+1);// 获得上传文件的文件名,客户端为windows
  3.                             if(fis.getName().lastIndexOf(“//”) == -1){
  4.                                 fileName = fis.getName().substring(
  5.                                         fis.getName().lastIndexOf(“/”)+1);// 获得上传文件的文件名,客户端为linux或unix
  6.                             }

上面lastIndexOf当找不到“//”时默认返回值为-1
第四:下面语句

  1. BufferedOutputStream out = new BufferedOutputStream(
  2.                                 new FileOutputStream(new File(saveDir
  3.                                          + fileName)));// 获得文件输出流

上面saveDir是一个File的实例,而更奇怪的是这个程序确实可以运行,说实话我现在没有明白这个语句编译器没有报错:new File(saveDir + fileName) 因为File的构造函数中好象没有File(File+ String) 这么的构造函数。。这个问题??保留。。有看到这个问题并知道的可以解释一下。。。

  1. 上面提到的问题已解决了
  2. 这里使用的+号运算符实际上相当于saveDir.toString + String

jdk api:中只有new File(File,String)构造函数,经测试这里使用:new File(saveDir,fileName)也可以。 这里同样有使用File.separator的问题:这里因为上面取得的fileName是/CNIL0437.TXT,而我改正后的是CNIL0437.TXT。。所以这里也必须改。。 改正后为:


 

  1. new FileOutputStream(new File(savePath + File.separator + fileName)));// 获得文件输出流

注意我改成了savePath 因为savePath 是String类型的。。 好了这个程序到这里改得基本差不多了。。。

  1. 注:
  2. 这个程序里,我的上传文件的目录名和文件名都用的是英文,但实际当中可能使用中文名称,特别是文件名是中文的情况,经测试程序当文件名和目录名中有中文时获得的是乱码,这个问题有待解决,此贴名称前面加“!”以示待改进!
  1. 2008-12-26:
  2.    修正bug:当上传的文件目录或文件名为中文时提交过后用fis.getName()得到的是乱码
  3.    如:C:/Documents and Settings/zqwf/桌面/华为面试题.txt
  4.    如果不做任何处理理到的将是:
  5.        C:/Documents and Settings/zqwf/妗岄潰/鍗庝负闈㈣瘯棰�.txt
  6.    在网上看了一些有关上传文件时乱码的问题,也看了FileUpload组件的相关源代码,但后来认为不需要对源码做修改。。。。
  7.    再看我前面的一篇博文:http://blog.csdn.net/luweifeng1983/archive/2008/11/20/3342003.aspx
  8.    提到post提交的乱码处理方法直接在servlet中添加
  9.    request.setCharacterEncoding(“UTF-8″);即可解决问题

下面看看我实际应用的环境: 服务器:jboss3.2.0 eclipse 1.3.1 服务器:windows和linux都有,正因为这个所以上面改进了一些地方,总结起来就是在new File的时候里面应该使用File.separator,但在外面普通字符串来写目录的时候一般不要使用。。。 另外一般把目录写成相对目录,然后保存在属性文件中,注意的是:服务器不同,这两个属性文件配置的目录也有些不同。。好了看我下面的实现 sys.properties文件 windows服务器

  1. path.install        = c://GeTSmart
  2. path.web.app        = ../jboss-3.2.0/server/minimal/deploy/GeTSmart.war/WEB-INF
  3. path.doc.ie.imp     = ./csv
  4. path.doc.ie.imp.temp = ./tmpdir

linux服务器下只需改成以下就可以了。

  1. path.install        =/usr/local/getseus
  2. path.web.app        = ../jboss-3.2.1/server/minimal/deploy/GeTSmart.war/WEB-INF
  3. path.doc.ie.imp     = ./csv
  4. path.doc.ie.imp.temp = ./tmpdir

下面看我的servlet代码:

  1. try {
  2.                 String tmpPath = EusUtil.getFullPath(SysProp.getProperty(“path.doc.ie.imp.temp”));
  3.                 //String tmpPath = “c://tmpdir”;
  4.                 String savePath = EusUtil.getFullPath(SysProp.getProperty(“path.doc.ie.imp”));
  5.                 //String savePath = “c://updir”;
  6.                 tmpDir = new File(tmpPath);
  7.                 saveDir = new File(savePath);
  8.                 if (!tmpDir.isDirectory())
  9.                     tmpDir.mkdir();
  10.                 if (!saveDir.isDirectory())
  11.                     saveDir.mkdir();
  12. Logger.logDebug(className,”the savePath dir name is :” + savePath);
  13.                 String userId = userBean.getUserId();
  14.                 savePath =savePath +  File.separator + userId;
  15.                 saveDir = new File(savePath);
  16.                 if (!saveDir.isDirectory())
  17.                     saveDir.mkdir();
  18. Logger.logDebug(className,”the savePath dir and the userid name is :” + savePath);
  19.                 if (ServletFileUpload.isMultipartContent(req)) {
  20.                     DiskFileItemFactory dff = new DiskFileItemFactory();// 创建该对象
  21.                     dff.setRepository(tmpDir);// 指定上传文件的临时目录
  22.                     dff.setSizeThreshold(1024000);// 指定在内存中缓存数据大小,单位为byte
  23.                     ServletFileUpload sfu = new ServletFileUpload(dff);// 创建该对象
  24.                     sfu.setFileSizeMax(5000000);// 指定单个上传文件的最大尺寸
  25.                     sfu.setSizeMax(10000000);// 指定一次上传多个文件的总尺寸
  26.                     FileItemIterator fii = sfu.getItemIterator(req);// 解析request
  27.                                                                         // 请求,并返回FileItemIterator集合
  28.                     while (fii.hasNext()) {
  29.                         FileItemStream fis = fii.next();// 从集合中获得一个文件流
  30.                         if (!fis.isFormField() && fis.getName().length() > 0) {// 过滤掉表单中非文件域
  31.                             String fileName = fis.getName().substring(
  32.                                     fis.getName().lastIndexOf(“//”)+1);// 获得上传文件的文件名,客户端为windows
  33.                             if(fis.getName().lastIndexOf(“//”)<0){
  34.                                 fileName = fis.getName().substring(
  35.                                         fis.getName().lastIndexOf(“/”)+1);// 获得上传文件的文件名,客户端为linux或unix
  36.                             }
  37.                                     //fis.getName().lastIndexOf(File.separator)+1);// 获得上传文件的文件名
  38. Logger.logDebug(className, “get the File.separator is :” + File.separator);
  39. Logger.logDebug(className, “get the client import file path is <<fis.getName()>>:” + fis.getName());
  40. Logger.logDebug(className, “get the client import file path last index of // + 1 is <<fis.getName() index of >>:” + (fis.getName().lastIndexOf(“//”)+1));
  41. Logger.logDebug(className, “get the import file name is :” + fileName);
  42.                             BufferedInputStream in = new BufferedInputStream(fis
  43.                                     .openStream());// 获得文件输入流
  44.                             BufferedOutputStream out = new BufferedOutputStream(
  45.                                     //new FileOutputStream(new File(saveDir + fileName)));// 获得文件输出流
  46.                                     new FileOutputStream(new File(savePath + File.separator + fileName)));// 获得文件输出流
  47. Logger.logDebug(className, “get the outputStream file path is <<new File(savePath + File.separator + fileName)>>” + savePath + File.separator + fileName);
  48.                             Streams.copy(in, out, true);// 开始把文件写到你指定的上传文件夹
  49.                         }
  50.                     }
  51.                     //resp.getWriter().println(“File upload successfully!!!”);// 终于成功了,还不到你的上传文件中看看,你要的东西都到齐了吗

上面用到的方法:getFullPath

  1. public static String getFullPath(String path) {
  2.         //get install path
  3.         String sInstallPath=CommonUtil.null2String(SysProp.getProperty(“path.install”));
  4.         String sWebAppPath =CommonUtil.null2String(SysProp.getProperty(“path.web.app”));
  5.         if (path.trim().substring(0,2).equals(“..”)) {
  6.             return replaceNameSeparator(sInstallPath+path.trim().substring(2));
  7.         }
  8.         else if (path.trim().charAt(0)==’.') {
  9.             if (sWebAppPath.trim().substring(0,2).equals(“..”))
  10.                 return replaceNameSeparator(sInstallPath+sWebAppPath.trim().substring(2)+path.trim().substring(1));
  11.             else
  12.                 return replaceNameSeparator(sWebAppPath+path.trim().substring(1));
  13.         } else {
  14.             return replaceNameSeparator(path.trim());
  15.         }
  16.     }
  1. public static String replaceNameSeparator(String path)
  2.     {
  3.       String newString = “”;
  4.       if (path.indexOf(‘/’)!=-1)
  5.         newString = path.replace(‘/’, File.separatorChar);
  6.       else
  7.         newString = path;
  8.       if (newString.indexOf(‘//’)!=-1)
  9.         newString = newString.replace(‘//’, File.separatorChar);
  10.       return newString;
  11.     }

主要注意我提到的几个要改的地方就行了。。其实就是要考虑当客户端和服务器都可能是windows和linux的情况。 说大了就是编程要注意细节,要尽量去完善代码。。。这样才能使代码不会因环境变化一下就不能运行。。。。
前面讲过可以实现更多文件上传,这里只需要改一下jsp页面就行了,改进后的代码如下: upload.jsp

  1. <%@ page language=”java” contentType=”text/html; charset=UTF-8″
  2.     pageEncoding=”UTF-8″%>
  3. <!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>
  4. <html>
  5. <head>
  6. <meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
  7. <title>Insert title here</title>
  8. <script>
  9.         var file_to=0;
  10.         function add_inp()
  11.         {
  12.             newInp = document.createElement(“input”);
  13.             newInp.setAttribute(“id”,”file_”+file_to);
  14.             newInp.setAttribute(“type”,”file”);
  15.             newInp.setAttribute(“name”,”upfile”);
  16.             div1.appendChild(newInp);
  17.             newInp = document.createElement(‘<input type=”button” value=”删除” onclick=”del_inp(‘+file_to+’)”>’);
  18.             newInp.setAttribute(“id”,”file_del_”+file_to);
  19.             div1.appendChild(newInp);
  20.             newInp = document.createElement(‘<BR />’);
  21.             newInp.setAttribute(“id”,”file_br_”+file_to);
  22.             div1.appendChild(newInp);
  23.             file_to++;
  24.       }
  25.         function del_inp(file_id)
  26.         {
  27.             div1.removeChild(document.getElementById(“file_”+file_id));
  28.             div1.removeChild(document.getElementById(“file_del_”+file_id));
  29.             div1.removeChild(document.getElementById(“file_br_”+file_id));
  30.         }
  31. </script>
  32. </head>
  33. <body>
  34. <form name=”upform” action=”UploadServlet” method=”POST” enctype=”multipart/form-data”>
  35. <div id=div1>
  36. </div>
  37. <INPUT TYPE=”button” VALUE=”添加上传文件” onclick=add_inp()>
  38. <input type=”submit” value=”Submit” />
  39. <input type=”reset” />
  40. </form>
  41. </body>
  42. </html>

最近查看fileupload的api的时候发现有另一种迭代及写文件的方式如下:

  1. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  2.         String step = “”;
  3.         File tempDir = null;
  4.         File saveDir = null;
  5.         try{
  6.             request.setCharacterEncoding(“UTF-8″);
  7.             step = “upload file to c:/updir”;
  8.             String tempPath = “c:/tempdir”;
  9.             String savePath = “c:/updir”;
  10.             tempDir = ImportManager.mkdir(tempPath);
  11.             saveDir = ImportManager.mkdir(savePath);
  12.             int maxMemorySize = 1024 * 1024 * 1024;
  13.             int MaxSize = 100 * 1024 * 1024;
  14.             if(ServletFileUpload.isMultipartContent(request)){
  15.                 DiskFileItemFactory factory = new DiskFileItemFactory();
  16.                 factory.setSizeThreshold(maxMemorySize);
  17.                 factory.setRepository(tempDir);
  18.                 ServletFileUpload upload = new ServletFileUpload(factory);
  19.                 upload.setSizeMax(MaxSize);
  20.                 List items = upload.parseRequest(request);
  21.                 Iterator iter = items.iterator();
  22.                 while(iter.hasNext()){
  23.                     FileItem item = (FileItem)iter.next();
  24.                     if(!item.isFormField()){
  25.                         //item.isFormField()返回为ture时为其它的表单域如submit等,为false时才为上传的文件
  26.                         String fieldName = item.getFieldName();
  27.                         String value = item.getString();
  28.                         String fileName = item.getName();
  29.                         String contentType = item.getContentType();
  30.                         boolean isInMemory = item.isInMemory();
  31.                         long sizeInbytes = item.getSize();
  32.                         //取上传的文件名
  33.                         if(fileName.lastIndexOf(“//”) != -1){
  34.                             fileName = fileName.substring(fileName.lastIndexOf(“//”)+1);
  35.                         }else{
  36.                             fileName = fileName.substring(fileName.lastIndexOf(“/”)+1);
  37.                         }
  38.                         //保存文件到指定目录
  39.                         item.write(new File(saveDir + File.separator + fileName));
  40. //                      BufferedInputStream is = new BufferedInputStream(item.getInputStream());
  41. //                      BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(new File(savePath + File.separator + fileName)));
  42. //                      Streams.copy(is,os,true);
  43.                     }
  44.                 }
  45.             }
  46.         }catch(Exception e){
  47.         }finally{
  48.             request.getRequestDispatcher(“AckMessage.jsp”).forward(request, response);
  49.         }
  50.     }

上面主要是ServletFileUpload 类提供了返回List的方法parseRequest,而前面的例子使用的是该类返回FileItemIterator的getItemIterator方法

http://blog.csdn.net/luweifeng1983/article/details/3613544