java实现文件打包下载

java实现文件打包下载

	//批量打包下载
	public void getMrtFileAllDown(Map paramMap, HttpServletRequest request, HttpServletResponse response) {
        try{
            int QYLX = StringUtils.getIntegerByObj(paramMap.get("QYLX"));
            String uuidArrayStr = StringUtils.getStringByObj(paramMap.get("uuidArrayStr"));
            String[] uuidArray = uuidArrayStr.split(",");
            //获取项目根目录
            String path = request.getSession().getServletContext().getRealPath("/");
            String save_xml_dir = "/uploadFiles/" + "license/mrt/" + QYLX + "/";
            String suffix = "";
            //压缩文件保存目录
            String zipSavePath = save_xml_dir + TimeUtils.getYYYY_MM_DD(new Date()) + suffix+".zip";
            File tempXmlFile = new File(path+zipSavePath);
            File fileParent = tempXmlFile.getParentFile();
            if (!fileParent.exists()) {
                fileParent.mkdirs();
            }
            ZipOutputStream out = new ZipOutputStream(new FileOutputStream(path+zipSavePath));
            String xklx = "";
            for (int i = 0; i <uuidArray.length; i++) {
                String UUID = uuidArray[i];
                Map searchMap = new HashMap();
                searchMap.put("UUID", UUID);
                searchMap.put("QYLX", QYLX);
                //生成mrt文件并返回文件存储路径mrtUrl
                Map mrtItem = this.saveCreateMrtFile(request,searchMap);
                String mrtUrl = StringUtils.getStringByObj(((Map)mrtItem.get("data")).get("mrtUrl"));
                if (!xklx.isEmpty()) {
                    xklx += ",";
                }
                xklx += StringUtils.getStringByObj(((Map) mrtItem.get("data")).get("XKLX"));
                if (QYLX == 95) {
                    suffix = "【再注册】";
                } else if (QYLX == 38) {
                    if (("," + xklx + ",").indexOf(",1,") > -1 && ("," + xklx + ",").indexOf(",51,") > -1) {
                        suffix = "【包装备案】【补充申请】";
                    } else if (",1,".indexOf("," + xklx + ",") > -1) {
                        suffix = "【包装备案】";
                    } else if (",51,".indexOf("," + xklx + ",") > -1) {
                        suffix = "【补充申请】";
                    }
                }
                FileInputStream fis = new FileInputStream(path + mrtUrl);
                String fileName = mrtUrl.substring(mrtUrl.lastIndexOf("/")+1);
                //为压缩包内每个文件命名
                out.putNextEntry(new ZipEntry(fileName));
                //写入压缩包
                int len;
                byte[] buffer = new byte[1024];
                while ((len = fis.read(buffer)) > 0) {
                    out.write(buffer, 0, len);
                }
                out.closeEntry();
                fis.close();
            }
            out.close();
            out.flush();

			//将压缩好的文件复制到文件服务器
            if (StringUtils.isNotEmptyObject(PropertiesUtil.FILE_SAVE_PATH)) {
                LicenseUtil.copyFile(path + zipSavePath, PropertiesUtil.FILE_SAVE_PATH + zipSavePath);
            }

            //下载压缩文件
            File zipFile = new File(path + zipSavePath);
            InputStream fis = null;
            OutputStream toClient = null;
            response.reset();
            //转码之后下载的文件不会出现中文乱码
            response.addHeader("Content-Disposition", "attachment;filename=" + new String((TimeUtils.getYYYY_MM_DD(new Date()) + suffix+".zip").getBytes("gbk"), "iso-8859-1"));  //转码之后下载的文件不会出现中文乱码
            response.addHeader("Content-Length", "" + zipFile.length());
            response.setContentType("application/x-msdownload");
            try {
                //以流的形式下载文件
                fis = new BufferedInputStream(new FileInputStream(path + zipSavePath));
                byte[] buffer = new byte[fis.available()];
                fis.read(buffer);
                toClient = new BufferedOutputStream(response.getOutputStream());
                toClient.write(buffer);
                toClient.flush();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                toClient.close();
                fis.close();
                //下载完成后,删除项目中的压缩文件
            	zipFile.delete();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
	//生成mrt文件返回存储路径
    public Map saveCreateMrtFile(HttpServletRequest request,Map paramMap) {
        Map<String,Object> dataMap = new HashMap<>();
        String UUID = StringUtils.getStringByObj(paramMap.get("UUID"));
        int QYLX = StringUtils.getIntegerByObj(paramMap.get("QYLX"));
        try {
            Map searchMap=new HashMap();
            searchMap.put("LICENSECOMPANYBASEUUID",UUID);
            searchMap.put("QYLX",QYLX);
            dataMap=mobileShareRestful.findByBaseInfoUtil(ResultCodeWraper.SUCCESS,searchMap);
            int XKLX = StringUtils.getIntegerByObj(dataMap.get("XKLX"));
            String path = request.getSession().getServletContext().getRealPath("");
            //获取模板
            String mrtModel = "";
            if(QYLX == 117){
                mrtModel = "/upload/license/mrtMuBan/YCXJKYPBA.xml";
            }else if(QYLX == 95){
                mrtModel = "/upload/license/mrtMuBan/GCYPZZCSP.xml";
            }else if(QYLX == 38){
                mrtModel = "/upload/license/mrtMuBan/GCYPZCBCSQ.xml";
            }
            SAXBuilder sb = new SAXBuilder();
            Document doc = sb.build(new File(path + mrtModel));
            Element root = doc.getRootElement();

            String mrtFileName = "";
            if (QYLX == 117) {
                mrtFileName = "_YCXJKYPBA";
                Xml_Mrt.analysisYcxjkypbaXml(root, dataMap);
            }else if(QYLX == 95){
                mrtFileName = StringUtils.getStringByObj(dataMap.get("QYMC"))+"_"+StringUtils.getStringByObj(dataMap.get("FZJG"))+"_"+StringUtils.getStringByObj(dataMap.get("YP_GG"))+"_再注册";
                Xml_Mrt.analysisGcypzzcspXml(root, dataMap,licenseCompanyBaseDao,this);
            } else if (QYLX == 38) {
                String suffix = "_包装备案";
                if (XKLX == 51) {
                    suffix = "_补充申请";
                }
                mrtFileName = StringUtils.getStringByObj(dataMap.get("QYMC")) + "_" + StringUtils.getStringByObj(dataMap.get("FZJG")) + "_" + StringUtils.getStringByObj(dataMap.get("YP_GG")) + suffix;
                Xml_Mrt.analysisGcypzcbcsqXml(root, dataMap, this);
            }

            XMLOutputter xmloutputter = new XMLOutputter();
            xmloutputter.setFormat(Format.getCompactFormat().setEncoding("GBK").setIndent(" "));

            String save_xml_dir = "/uploadFiles/" + "license/mrt/" + QYLX + "/";
            String save_xml_path = save_xml_dir + mrtFileName + ".xml";
            String save_mrt_path = save_xml_dir + mrtFileName + ".mrt";
            File xmlFile = new File(path + save_xml_path);
            File xmlfileParent = xmlFile.getParentFile();
            if (!xmlfileParent.exists()) {
                xmlfileParent.mkdirs();
            }

            OutputStream outputStream = new FileOutputStream(path + save_xml_path);
            xmloutputter.output(doc, outputStream);

            System.err.println("xml文档生成成功!");
            if (StringUtils.isNotEmptyObject(PropertiesUtil.FILE_SAVE_PATH)) {
                LicenseUtil.copyFile(path + save_xml_path, PropertiesUtil.FILE_SAVE_PATH + save_xml_path);
            }

            //修改文件类型
            if (xmlFile.exists()) {
                FileInputStream fis = new FileInputStream(xmlFile);
                //修改后缀名
                String destFileName = xmlFile.getName().replaceAll("\\.xml$", "\\.mrt");
                FileOutputStream fos = new FileOutputStream(new File(xmlFile.getParent().toString().replace(path+save_xml_path, path+save_xml_dir), destFileName));
                //复制写入
                byte[] buf = new byte[1024];
                int len = 0;
                /*读取文件内容并写入文件字节流中*/
                while((len = fis.read(buf))!=-1) {
                    fos.write(buf, 0, len);
                }
                fis.close();
                fos.close();

                System.err.println("mrt文件生成成功!");
                if (StringUtils.isNotEmptyObject(PropertiesUtil.FILE_SAVE_PATH)) {
                    LicenseUtil.copyFile(path + save_mrt_path, PropertiesUtil.FILE_SAVE_PATH + save_mrt_path);
                }
                //修改导出标识
                licenseCompanyBaseDao.execSQL(" update license_company_base set LSZDCOMPANYBASEUUID = '1' where uuid = '" + UUID + "' ");
                //导出成功后,插入审核记录
                Map recordMap = new HashMap<String,Object>();
                recordMap.put("UUID",UUID);
                recordMap.put("huanJie", "mrt文件导出");
                recordMap.put("yiJian", mrtFileName + "mrt文件导出");
                recordMap.put("jieLun", "导出成功");
                recordMap.put("shenHeRen", StringUtils.getStringByObj(((SysUser) request.getSession().getAttribute("user")).getId()));
                this.saveShenHeJiLu(recordMap);
            } else {
                System.out.println("文件不存在!");
            }
            dataMap.put("mrtUrl",save_mrt_path);
            dataMap.put("XKLX",XKLX);
            return LicenseUtil.reMapSucces(dataMap);
        } catch (Exception e) {
            e.printStackTrace();
            return LicenseUtil.reMapError();
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值