• 使用hutool工具导出excel,使用jodd工具压缩流,使用oss上传流


    最近写了一个业务,用到一些比较实用的工具记录下。

    List<RobotCard> robotCardList = null;
    		if (BizRouteEnum.ROUTE_1.getRouteCode().equals(param.getExportModel())){ //1按条件导出,否则按给定数据导出
    			RobotCard robotCardParam = BeanUtil.toBean(map, RobotCard.class);
    			robotCardList = this.searchList(robotCardParam);
    		} else {
    			List<Long> ids = param.getIds();
    			RobotAssert.notNull(ids, "请选择需要导出的数据。");
    			robotCardList = this.selectBatchIds(ids);
    		}
    		
    		RobotAssert.notNull(robotCardList, "无有效数据。");
    		
    		ArrayList<Map<String, Object>> rows = new ArrayList<>();
    		robotCardList.forEach(rc -> {
           		Map<String, Object> col = new LinkedHashMap<>();
           		col.put("编号", rc.getId().toString());
           		col.put("金额", rc.getCardAmount());
           		col.put("密码", rc.getPayPasswd());
           		rows.add(col);
           	});
    		
    		ExcelWriter excelWriter = ExcelUtil.getWriter();
           	excelWriter.setColumnWidth(0, 30);
           	excelWriter.write(rows);
           	
         	ByteArrayOutputStream baos = new ByteArrayOutputStream();
         	excelWriter.flush(baos);
         	excelWriter.close();
         	
         	ByteArrayOutputStream bos = new ByteArrayOutputStream();
    		ZipOutputStream zos = new ZipOutputStream(bos, Charset.defaultCharset());
    		
    		//添加xls
    		try {
    			jodd.io.ZipUtil.addToZip(zos, baos.toByteArray(), "制卡数据.xls", "制卡数据");
    		} catch (Exception e1) {
    			LOGGER.error("文件压缩失败。", e1);
    		}
    		
    		robotCardList.forEach(card -> {
    			//生产二维码
    			QrConfig config = new QrConfig(300, 300);
    			config.setCharset(Charset.forName("utf-8"));
    			String cardId = card.getId().toString();
    			
    			byte[] data = QrCodeUtil.generatePng(cardId, config);
    			
       			//压缩
       			try {
    				jodd.io.ZipUtil.addToZip(zos, data, cardId.concat(CommonConstant.PNG), cardId);
    			} catch (Exception e) {
    				LOGGER.error("文件压缩失败。", e);
    			}
    		});
    		
       		try {
    			zos.finish();
    			zos.close();
    		} catch (Exception e) {
    			LOGGER.error("压缩流关闭失败。", e);
    		}
       		
       		//上传
       		ByteArrayInputStream in = new ByteArrayInputStream(bos.toByteArray());
       		String url = oSSClientUtil.putObjectForInputStream(
       				OssConstant.PAYMENT_TEMP.concat(DateUtil.format(DateUtil.date(), 
       						DatePattern.PURE_DATETIME_PATTERN)).concat(CommonConstant.PNG), in);
    

      

  • 相关阅读:
    Row versus Set Processing, Surprise!(集合处理和单行处理数据的差异性)
    SVN导出增量包的方法
    Number of Digit One
    VPS+LNMP+WordPress搭建个人网站
    寻找最小的k个数(大顶堆方法)
    二叉搜索树转化为有序双向链表(C语言)
    LeetCode 345 Reverse Vowels of a String
    LeetCode 278 First Bad Version
    LeetCode 205 Isomorphic Strings
    LeetCode 88 Merge Sorted Array
  • 原文地址:https://www.cnblogs.com/yun965861480/p/11249415.html
Copyright © 2020-2023  润新知