Java批量下载打包成zip文件

Java批量下载打包成zip文件

话不多说,直接上代码

 
    /**
     * 批量下载
     * @param filepathList (格式xx,xx2,xx3) 文件路径
     * @param request
     * @param response
     */
    @RequestMapping(value = "downloadPlanFile")
    public void downloadPlanFile(@RequestParam(value = "filePathList",required = false) List<String> filepathList,
                                 HttpServletRequest request,HttpServletResponse response){
        omsEducationDataService.downloadPlanFile(filepathList,request,response);
    }
        
    
    
    
    /**
     * 批量下载
     * @param filepathList (格式xx,xx2,xx3) 文件路径
     * @param request
     * @param response
     */
    HttpServletResponse downloadPlanFile(List<String> filepathList, HttpServletRequest request,HttpServletResponseresponse);

    
    


/**
     * 批量下载
     * @param filepathList (格式xx,xx2,xx3) 文件路径
     * @param request
     * @param response
     */
    public HttpServletResponse downloadPlanFile(List<String> filepathList, HttpServletRequest request, HttpServletResponse response) {
        HttpServletResponse response1 = fileUploadTool.downLoadBatch(filepathList,request,response);
        return response1;
    }

    
        
        
        
        /**
     * <b>功能描述: 批量下载</b>
     * @Param: [filepathList, request, response]
     * @Return: javax.servlet.http.HttpServletResponse
     * @Author: luoshuai
     * @Date: 2020/9/15 20:10
     */
    public HttpServletResponse downLoadBatch(List<String> filepathList, HttpServletRequest request, HttpServletResponse response) {
        if(filepathList == null || filepathList.size() < 1){
            throw new CustomMessageException("未选择要下载的文件资料");
        }

        //响应头的设置
        response.reset();
        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");
        //设置压缩包的名字
        String dates = String.valueOf(new Date().getTime());
        String billname = "行前教育资料包-" + dates;
        String downloadName = billname + ".zip";
        //返回客户端浏览器的版本号、类型
        String agent = request.getHeader("USER-AGENT");
        try {
            //针对IE或者以IE为内核的浏览器:
            if (agent.contains("MSIE") || agent.contains("Trident")) {
                downloadName = java.net.URLEncoder.encode(downloadName, "UTF-8");
            } else {
                //非IE浏览器的处理:
                downloadName = new String(downloadName.getBytes("UTF-8"), "ISO-8859-1");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        response.setHeader("Content-Disposition", "attachment;fileName=\"" + downloadName + "\"");

        //设置压缩流:直接写入response,实现边压缩边下载
        ZipOutputStream zipos = null;
        try {
            zipos = new ZipOutputStream(new BufferedOutputStream(response.getOutputStream()));
            zipos.setMethod(ZipOutputStream.DEFLATED); //设置压缩方法
        } catch (Exception e) {
            e.printStackTrace();
        }

        //循环将文件写入压缩流
        DataOutputStream os = null;

        for (String filePath : filepathList) {
            //文件路径
            File file = new File(filePath);
            if (!file.exists()) {
                throw new CustomMessageException("文件已不存在");
            } else {
                try {
                    //添加ZipEntry,并ZipEntry中写入文件流
                    String fileName = file.getName();
                    zipos.putNextEntry(new ZipEntry(fileName));
                    os = new DataOutputStream(zipos);
                    InputStream is = new FileInputStream(file);
                    byte[] b = new byte[100];
                    int length = 0;
                    while ((length = is.read(b)) != -1) {
                        os.write(b, 0, length);
                    }
                    is.close();
                    zipos.closeEntry();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
        //关闭流
        try {
            os.flush();
            os.close();
            zipos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return response;
    }
    
    
    
    
    

猜你喜欢

转载自blog.csdn.net/weixin_37999518/article/details/114059909