使用smartUpload进行文件的上传(两种情况:1.在另一个jsp页面取值上传,2.在servlet中取值上传)

  1. sad导入smartupload的jar包
  2. 提交到jsp页面上传
    1. 创建表单元素
      1. 
        
        文件名:
        文件1:

         

    2. 提交到upload.jsp页面
      1. <%@page import="com.jspsmart.upload.*"%>          
        <%@page import="com.jspsmart.upload.SmartUpload"%>
        
        
        //获取SmartUpload对象  注意是import="com.jspsmart.upload.SmartUpload"
        SmartUpload smartUpload=new SmartUpload();                                                   
        //初始化页面的上下文对象                                                                                
        smartUpload.initialize(pageContext);                                                         
        //设置允许上传的文件格式                                                                                
        smartUpload.setAllowedFilesList("jpg,png,gif");                                              
        //设置不允许上传的文件格式                                                                               
        smartUpload.setDeniedFilesList("docx");                                                      
        //设置单个上传文件的最大大小(20M,默认为2M)                                                                   
        smartUpload.setMaxFileSize(1024*1024*20);                                                    
        //设置总共文件允许的大小                                                                                
        smartUpload.setTotalMaxFileSize(1024*1024*1024*2);                                           
        try{                                                                                         
        	smartUpload.upload();//上传                                                                
        }catch(Exception e){                                                                         
        	e.printStackTrace();                                                                     
        }                                                                                            
        Files files=smartUpload.getFiles();//获取所有的file文件                                             
        for(int i=0;i标签的表单元素name属性的名称                          
        	String fileName=file.getFileName();//文件本身的名称                                             
        	file.saveAs("img/"+fileName,smartUpload.SAVE_AUTO);//保存在tomcat服务器WebRoot下的img文件夹         
        	file.saveAs("d://"+fileName,smartUpload.SAVE_AUTO);//保存到电脑本地d盘下                          
        }         
                                                                                                                                       

         

  3. 提交到Servlet中上传
    1.  创建表单
      1. 
        
        文件名:
        文件1:

         

    2. 提交到SmartUploadServlet.java
      1.  
        import javax.servlet.ServletException;        
        import javax.servlet.http.HttpServlet;        
        import javax.servlet.http.HttpServletRequest; 
        import javax.servlet.http.HttpServletResponse;
        import javax.servlet.jsp.JspFactory;          
        import javax.servlet.jsp.PageContext;         
        
        import com.jspsmart.upload.File;       
        import com.jspsmart.upload.Files;      
        import com.jspsmart.upload.SmartUpload;
        
        response.setContentType("text/html;charset=utf-8");                                            
        response.setCharacterEncoding("utf-8");                                                        
        request.setCharacterEncoding("utf-8");                                                         
        PrintWriter out = response.getWriter();                                                        
        //获取smartupload对象                                                                              
        SmartUpload smartUpload=new SmartUpload();                                                     
        //pageContext是jsp内置对象,需要处理一下                                                                   
        //方法一:                                                                                         
        JspFactory _jspxFactory = JspFactory.getDefaultFactory();                                      
        PageContext pageContext = _jspxFactory.getPageContext(this,request,response,"",true,8192,true);
        smartUpload.initialize(pageContext);//初始化                                                      
                                                                                                       
        //方法二:                                                                                         
        smartUpload.initialize(this, request, response);//初始化                                          
        try {                                                                                          
        	smartUpload.upload();//上传                                                                  
        } catch (Exception e) {                                                                        
        	e.printStackTrace();                                                                       
        }                                                                                              
        Files files = smartUpload.getFiles();//获取所有的file集                                              
        for (int i = 0; i < files.getCount(); i++) {                                                   
        	File file = files.getFile(i);//获取第i个file(单个)                                               
        	String fileName = file.getFileName();//获取的是文件的名称(带后缀名)                                     
        	String fieldName = file.getFieldName();//获取的是input控件的name="名称"                             
        	file.saveAs("img/"+fileName,smartUpload.SAVE_AUTO);//保存到服务器WebRoot下的img文件夹                 
        	file.saveAs("d:/"+fileName,smartUpload.SAVE_AUTO);//保存到本地磁盘d盘                              
        }                                                                                                 

         

你可能感兴趣的:(jsp基本)