`
mqzsweet
  • 浏览: 132808 次
  • 性别: Icon_minigender_1
  • 来自: 江苏
社区版块
存档分类
最新评论

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

    博客分类:
  • JSP
阅读更多

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. }   


分享到:
评论

相关推荐

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

    博文链接:https://mqzsweet.iteye.com/blog/141965

    利用commons-fileupload组件实现文件上传-实例

    源码 博文链接:https://lisanlai.iteye.com/blog/660238

    研究论文-Struts2架构中的Commons-fileupload组件应用技术.pdf

    基于HTTP传输协议的Web网页中,采用嵌入Commonsfileupload组件的Struts2框架,实现了文件的上传与下载.利用OGNL表达式提取Session持久层中的文件名,并利用此文件名实现下载文件的动态更名.灵活利用struts2.0自身...

    common-fileupload

    利用commons-fileupload组件实现的java的文件的上传与下载。

    Struts2架构中的Commons-fileupload组件应用技术 (2011年)

    基于HTTP传输协议的Web网页中,采用嵌入Commons-fileupload组件的Struts2框架,实现了文件的上传与下载。利用OGNL表达式提取Session持久层中的文件名,并利用此文件名实现下载文件的动态更名。灵活利用struts2.0自身...

    利用 FormData 对象和 Spring MVC 配合实现Ajax文件下载功能

    利用 FormData 对象和 Spring MVC 配合可以实现Ajax文件上载功能: 步骤 1.导入组件并准备静态脚本 &lt;groupId&gt;commons-fileupload &lt;artifactId&gt;commons-fileupload &lt;version&gt;1.3.2 &lt;h1&gt;Ajax 文件上载 &lt;...

    web项目常用jar包及说明.zip

    1.commons-fileupload.jar(commons项目中的关于文件上传的包, struts2.1.6版本后必须加入此文件) 2.commons-io.jar(commons项目(commons项目就是java中一些常用的公共的组件)的io子项目,是处理异常的) 3....

    SSH 项目 整合jar包

    1.commons-fileupload.jar(commons项目中的关于文件上传的包, struts2.1.6版本后必须加入此文件) 2.commons-io.jar(commons项目(commons项目就是java中一些常用的公共的组件)的io子项目,是处理异常的) 3....

    SSH 框架所需JAR包

    1.commons-fileupload.jar(commons项目中的关于文件上传的包, struts2.1.6版本后必须加入此文件) 2.commons-io.jar(commons项目(commons项目就是java中一些常用的公共的组件)的io子项目,是处理异常的) 3....

    web开发常用jar

    Apache Commons包中的一个,java.io.*的扩展,输入输出,支持文件上传 commons-fileupload.jar Apache Commons包中的一个,是一个通过Http接收上传的文件并处理结果文件的库 dom4j-1.4.jar 和 jaxen-1.1.1.jar ...

    java开发常用jar包

    Apache Commons包中的一个,java.io.*的扩展,输入输出,支持文件上传 commons-fileupload.jar Apache Commons包中的一个,是一个通过Http接收上传的文件并处理结果文件的库 dom4j-1.4.jar 和 jaxen-1.1.1.jar 是一...

    JAVA WEB典型模块与项目实战大全

    15.2 文件上传组件fileupload  15.3 初步使用文件上传组件(components-fileupload)  15.4 单文件的上传  15.5 多文件的上传  15.6 小结  第16章 网上投票系统(struts 2.x+hfreechart)  ]6.1 网上投票...

Global site tag (gtag.js) - Google Analytics