Servlet多文件打包下载

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用

Servlet代码

    package project;  
      
    import java.io.File;  
    import java.io.FileInputStream;  
    import java.io.IOException;  
    import java.util.Date;  
      
    import javax.servlet.ServletException;  
    import javax.servlet.http.HttpServlet;  
    import javax.servlet.http.HttpServletRequest;  
    import javax.servlet.http.HttpServletResponse;  
    import java.util.zip.*;  
    /** 
     * Servlet实现多文件打包下载 
     * @author Frank 
     * 
     */  
    public class Download extends HttpServlet {  
      
        /** 
         *  
         */  
        private static final long serialVersionUID = -1379339763454618902L;  
      
        public Download() {  
            super();  
        }  
          
        /** 
         * doPost提交方式 
         */  
        public void doPost(HttpServletRequest request, HttpServletResponse response)  
                throws ServletException, IOException {  
            this.doGet(request, response);  
        }  
          
        /** 
         * doGet提交方式 
         */  
        public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {  
            response.setContentType("APPLICATION/OCTET-STREAM");  
            response.setHeader("Content-Disposition", "attachment; filename="+ this.getZipFilename());  
            System.out.println("in BatchDownload................");  
            ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());  
            File[] files = new File[3];  
            /*这里可以根据前台进行赋值,我这样做事为了快速做测试而已 */  
            files[0] = new File("F:/1111.png");  
            files[1] = new File("F:/11.png");  
            files[2] = new File("D:/testduo");  
            for(File f:files){  
                zipFile(f, "", zos);  
            }  
            zos.flush();  
            zos.close();  
        }  
          
        /** 
         * 使用递归进行文件夹,文件的扫描 
         */  
        private void zipFile(File subs, String baseName, ZipOutputStream zos)throws IOException {  
            if(subs.exists()){  
                if(subs.isFile()){  
                    zos.putNextEntry(new ZipEntry(baseName + subs.getName()));  
                    FileInputStream fis = new FileInputStream(subs);  
                    byte[] buffer = new byte[1024];  
                    int r = 0;  
                    while ((r = fis.read(buffer)) != -1) {  
                        zos.write(buffer, 0, r);  
                    }  
                    fis.close();  
                }else{  
                    //如果是目录。递归查找里面的文件  
                    String dirName = baseName + subs.getName() + "/";  
                    zos.putNextEntry(new ZipEntry(dirName));  
                    File[] sub = subs.listFiles();  
                    for (File f : sub) {  
                        zipFile(f, dirName, zos);  
                    }  
                }  
            }  
          
        }  
      
        /** 
         * 获取zip文件名 
         * @return 
         */  
        private String getZipFilename() {  
            Date date = new Date();  
            String s = date.getTime() + ".zip";  
            return s;  
        }  
      
    }  

Web.xml配置
    <?xml version="1.0" encoding="utf-8" ?>  
    <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">  
    <web-app>       
      <servlet>  
            <servlet-name>Down</servlet-name>  
            <servlet-class>project.Download</servlet-class>  
      </servlet>  
      <servlet-mapping>  
            <servlet-name>Down</servlet-name>  
            <url-pattern>/scripts/Down</url-pattern>  
      </servlet-mapping>  
       
       <welcome-file-list>  
            <welcome-file>/login.jsp</welcome-file>  
       </welcome-file-list>  
     </web-app>  

标签: isp 代码

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:Python用户定义类实现

下一篇:Go语言使用http下载文件