servlet 通过 FileItem 实现多文件上传

【本文简介】

一个servlet 多文件上传的简单例子。

 

【依赖包】

  1. commons-fileupload-1.3.1.jar
  2. commons-io-2.2.jar

【依赖包下载】

  1. commons-fileupload-1.3.1.jar:http://commons.apache.org/proper/commons-fileupload/download_fileupload.cgi
  2. commons-io-2.2.jar:http://commons.apache.org/proper/commons-io/download_io.cgi

 

【文件夹结构】

 

【HTML表单】

1   <body>
2     <form action="UploadServlet" method="post" enctype="multipart/form-data">
3         <input name="file" type="file" />
4         <input name="file" type="file" />
5         <input type="submit" value="上传"/>
6     </form>
7   </body>

 

【java代码】

 1 package com.zjm.www.servlet;
 2 
 3 import java.io.File;
 4 import java.io.IOException;
 5 import java.io.PrintWriter;
 6 import java.util.Iterator;
 7 
 8 import javax.servlet.ServletException;
 9 import javax.servlet.http.HttpServlet;
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpServletResponse;
12 
13 import org.apache.commons.fileupload.FileItem;
14 import org.apache.commons.fileupload.FileItemFactory;
15 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
16 import org.apache.commons.fileupload.servlet.ServletFileUpload;
17  
18 /**
19   * @描述 servlet 通过 FileItem 实现多文件上传
20   * @作者   小M
21   * @博客 http://www.cnblogs.com/xiaoMzjm/
22   * @时间 2014/07/30
23   */
24 public class UploadServlet extends HttpServlet {
25  
26     private static final long serialVersionUID = 1L;
27  
28     public void doGet(HttpServletRequest request, HttpServletResponse response)
29             throws ServletException, IOException {
30         doPost(request, response);
31     }
32  
33     @SuppressWarnings("unchecked")
34     public void doPost(HttpServletRequest request, HttpServletResponse response)
35             throws ServletException, IOException {
36          
37         // 转码,建议使用过滤器转码
38         request.setCharacterEncoding("utf-8");
39         response.setCharacterEncoding("utf-8");
40         response.setContentType("text/html;charset=utf-8");
41  
42         // 判断提交过来的表单是否为文件上传菜单,只适用于servlet
43         boolean isMultipart = ServletFileUpload.isMultipartContent(request);
44          
45         if (isMultipart) {
46             // 构造一个文件上传处理对象
47             FileItemFactory factory = new DiskFileItemFactory();
48             ServletFileUpload upload = new ServletFileUpload(factory);
49  
50             Iterator<FileItem> items;
51              
52             try {
53                 // 解析表单中提交的所有文件内容
54                 items = upload.parseRequest(request).iterator();
55                  
56                 while (items.hasNext()) {
57                      
58                     FileItem item = (FileItem) items.next();
59                      
60                     if (!item.isFormField()) {
61                          
62                         // 取出上传文件的文件名称
63                         String name = item.getName();
64                         
65                         // 取得上传文件以后的存储路径 
66                         String fileName = name.substring(
67                                 name.lastIndexOf('\\') + 1, name.length());
68                         
69                         // 上传文件以后的存储路径 , 有关路径问题,请参考另一篇博文:http://www.cnblogs.com/xiaoMzjm/p/3878758.html
70                         String path = request.getServletContext().getRealPath("/")+"\\UpLoadFile"
71                                 + File.separatorChar + fileName;
72  
73                         // 上传文件
74                         File uploaderFile = new File(path);
75                         item.write(uploaderFile);
76                          
77                         // 打印上传成功信息
78                         PrintWriter out = response.getWriter();
79                         out.print("success");
80                     }
81                 }
82             } catch (Exception e) {
83                 e.printStackTrace();
84             }
85         }
86     }
87 }

 

posted @ 2014-07-30 20:05  小M的博客  阅读(1014)  评论(0编辑  收藏  举报