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

  1. sad导入smartupload的jar包
  2. 提交到jsp页面上传
    1. 创建表单元素
      1. <!--method="post" enctype="multipart/form-data"   必须写死-->
        <form action="upload.jsp" method="post" enctype="multipart/form-data">
        	文件名:<input type="text" name="fileName"/><br/>                             
        	文件1:<input type="file" name="myFile1"/><br/>                              
         	    <!--文件2:<input type="file" name="myFile2"/><br/> -->                      
         		<!--文件3:<input type="file" name="myFile3"/><br/> -->                      
         		<!--文件4:<input type="file" name="myFile4"/><br/> -->                      
        	<input type="submit" value="提交"/>                                         
        </form>                                                                       

         

    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<files.getCount();i++){                                                         
        	File file=files.getFile(i);//获取第i个file                                                   
        	String inputName=file.getFieldName();//<input/>标签的表单元素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. <!--method="post" enctype="multipart/form-data"   必须写死-->
        <form action="SmartUploadServlet" method="post" enctype="multipart/form-data">
        	文件名:<input type="text" name="fileName"/><br/>                             
        	文件1:<input type="file" name="myFile1"/><br/>                              
         	    <!--文件2:<input type="file" name="myFile2"/><br/> -->                      
         		<!--文件3:<input type="file" name="myFile3"/><br/> -->                      
         		<!--文件4:<input type="file" name="myFile4"/><br/> -->                      
        	<input type="submit" value="提交"/>                                         
        </form>             

         

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

         

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值