利用commons-fileupload上传组件上传文件(二)

FileUpload.java  上传处理页面(Servlet) 

 

java 代码
  1. package it;   
  2.   
  3. import java.io.*;   
  4. import javax.servlet.*;   
  5. import javax.servlet.http.*;   
  6. import java.util.*;   
  7. import java.util.regex.*;   
  8. import org.apache.commons.fileupload.*;   
  9. import org.apache.commons.fileupload.disk.*;   
  10. import org.apache.commons.fileupload.servlet.*;   
  11.   
  12.   
  13. public class FileUpload extends HttpServlet {   
  14.  private String uploadPath = "d:\\addnetFile\\";  
  15.  private File tempPath = new File("c:\\temp\\");  
  16.  
  17.  public void destroy() {  
  18.   super.destroy(); // Just puts "destroy" string in log  
  19.   // Put your code here  
  20.  }  
  21.  
  22.  /**  
  23.   * The doGet method of the servlet. <br>  
  24.   *  
  25.   * This method is called when a form has its tag value method equals to get.  
  26.   *   
  27.   * @param request the request send by the client to the server  
  28.   * @param response the response send by the server to the client  
  29.   * @throws ServletException if an error occurred  
  30.   * @throws IOException if an error occurred  
  31.   */  
  32.  public void doGet(HttpServletRequest request, HttpServletResponse response)  
  33.    throws ServletException, IOException {  
  34.   response.setContentType("text/html;charset=gb2312");  
  35.   PrintWriter out = response.getWriter();  
  36.     
  37.   DiskFileItemFactory factory = new DiskFileItemFactory();  
  38.     
  39.   factory.setSizeThreshold(1048576);  
  40.   factory.setRepository(tempPath);  
  41.     
  42.   ServletFileUpload upload = new ServletFileUpload(factory);  
  43.     
  44.   upload.setSizeMax(1073741824);  
  45.     
  46.   List fileitems = null;  
  47.     
  48.     
  49.   try{  
  50.    fileitems =upload.parseRequest(request);  
  51.    Iterator iter = fileitems.iterator();  
  52.      
  53.      
  54.    String regExp = ".+\\\\(.+)";  
  55.    String[] errorType = {".exe",".com",".cgi",".asp"};  
  56.      
  57.    Pattern p = Pattern.compile(regExp);  
  58.      
  59.    while(iter.hasNext()){  
  60.     FileItem item = (FileItem)iter.next();  
  61.       
  62.     if(!item.isFormField()){  
  63.      String name = item.getName();  
  64.      long size = item.getSize();  
  65.        
  66.  
  67.      if((name == null||"".equals(name))&&size == 0)  
  68.       continue;  
  69.      Matcher m = p.matcher(name);  
  70.      boolean result = m.find();  
  71.        
  72.        
  73.      if(result){  
  74.       for(int temp = 0;temp<errorType.length;temp++){  
  75.        if(m.group(1).endsWith(errorType[temp])){  
  76.         throw new IOException(name +"wrong type");  
  77.        }  
  78.       }  
  79.         
  80.       try{  
  81.        item.write(new File(uploadPath,m.group(1)));  
  82.        out.println(name+"&nbsp&nbsp&nbsp&nbsp"+size+"<br>");  
  83.       }catch(Exception e){  
  84.        out.println("333"+e);  
  85.       }  
  86.      }  
  87.      else{  
  88.       throw new IOException("fail to upload");  
  89.      }  
  90.     }  
  91.    }  
  92.   }catch(IOException e1){  
  93.    out.println("222"+e1);  
  94.   }catch(FileUploadException e2){  
  95.    out.println("111"+e2);   
  96.   }   
  97.  }   
  98.   
  99.  /**  
  100.   * The doPost method of the servlet. <br>  
  101.   *  
  102.   * This method is called when a form has its tag value method equals to post.  
  103.   *   
  104.   * @param request the request send by the client to the server  
  105.   * @param response the response send by the server to the client  
  106.   * @throws ServletException if an error occurred  
  107.   * @throws IOException if an error occurred  
  108.   */  
  109.  public void doPost(HttpServletRequest request, HttpServletResponse response)   
  110.    throws ServletException, IOException {   
  111.   doGet(request,response);   
  112.  }   
  113.   
  114.  /**  
  115.   * Initialization of the servlet. <br>  
  116.   *  
  117.   * @throws ServletException if an error occure  
  118.   */  
  119.  public void init() throws ServletException {   
  120.   if(!new File(uploadPath).isDirectory()){   
  121.    new File(uploadPath).mkdir();   
  122.   }   
  123.      
  124.   if(!tempPath.isDirectory()){   
  125.    tempPath.mkdir();   
  126.   }   
  127.  }   
  128.   
  129. }   


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值