多文件上传的后台代码实现

来源:互联网 发布:要怎么加盟农村淘宝 编辑:程序博客网 时间:2024/05/17 06:28

多文件上传后台代码的实现:


  1. /** 
  2.  * 文件上传实例 
  3.  * @author samLee 
  4.  * 
  5.  */  
  6. public class UploadPhotoServlet extends HttpServlet {  
  7.     private static final long serialVersionUID = 1L;  
  8.          
  9.     public UploadPhotoServlet() {  
  10.         super();  
  11.     }  
  12.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  13.         this.doPost(request, response);  
  14.     }  
  15.   
  16.     @SuppressWarnings("unchecked")  
  17.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  18.           
  19.         //文件存放的目录  
  20.         File tempDirPath =new File(request.getSession().getServletContext().getRealPath("/")+"\\upload\\");  
  21.         if(!tempDirPath.exists()){  
  22.             tempDirPath.mkdirs();  
  23.         }  
  24.           
  25.         //创建磁盘文件工厂  
  26.         DiskFileItemFactory fac = new DiskFileItemFactory();      
  27.         //创建servlet文件上传组件  
  28.         ServletFileUpload upload = new ServletFileUpload(fac);      
  29.         //文件列表  
  30.         List fileList = null;      
  31.         //解析request从而得到前台传过来的文件  
  32.         try {      
  33.             fileList = upload.parseRequest(request);      
  34.         } catch (FileUploadException ex) {      
  35.             ex.printStackTrace();      
  36.             return;      
  37.         }   
  38.         //保存后的文件名  
  39.         String imageName = null;  
  40.         //便利从前台得到的文件列表  
  41.         Iterator<FileItem> it = fileList.iterator();     
  42.         while(it.hasNext()){      
  43.             FileItem item =  it.next();     
  44.             //如果不是普通表单域,当做文件域来处理  
  45.             if(!item.isFormField()){  
  46.             imageName = new Date().getTime()+Math.random()*10000+item.getName();  
  47.                 BufferedInputStream in = new BufferedInputStream(item.getInputStream());     
  48.                 BufferedOutputStream out = new BufferedOutputStream(        
  49.                         new FileOutputStream(new File(tempDirPath+"\\"+imageName)));  
  50.                 Streams.copy(in, out, true);  
  51.                   
  52.             }  
  53.         }  
  54.         //  
  55.         PrintWriter out = null;  
  56.         try {  
  57.             out = encodehead(request, response);  
  58.         } catch (IOException e) {  
  59.             e.printStackTrace();  
  60.         }  
  61.         //这个地方不能少,否则前台得不到上传的结果  
  62.         out.write("1");  
  63.         out.close();   
  64.     }  
  65.       
  66.     /** 
  67.      * Ajax辅助方法 获取 PrintWriter 
  68.      * @return 
  69.      * @throws IOException  
  70.      * @throws IOException  
  71.      * request.setCharacterEncoding("utf-8"); 
  72.         response.setContentType("text/html; charset=utf-8"); 
  73.      */  
  74.     private PrintWriter encodehead(HttpServletRequest request,HttpServletResponse response) throws IOException{  
  75.         request.setCharacterEncoding("utf-8");  
  76.         response.setContentType("text/html; charset=utf-8");  
  77.         return response.getWriter();  
  78.     }  
  79. }  


0 0
原创粉丝点击