LocalSysFileServiceImpl.java 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  1. package com.xunmei.file.service;
  2. import cn.hutool.core.collection.CollectionUtil;
  3. import cn.hutool.core.collection.ListUtil;
  4. import cn.hutool.core.date.DateUtil;
  5. import cn.hutool.core.io.FileUtil;
  6. import cn.hutool.core.util.ObjectUtil;
  7. import com.alibaba.fastjson2.JSON;
  8. import com.baomidou.mybatisplus.core.toolkit.IdWorker;
  9. import com.lowagie.text.*;
  10. import com.lowagie.text.pdf.*;
  11. import com.xunmei.common.core.constant.CacheConstants;
  12. import com.xunmei.common.core.domain.IdName;
  13. import com.xunmei.common.core.domain.registerbook.dto.CoreRegisterBookPdfExportDto;
  14. import com.xunmei.common.core.domain.registerbook.dto.ExportPdfDto;
  15. import com.xunmei.common.core.domain.registerbook.vo.CoreRegisterBookPdfPageVo;
  16. import com.xunmei.common.core.domain.registerbook.vo.PdfLocalFileTempVo;
  17. import com.xunmei.common.core.domain.registerbook.vo.PdfToZipTempVo;
  18. import com.xunmei.common.core.enums.RegisterBookType;
  19. import com.xunmei.common.core.utils.DateHelper;
  20. import com.xunmei.common.core.utils.uuid.UUID;
  21. import com.xunmei.common.redis.utils.RedisUtils;
  22. import com.xunmei.file.utils.FileDownUtils;
  23. import com.xunmei.file.utils.FileUploadUtils;
  24. import com.xunmei.file.utils.PdfPageHelperEvent;
  25. import com.xunmei.file.utils.PdfUtil;
  26. import com.xunmei.file.vo.FileBase64Vo;
  27. import com.xunmei.file.vo.ItextPdfTableVo;
  28. import com.xunmei.file.vo.PdfFilePathVo;
  29. import com.xunmei.system.api.domain.AccessPdf;
  30. import com.xunmei.system.api.domain.SafeCheckTaskRegisterBookVo;
  31. import com.xunmei.system.api.vo.SysOrgVO;
  32. import io.netty.util.internal.StringUtil;
  33. import org.apache.commons.io.FileUtils;
  34. import org.apache.commons.lang3.StringUtils;
  35. import org.apache.commons.text.StringEscapeUtils;
  36. import org.apache.poi.ss.usermodel.Footer;
  37. import org.apache.tools.zip.ZipEntry;
  38. import org.apache.tools.zip.ZipOutputStream;
  39. import org.slf4j.Logger;
  40. import org.slf4j.LoggerFactory;
  41. import org.springframework.beans.factory.annotation.Autowired;
  42. import org.springframework.beans.factory.annotation.Value;
  43. import org.springframework.context.annotation.Primary;
  44. import org.springframework.data.redis.core.BoundValueOperations;
  45. import org.springframework.data.redis.core.StringRedisTemplate;
  46. import org.springframework.stereotype.Service;
  47. import org.springframework.web.multipart.MultipartFile;
  48. import javax.servlet.ServletOutputStream;
  49. import javax.servlet.http.HttpServletRequest;
  50. import javax.servlet.http.HttpServletResponse;
  51. import java.io.*;
  52. import java.math.BigDecimal;
  53. import java.net.URLDecoder;
  54. import java.net.URLEncoder;
  55. import java.nio.charset.StandardCharsets;
  56. import java.nio.file.Files;
  57. import java.nio.file.Paths;
  58. import java.util.List;
  59. import java.util.*;
  60. import java.util.concurrent.CountDownLatch;
  61. import java.util.regex.Matcher;
  62. import java.util.regex.Pattern;
  63. import java.util.stream.Collectors;
  64. import static java.util.regex.Pattern.compile;
  65. /**
  66. * 本地文件存储
  67. *
  68. * @author xunmei
  69. */
  70. @Primary
  71. @Service
  72. public class LocalSysFileServiceImpl implements ISysFileService {
  73. private static final Logger log = LoggerFactory.getLogger(LocalSysFileServiceImpl.class);
  74. public static final String TEMP_DIR_NAME = "registerBookPdfBatchExportTempDir";
  75. @Value("${file.path}")
  76. private String localFilePath;
  77. @Value("${file.prefix}")
  78. public String prefix;
  79. @Autowired
  80. private HttpServletRequest request;
  81. @Autowired
  82. private StringRedisTemplate redisTemplate;
  83. private PdfFilePathVo getLocalFilePath(String businessType, String fileName) {
  84. // 验证输入
  85. if (isValidFileName(fileName)) {
  86. throw new IllegalArgumentException("Invalid file name");
  87. }
  88. final String path = File.separator + businessType +
  89. File.separator +
  90. DateUtil.format(new Date(), "yyyy" + File.separator + "MM" + File.separator + "dd" +
  91. File.separator);
  92. String filePath = localFilePath + path;
  93. // 规范化路径
  94. String absolutePath = Paths.get(filePath).toAbsolutePath().normalize().toString();
  95. final File file = FileUtils.getFile(absolutePath);
  96. if (!file.exists()) {
  97. file.mkdirs();
  98. }
  99. PdfFilePathVo pathVo = new PdfFilePathVo();
  100. pathVo.setAbsolutePath(localFilePath + path + fileName);
  101. pathVo.setTempFileName(localFilePath + path + IdWorker.getId() + ".pdf");
  102. pathVo.setRelativePath(path + fileName);
  103. return pathVo;
  104. }
  105. private boolean isValidFileName(String fileName) {
  106. /*
  107. a-zA-Z:匹配所有大写和小写字母;
  108. 0-9:匹配所有数字;
  109. ._\-:匹配点(.)、下划线(_)和短横线(-)。
  110. +:表示前面的字符集出现一次或多次。
  111. */
  112. // 使用正则表达式检查文件名是否合法
  113. //return fileName.matches("[a-zA-Z0-9._\\-]+");
  114. return fileName.matches("[a-zA-Z0-9]+");
  115. }
  116. /**
  117. * 修复路径操纵bug
  118. *
  119. * @param param
  120. * @return
  121. */
  122. private static String filterPath(String param) {
  123. Pattern pattern = compile("[/\\:*?<>|]");
  124. Matcher matcher = pattern.matcher(param);
  125. param = matcher.replaceAll("");
  126. return param;
  127. }
  128. /**
  129. * 本地文件上传接口
  130. *
  131. * @param file 上传的文件
  132. * @return 访问地址
  133. * @throws Exception
  134. */
  135. @Override
  136. public String uploadFile(MultipartFile file) throws Exception {
  137. String name = FileUploadUtils.upload(localFilePath, file);
  138. return name;
  139. }
  140. @Override
  141. public String uploadFile(MultipartFile file, String busType) throws Exception {
  142. String name = FileUploadUtils.upload(localFilePath, file, busType);
  143. return name;
  144. }
  145. @Override
  146. public void downloadFile(HttpServletResponse response, String filePath) {
  147. ByteArrayOutputStream out = null;
  148. try {
  149. filePath = localFilePath + filePath;
  150. filePath = URLDecoder.decode(filePath, "UTF-8");
  151. out = FileDownUtils.downloadFile(filePath);
  152. String fileSuffix = FileDownUtils.getFileSuffix(filePath);
  153. String formFileName = UUID.randomUUID().toString() + fileSuffix;
  154. String userAgent = request.getHeader("User-Agent");
  155. // 针对IE或者以IE为内核的浏览器:
  156. if (StringUtils.isNotEmpty(userAgent) && (userAgent.contains("MSIE") || userAgent.contains("Trident"))) {
  157. formFileName = java.net.URLEncoder.encode(formFileName, "UTF-8");
  158. } else {
  159. // 非IE浏览器的处理:
  160. formFileName = new String(formFileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
  161. }
  162. response.reset();
  163. response.setCharacterEncoding("UTF-8");
  164. response.addHeader("Content-Disposition", "attachment;filename=" + formFileName);
  165. response.addHeader("Content-Length", "" + out.size());
  166. out.writeTo(response.getOutputStream());
  167. response.setContentType("application/octet-stream");
  168. out.flush();
  169. out.close();
  170. } catch (IOException e) {
  171. log.error("文件下载失败! ");
  172. e.printStackTrace();
  173. } finally {
  174. try {
  175. if (out != null) {
  176. out.close();
  177. }
  178. } catch (IOException e) {
  179. e.printStackTrace();
  180. }
  181. }
  182. }
  183. @Override
  184. public String getRelativePath(String path) {
  185. if (ObjectUtil.isEmpty(path)) {
  186. return null;
  187. }
  188. if (path.startsWith(this.localFilePath)) {
  189. return path.substring(this.localFilePath.length());
  190. }
  191. return path;
  192. }
  193. private void dealHeader(Map<String, Object> dataMap, Document document, BaseFont abf, BaseFont fs) throws
  194. DocumentException {
  195. //处理标题
  196. Paragraph p = new Paragraph("日常履职登记簿", new Font(fs, 20, Font.NORMAL));
  197. p.setAlignment(Paragraph.ALIGN_CENTER);
  198. //段落下空白
  199. p.setSpacingAfter(10f);
  200. document.add(p);
  201. //日期
  202. String blankStr1 = " ";
  203. String blankStr2 = " ";
  204. Paragraph p2 = new Paragraph(dataMap.get("orgName") + blankStr1 + dataMap.get("dateStr") + blankStr2, new Font(abf, 10, Font.NORMAL));
  205. p2.setAlignment(Paragraph.ALIGN_RIGHT);
  206. document.add(p2);
  207. }
  208. @Override
  209. public String generateEduTrainingPdf(Map<String, Object> data) throws Exception {
  210. String fileName = filterPath(data.get("fileName").toString());
  211. PdfFilePathVo pathVo = getLocalFilePath("edu", fileName);
  212. String afterStr = StringEscapeUtils.escapeEcmaScript(pathVo.getTempFileName());
  213. log.info("开始生成教育培训登记簿,当前绝对地址为:{}", afterStr);
  214. final ItextPdfTableVo pdfTableVo = PdfUtil.createTable(pathVo.getTempFileName(), 6, 10);
  215. final Document document = pdfTableVo.getDocument();
  216. final PdfWriter writer = pdfTableVo.getWriter();
  217. final PdfPTable table = pdfTableVo.getTable();
  218. final BaseFont fs = pdfTableVo.getFs();
  219. final Font tableFont = pdfTableVo.getTableFont();
  220. document.open();
  221. PdfUtil.dealHeader(document, fs, "学 习 教 育 记 录", 24);
  222. PdfUtil.dealEduBody(document, table, tableFont, data);
  223. document.close();
  224. writer.close();
  225. PdfUtil.addPageNum(pathVo.getTempFileName(), pathVo.getAbsolutePath(), fs, tableFont);
  226. log.info("教育培训登记簿生成结束,当前绝对地址为:{}", afterStr);
  227. //此处返回 /statics/edu/xxx.pdf
  228. return this.prefix + pathVo.getRelativePath();
  229. }
  230. @Override
  231. public String generateResumptionPdf(Map<String, Object> data) throws Exception {
  232. PdfFilePathVo pathVo = getLocalFilePath("resumption", data.get("fileName").toString());
  233. String afterStr = StringEscapeUtils.escapeEcmaScript(pathVo.getAbsolutePath());
  234. log.info("开始生成履职登记簿,当前绝对地址为:{}", afterStr);
  235. Document document = new Document();
  236. PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pathVo.getAbsolutePath()));
  237. document.open();
  238. // 使用语言包字体
  239. BaseFont abf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
  240. // 外部字体
  241. BaseFont fs = BaseFont.createFont("/fonts/msyh.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
  242. Font tableFont = new Font(fs, 12, Font.NORMAL);
  243. PdfPTable table = new PdfPTable(8);
  244. table.setSpacingBefore(16f);
  245. this.dealHeader(data, document, abf, fs);
  246. PdfUtil.dealResumptionBody(document, table, tableFont, data);
  247. document.close();
  248. writer.close();
  249. log.info("履职登记簿生成结束,当前绝对地址为:{}", afterStr);
  250. //此处返回 /statics/edu/xxx.pdf
  251. return this.prefix + pathVo.getRelativePath();
  252. }
  253. @Override
  254. public String generateSafeCheckPdf(SafeCheckTaskRegisterBookVo data) throws Exception {
  255. PdfFilePathVo pathVo = getLocalFilePath("safeCheck", data.getDest());
  256. String afterStr = StringEscapeUtils.escapeEcmaScript(pathVo.getTempFileName());
  257. log.info("开始生成安全检查登记簿,当前绝对地址为:{}", afterStr);
  258. final ItextPdfTableVo pdfTableVo = PdfUtil.createTable(pathVo.getTempFileName(), 14, 10);
  259. final Document document = pdfTableVo.getDocument();
  260. final PdfWriter writer = pdfTableVo.getWriter();
  261. final PdfPTable table = pdfTableVo.getTable();
  262. final BaseFont fs = pdfTableVo.getFs();
  263. final Font tableFont = pdfTableVo.getTableFont();
  264. PdfUtil.dealHeader(document, fs, "安全保卫检查情况登记簿", 24);
  265. PdfUtil.dealSafeCheckPBody(document, table, tableFont, data);
  266. document.close();
  267. writer.close();
  268. PdfUtil.addPageNum(pathVo.getTempFileName(), pathVo.getAbsolutePath(), fs, tableFont);
  269. log.info("安全检查登记簿生成结束,当前绝对地址为:{}", afterStr);
  270. //此处返回 /statics/edu/xxx.pdf
  271. return this.prefix + pathVo.getRelativePath();
  272. }
  273. @Override
  274. public String generateAccessPdf(AccessPdf data) throws Exception {
  275. PdfFilePathVo pathVo = getLocalFilePath("access", data.getDest());
  276. String afterStr = StringEscapeUtils.escapeEcmaScript(pathVo.getAbsolutePath());
  277. log.info("开始生成监控调阅登记簿,当前绝对地址为:{}", afterStr);
  278. final ItextPdfTableVo pdfTableVo = PdfUtil.createTable(pathVo.getAbsolutePath(), 46, 10);
  279. final Document document = pdfTableVo.getDocument();
  280. final PdfWriter writer = pdfTableVo.getWriter();
  281. final PdfPTable table = pdfTableVo.getTable();
  282. final BaseFont fs = pdfTableVo.getFs();
  283. final Font tableFont = pdfTableVo.getTableFont();
  284. Font font = new Font(fs, 9, Font.NORMAL);
  285. PdfUtil.dealHeader(document, fs, "监控点阅情况登记簿", 14);
  286. String checkTime = data.getTaskStartTime();
  287. String checkUser = data.getCheckUser();
  288. PdfPCell orgCell = new PdfPCell(new Phrase("调阅单位", font));
  289. orgCell.setColspan(8);
  290. orgCell.setRowspan(1);
  291. orgCell.setBorder(Rectangle.NO_BORDER);
  292. orgCell.setHorizontalAlignment(Element.ALIGN_CENTER);
  293. PdfPCell orgNameCell = new PdfPCell(new Phrase(data.getOrgName(), font));
  294. orgNameCell.setColspan(15);
  295. orgNameCell.setRowspan(1);
  296. orgNameCell.setBorder(Rectangle.NO_BORDER);
  297. orgNameCell.setHorizontalAlignment(Element.ALIGN_CENTER);
  298. PdfPCell userCell = new PdfPCell(new Phrase("调阅人", font));
  299. userCell.setColspan(8);
  300. userCell.setRowspan(1);
  301. userCell.setBorder(Rectangle.NO_BORDER);
  302. userCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
  303. PdfPCell userNameCell = new PdfPCell(new Phrase(checkUser, font));
  304. userNameCell.setColspan(15);
  305. userNameCell.setRowspan(1);
  306. userNameCell.setBorder(Rectangle.NO_BORDER);
  307. userNameCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
  308. PdfPCell timeNameCell = new PdfPCell(new Phrase("调阅时间", font));
  309. timeNameCell.setColspan(9);
  310. timeNameCell.setRowspan(1);
  311. timeNameCell.setBorder(Rectangle.NO_BORDER);
  312. timeNameCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
  313. PdfPCell timeCell = new PdfPCell(new Phrase(checkTime, font));
  314. timeCell.setColspan(37);
  315. timeCell.setRowspan(1);
  316. timeCell.setBorder(Rectangle.NO_BORDER);
  317. timeCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
  318. /* PdfPCell findCell = new PdfPCell(new Phrase("发现问题情况", font));
  319. findCell.setColspan(46);
  320. findCell.setRowspan(1);
  321. findCell.setBorder(Rectangle.NO_BORDER);
  322. findCell.setHorizontalAlignment(Element.ALIGN_RIGHT);*/
  323. /* table.addCell(orgCell);
  324. table.addCell(orgNameCell);
  325. table.addCell(userCell);
  326. table.addCell(userNameCell);
  327. table.addCell(timeNameCell);
  328. table.addCell(timeCell);*/
  329. // table.addCell(findCell);
  330. Font titleFont = new Font(fs, 8, Font.NORMAL);
  331. PdfUtil.createPDFCell(titleFont, table, "调阅单位", Element.ALIGN_MIDDLE, 8, 0);
  332. PdfUtil.createPDFCell(titleFont, table, data.getOrgName(), Element.ALIGN_MIDDLE, 15, 0);
  333. PdfUtil.createPDFCell(titleFont, table, "调阅人", Element.ALIGN_MIDDLE, 8, 0);
  334. PdfUtil.createPDFCell(titleFont, table, checkUser, Element.ALIGN_MIDDLE, 15, 0);
  335. PdfUtil.createPDFCell(titleFont, table, "调阅时间", Element.ALIGN_MIDDLE, 9, 0);
  336. PdfUtil.createPDFCell(titleFont, table, checkTime, Element.ALIGN_MIDDLE, 37, 0);
  337. PdfUtil.createPDFCell(titleFont, table, "发现问题情况", Element.ALIGN_MIDDLE, 46, 0);
  338. PdfUtil.dealAccessPBody(document, table, tableFont, titleFont, data.getDataVos());
  339. document.close();
  340. writer.close();
  341. log.info("监控调阅登记簿生成结束,当前绝对地址为:{}", afterStr);
  342. //此处返回 /statics/edu/xxx.pdf
  343. return this.prefix + pathVo.getRelativePath();
  344. }
  345. @Override
  346. public String generateDrillPdf(Map<String, Object> data) throws Exception {
  347. PdfFilePathVo pathVo = getLocalFilePath("drill", data.get("fileName").toString());
  348. String afterStr = StringEscapeUtils.escapeEcmaScript(pathVo.getTempFileName());
  349. log.info("开始生成预案演练登记簿,当前绝对地址为:{}", afterStr);
  350. final ItextPdfTableVo pdfTableVo = PdfUtil.createTable(pathVo.getTempFileName(), 6, 10);
  351. final Document document = pdfTableVo.getDocument();
  352. final PdfWriter writer = pdfTableVo.getWriter();
  353. final PdfPTable table = pdfTableVo.getTable();
  354. final BaseFont fs = pdfTableVo.getFs();
  355. final Font tableFont = pdfTableVo.getTableFont();
  356. document.open();
  357. PdfUtil.dealHeader(document, fs, "预 案 演 练 记 录", 24);
  358. PdfUtil.dealDrillBody(document, table, tableFont, data);
  359. document.close();
  360. writer.close();
  361. log.info("预案演练登记簿生成结束,当前绝对地址为:{}", afterStr);
  362. PdfUtil.addPageNum(pathVo.getTempFileName(), pathVo.getAbsolutePath(), fs, tableFont);
  363. //此处返回 /statics/edu/xxx.pdf
  364. return this.prefix + pathVo.getRelativePath();
  365. }
  366. @Override
  367. public String generateOutInPdf(Map<String, Object> data) throws Exception {
  368. PdfFilePathVo pathVo = getLocalFilePath("visit", data.get("fileName").toString());
  369. String afterStr = StringEscapeUtils.escapeEcmaScript(pathVo.getAbsolutePath());
  370. log.info("开始生成来访管理登记簿,当前绝对地址为:{}", afterStr);
  371. final ItextPdfTableVo pdfTableVo = PdfUtil.createTable(pathVo.getAbsolutePath(), 6, 10);
  372. final Document document = pdfTableVo.getDocument();
  373. final PdfWriter writer = pdfTableVo.getWriter();
  374. final PdfPTable table = pdfTableVo.getTable();
  375. final BaseFont fs = pdfTableVo.getFs();
  376. final Font tableFont = pdfTableVo.getTableFont();
  377. PdfUtil.dealHeader(document, fs, "来 访 管 理 登 记 簿", 24);
  378. PdfUtil.dealOutInBody(document, table, tableFont, data);
  379. document.close();
  380. writer.close();
  381. log.info("来访管理登记簿生成结束,当前绝对地址为:{}", afterStr);
  382. // PdfUtil.addPageNum(pathVo.getTempFileName(), pathVo.getAbsolutePath(), fs, tableFont);
  383. //此处返回 /statics/edu/xxx.pdf
  384. return this.prefix + pathVo.getRelativePath();
  385. }
  386. @Override
  387. public String uploadFileBase64(FileBase64Vo file) throws Exception {
  388. String filePath = FileUploadUtils.uploadBase64(localFilePath, file);
  389. return filePath;
  390. }
  391. /**
  392. * 履职转pdf
  393. *
  394. * @param dest
  395. * @throws IOException
  396. * @throws DocumentException
  397. */
  398. public void dataToPdf(String dest) throws IOException, DocumentException {
  399. Document document = new Document();
  400. PdfWriter.getInstance(document, new FileOutputStream(dest));
  401. document.open();
  402. // 使用语言包字体
  403. BaseFont abf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
  404. //字体
  405. Font font = new Font(abf, 8);
  406. //段落
  407. Paragraph p = new Paragraph("测试结算单", new Font(abf, 12, Font.BOLD));
  408. p.setAlignment(Paragraph.ALIGN_CENTER);
  409. document.add(p);
  410. //表格
  411. PdfPTable table = new PdfPTable(8);//numcolumns:列数
  412. table.setSpacingBefore(16f);//表格与上面段落的空隙
  413. //表格列创建并赋值
  414. PdfPCell cell = new PdfPCell(new Phrase("单位名称:测试有限公司", font));
  415. cell.setHorizontalAlignment(Element.ALIGN_LEFT);//居中
  416. cell.disableBorderSide(13);//去除左右上边框,保留下边框
  417. cell.setColspan(4);//合并列数
  418. table.addCell(cell);
  419. cell = new PdfPCell(new Phrase("日期:2020-06-05", font));
  420. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  421. cell.disableBorderSide(13);
  422. cell.setColspan(3);
  423. table.addCell(cell);
  424. cell = new PdfPCell(new Phrase("单位(元)", font));
  425. cell.setHorizontalAlignment(Element.ALIGN_LEFT);
  426. cell.disableBorderSide(13);
  427. cell.setColspan(1);
  428. table.addCell(cell);
  429. //首行
  430. cell = new PdfPCell(new Phrase("期间", font));
  431. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  432. cell.setColspan(2);
  433. table.addCell(cell);
  434. cell = new PdfPCell(new Phrase("月份", font));
  435. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  436. table.addCell(cell);
  437. cell = new PdfPCell(new Phrase("分类", font));
  438. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  439. table.addCell(cell);
  440. cell = new PdfPCell(new Phrase("年利率", font));
  441. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  442. table.addCell(cell);
  443. cell = new PdfPCell(new Phrase("日利率", font));
  444. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  445. table.addCell(cell);
  446. cell = new PdfPCell(new Phrase("基数", font));
  447. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  448. table.addCell(cell);
  449. cell = new PdfPCell(new Phrase("利息", font));
  450. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  451. table.addCell(cell);
  452. cell = new PdfPCell(new Phrase("起始日:2020-03-26\n" +
  453. "结束日:2020-04-25", font));
  454. cell.setPadding(16f);
  455. cell.setVerticalAlignment(Element.ALIGN_CENTER);
  456. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  457. cell.setRowspan(3);
  458. cell.setColspan(2);
  459. table.addCell(cell);
  460. cell = new PdfPCell(new Phrase("4", font));
  461. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  462. table.addCell(cell);
  463. cell = new PdfPCell(new Phrase("资金", font));
  464. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  465. table.addCell(cell);
  466. cell = new PdfPCell(new Phrase("1.10%", font));
  467. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  468. table.addCell(cell);
  469. cell = new PdfPCell(new Phrase("0.000031", font));
  470. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  471. table.addCell(cell);
  472. cell = new PdfPCell(new Phrase("10598164.91", font));
  473. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  474. table.addCell(cell);
  475. cell = new PdfPCell(new Phrase("325.01", font));
  476. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  477. table.addCell(cell);
  478. cell = new PdfPCell(new Phrase("4", font));
  479. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  480. table.addCell(cell);
  481. cell = new PdfPCell(new Phrase("资金", font));
  482. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  483. table.addCell(cell);
  484. cell = new PdfPCell(new Phrase("1.10%", font));
  485. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  486. table.addCell(cell);
  487. cell = new PdfPCell(new Phrase("0.000031", font));
  488. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  489. table.addCell(cell);
  490. cell = new PdfPCell(new Phrase("-", font));
  491. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  492. table.addCell(cell);
  493. cell = new PdfPCell(new Phrase("-", font));
  494. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  495. table.addCell(cell);
  496. cell = new PdfPCell(new Phrase("4", font));
  497. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  498. table.addCell(cell);
  499. cell = new PdfPCell(new Phrase("资金", font));
  500. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  501. table.addCell(cell);
  502. cell = new PdfPCell(new Phrase("1.10%", font));
  503. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  504. table.addCell(cell);
  505. cell = new PdfPCell(new Phrase("0.000031", font));
  506. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  507. table.addCell(cell);
  508. cell = new PdfPCell(new Phrase("-", font));
  509. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  510. table.addCell(cell);
  511. cell = new PdfPCell(new Phrase("-", font));
  512. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  513. table.addCell(cell);
  514. cell = new PdfPCell(new Phrase("合计", font));
  515. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  516. cell.setColspan(7);
  517. table.addCell(cell);
  518. cell = new PdfPCell(new Phrase("325.01", font));
  519. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  520. table.addCell(cell);
  521. cell = new PdfPCell(new Phrase("会计制单:", font));
  522. cell.setHorizontalAlignment(Element.ALIGN_LEFT);
  523. cell.disableBorderSide(14);
  524. cell.setColspan(4);
  525. table.addCell(cell);
  526. cell = new PdfPCell(new Phrase("复核:", font));
  527. cell.setHorizontalAlignment(Element.ALIGN_LEFT);
  528. cell.disableBorderSide(14);
  529. cell.setColspan(4);
  530. table.addCell(cell);
  531. table.setSpacingBefore(16f);
  532. document.add(table);
  533. //下一页
  534. document.newPage();
  535. //段落
  536. Paragraph p1 = new Paragraph("下一页测试结算单", new Font(abf, 12, Font.BOLD));
  537. p1.setAlignment(Paragraph.ALIGN_CENTER);
  538. document.add(p1);
  539. //表格
  540. table = new PdfPTable(8);//numcolumns:列数
  541. table.setSpacingBefore(16f);//表格与上面段落的空隙
  542. //表格列创建并赋值
  543. cell = new PdfPCell(new Phrase("单位名称:测试有限公司", font));
  544. cell.setHorizontalAlignment(Element.ALIGN_LEFT);//居中
  545. cell.disableBorderSide(13);//去除左右上边框,保留下边框
  546. cell.setColspan(4);//合并列数
  547. table.addCell(cell);
  548. cell = new PdfPCell(new Phrase("日期:2020-06-05", font));
  549. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  550. cell.disableBorderSide(13);
  551. cell.setColspan(3);
  552. table.addCell(cell);
  553. cell = new PdfPCell(new Phrase("单位(元)", font));
  554. cell.setHorizontalAlignment(Element.ALIGN_LEFT);
  555. cell.disableBorderSide(13);
  556. cell.setColspan(1);
  557. table.addCell(cell);
  558. //首行
  559. cell = new PdfPCell(new Phrase("期间", font));
  560. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  561. cell.setColspan(2);
  562. table.addCell(cell);
  563. cell = new PdfPCell(new Phrase("月份", font));
  564. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  565. table.addCell(cell);
  566. cell = new PdfPCell(new Phrase("分类", font));
  567. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  568. table.addCell(cell);
  569. cell = new PdfPCell(new Phrase("年利率", font));
  570. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  571. table.addCell(cell);
  572. cell = new PdfPCell(new Phrase("日利率", font));
  573. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  574. table.addCell(cell);
  575. cell = new PdfPCell(new Phrase("基数", font));
  576. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  577. table.addCell(cell);
  578. cell = new PdfPCell(new Phrase("利息", font));
  579. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  580. table.addCell(cell);
  581. cell = new PdfPCell(new Phrase("起始日:2020-04-26\n" +
  582. "结束日:2020-05-25", font));
  583. cell.setPadding(16f);
  584. cell.setVerticalAlignment(Element.ALIGN_CENTER);
  585. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  586. cell.setColspan(2);
  587. table.addCell(cell);
  588. cell = new PdfPCell(new Phrase("4", font));
  589. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  590. table.addCell(cell);
  591. cell = new PdfPCell(new Phrase("资金", font));
  592. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  593. table.addCell(cell);
  594. cell = new PdfPCell(new Phrase("1.10%", font));
  595. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  596. table.addCell(cell);
  597. cell = new PdfPCell(new Phrase("0.000031", font));
  598. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  599. table.addCell(cell);
  600. cell = new PdfPCell(new Phrase("10598164.91", font));
  601. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  602. table.addCell(cell);
  603. cell = new PdfPCell(new Phrase("325.01", font));
  604. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  605. table.addCell(cell);
  606. cell = new PdfPCell(new Phrase("合计", font));
  607. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  608. cell.setColspan(7);
  609. table.addCell(cell);
  610. cell = new PdfPCell(new Phrase("325.01", font));
  611. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  612. table.addCell(cell);
  613. cell = new PdfPCell(new Phrase("会计制单:", font));
  614. cell.setHorizontalAlignment(Element.ALIGN_LEFT);
  615. cell.disableBorderSide(14);
  616. cell.setColspan(4);
  617. table.addCell(cell);
  618. cell = new PdfPCell(new Phrase("复核:", font));
  619. cell.setHorizontalAlignment(Element.ALIGN_LEFT);
  620. cell.disableBorderSide(14);
  621. cell.setColspan(4);
  622. table.addCell(cell);
  623. table.setSpacingBefore(16f);
  624. document.add(table);
  625. document.close();
  626. }
  627. @Override
  628. public String absolutePath(String path) {
  629. if (ObjectUtil.isEmpty(path)) {
  630. return StringUtil.EMPTY_STRING;
  631. }
  632. if (path.startsWith(this.localFilePath)) {
  633. return path;
  634. }
  635. if (path.startsWith(this.prefix)) {
  636. return this.localFilePath + path.substring(this.prefix.length());
  637. }
  638. return StringUtil.EMPTY_STRING;
  639. }
  640. @Override
  641. public InputStream getFileStream(String path) throws IOException {
  642. String absolutePath = this.absolutePath(path);
  643. File file = FileUtils.getFile(absolutePath);
  644. return Files.newInputStream(file.toPath());
  645. }
  646. @Override
  647. public void registerBookCompressPdf(ExportPdfDto pdfDto, HttpServletResponse response) throws IOException {
  648. final List<CoreRegisterBookPdfPageVo> registerBookPdfList = pdfDto.getRegisterBookPdfList();
  649. try {
  650. String zipName = URLEncoder.encode(pdfDto.getFileName() + DateHelper.getDateString(new Date()) + ".zip", "UTF-8");
  651. final CountDownLatch count = new CountDownLatch(registerBookPdfList.size());
  652. ServletOutputStream outputStream = response.getOutputStream();
  653. ZipOutputStream zos = new ZipOutputStream(outputStream);
  654. response.setContentType("application/octet-stream");
  655. response.setHeader("Content-Disposition", "attachment; filename=" + zipName);
  656. List<PdfToZipTempVo> pdfToZipTempVoList = registerBookPdfList.parallelStream().map(pdf -> {
  657. return resolve(pdf, count);
  658. }).filter(Objects::nonNull)
  659. .collect(Collectors.toList());
  660. count.await();
  661. pdfToZipTempVoList.removeIf(pdfToZipTempVo -> !FileUtil.exist(pdfToZipTempVo.getFile()));
  662. log.info("登记簿全部下载完成,开始压缩文件,数量:{}", pdfToZipTempVoList.size());
  663. for (PdfToZipTempVo tempVo : pdfToZipTempVoList) {
  664. log.info("当前开始处理第{}个文件 ", pdfToZipTempVoList.indexOf(tempVo) + 1);
  665. deal(zos, tempVo);
  666. }
  667. zos.flush();
  668. zos.close();
  669. outputStream.close();
  670. log.info("登记簿批量导出压缩文件完成,文件数量:{}", pdfToZipTempVoList.size());
  671. } catch (Throwable e) {
  672. String errMsg = String.format("登记簿导出失败:%s", e);
  673. log.error(errMsg);
  674. } finally {
  675. final File temp = new File(TEMP_DIR_NAME);
  676. if (temp.exists()) {
  677. FileUtil.del(temp);
  678. log.info("临时目录已删除");
  679. }
  680. }
  681. }
  682. public PdfToZipTempVo resolve(CoreRegisterBookPdfPageVo pdf, CountDownLatch count) {
  683. final File temp = new File(TEMP_DIR_NAME);
  684. if (!temp.exists()) {
  685. temp.mkdirs();
  686. }
  687. InputStream inputStream;
  688. try {
  689. inputStream = getFileStream(pdf.getFileUrl());
  690. if (ObjectUtil.isEmpty(inputStream)) {
  691. log.error("登记簿导出失败,文件不存在,文件名:{}", pdf.getFileUrl());
  692. throw new RuntimeException("登记簿导出失败,文件不存在");
  693. }
  694. final String pdfFileName = pdf.getFileName();
  695. //pdfFileName==null的时候在下面会报错此处加个判断,要处理问题还需要在问题源头除处理
  696. // registerBookPdfBatchExportTempDir (Is a directory)
  697. if (StringUtils.isEmpty(pdfFileName)) {
  698. throw new RuntimeException("登记簿导出失败,文件不存在");
  699. }
  700. final PdfToZipTempVo tempVo = new PdfToZipTempVo();
  701. //tempVo.setBytes(bytes);
  702. tempVo.setFileName(pdfFileName);
  703. tempVo.setOrgId(pdf.getOrgId());
  704. tempVo.setOrgName(pdf.getOrgName());
  705. tempVo.setRegisterBookType(RegisterBookType.getEnums(pdf.getRegisterBookType()));
  706. final File file = new File(temp + File.separator + pdfFileName);
  707. final FileOutputStream outputStream = new FileOutputStream(file);
  708. byte[] buffer = new byte[4096];
  709. int len;
  710. while ((len = inputStream.read(buffer)) > 0) {
  711. outputStream.write(buffer, 0, len);
  712. }
  713. outputStream.close();
  714. inputStream.close();
  715. tempVo.setFile(file);
  716. final List<IdName<Long, String>> idNameList = pdf.getOrgList();
  717. if (ObjectUtil.isEmpty(idNameList)) {
  718. tempVo.setEntryName(File.separator + RegisterBookType.getEnums(pdf.getRegisterBookType()).getText() + File.separator + pdfFileName);
  719. return tempVo;
  720. }
  721. StringJoiner stringJoiner = new StringJoiner(File.separator);
  722. for (IdName<Long, String> org : idNameList) {
  723. stringJoiner.add(org.getName());
  724. if (idNameList.lastIndexOf(org) == idNameList.size() - 1) {
  725. stringJoiner.add(RegisterBookType.getEnums(pdf.getRegisterBookType()).getText() + File.separator + pdfFileName);
  726. }
  727. }
  728. tempVo.setEntryName(stringJoiner.toString());
  729. return tempVo;
  730. } catch (Exception e) {
  731. log.error("读取文件失败", e);
  732. return null;
  733. } finally {
  734. count.countDown();
  735. }
  736. }
  737. public void deal(ZipOutputStream zos, PdfToZipTempVo zipTempVo) throws Throwable {
  738. zos.setEncoding("GBK");
  739. FileInputStream fileInputStream = new FileInputStream(zipTempVo.getFile());
  740. zos.putNextEntry(new ZipEntry(zipTempVo.getEntryName()));
  741. byte[] buffer = new byte[4096];
  742. int len;
  743. while ((len = fileInputStream.read(buffer)) > 0) {
  744. zos.write(buffer, 0, len);
  745. }
  746. zos.closeEntry();
  747. fileInputStream.close();
  748. }
  749. private List<SysOrgVO> getChildrenList(Long orgId) {
  750. List<SysOrgVO> cacheList = RedisUtils.getCacheList(CacheConstants.ORG_CACHE_LIST_KEY);
  751. return cacheList.stream()
  752. .filter(org -> ObjectUtil.equal(org.getParentId(), orgId))
  753. .collect(Collectors.toList());
  754. }
  755. private SysOrgVO getCurOrg(Long orgId) {
  756. List<SysOrgVO> cacheList = RedisUtils.getCacheList(CacheConstants.ORG_CACHE_LIST_KEY);
  757. return cacheList.stream()
  758. .filter(org -> ObjectUtil.equal(org.getId(), orgId))
  759. .findFirst().get();
  760. }
  761. @Override
  762. public void cutFileCompress(CoreRegisterBookPdfExportDto pdfDto) {
  763. SysOrgVO org = getCurOrg(pdfDto.getOrgId());
  764. Date date = new Date();
  765. String fileName = pdfDto.getIsRegisterBookPage() ? org.getName() + "_登记簿_" : org.getName() + "_数据报表_";
  766. //判断需要分几片导出
  767. List<List<CoreRegisterBookPdfPageVo>> lists = checkSubList(pdfDto);
  768. int num = 1;
  769. try {
  770. for (List<CoreRegisterBookPdfPageVo> list : lists) {
  771. CountDownLatch count = new CountDownLatch(list.size());
  772. String zipName = null;
  773. String str = lists.size() == 1 ? "" : "_part_" + num;
  774. String fileNameStr = fileName + DateHelper.getDateString(new Date()) + str;
  775. zipName = URLEncoder.encode(fileNameStr + ".zip", "UTF-8");
  776. List<PdfToZipTempVo> pdfToZipTempVoList = list.parallelStream().map(pdf -> {
  777. return resolve(pdf, count);
  778. }).filter(Objects::nonNull)
  779. .collect(Collectors.toList());
  780. pdfToZipTempVoList.removeIf(pdfToZipTempVo -> !FileUtil.exist(pdfToZipTempVo.getFile()));
  781. log.info("登记簿全部下载完成,开始压缩文件,数量:{}", pdfToZipTempVoList.size());
  782. String encodedFileName = URLEncoder.encode(CacheConstants.REGISTER_PDF_FILE_KEY + DateHelper.getDateString(date) + str + ".zip", "UTF-8");
  783. String filePath = this.localFilePath + File.separator + encodedFileName;
  784. File file = FileUtils.getFile(filePath);
  785. FileOutputStream fos = new FileOutputStream(file);
  786. ZipOutputStream zos = new ZipOutputStream(fos);
  787. long fileSize = 0L;
  788. for (PdfToZipTempVo tempVo : pdfToZipTempVoList) {
  789. fileSize += tempVo.getFile().length();
  790. log.info("当前开始处理第{}个文件 ", pdfToZipTempVoList.indexOf(tempVo) + 1);
  791. deal(zos, tempVo);
  792. }
  793. zos.flush();
  794. fos.flush();
  795. zos.close();
  796. fos.close();
  797. num++;
  798. saveFileDataToRedis(org, date, zipName, filePath, fileSize, pdfDto);
  799. }
  800. } catch (Throwable e) {
  801. throw new RuntimeException(e);
  802. } finally {
  803. File file = new File(TEMP_DIR_NAME);
  804. if (file.exists()) {
  805. FileUtil.del(file);
  806. }
  807. }
  808. }
  809. private void saveFileDataToRedis(SysOrgVO org, Date date, String zipName, String localFileName, long fileSize, CoreRegisterBookPdfExportDto pdfDto) throws UnsupportedEncodingException {
  810. PdfLocalFileTempVo pdfLocalFileTempVo = new PdfLocalFileTempVo();
  811. pdfLocalFileTempVo.setOrgId(pdfDto.getOrgId());
  812. pdfLocalFileTempVo.setOrgName(org.getName());
  813. pdfLocalFileTempVo.setOrgPath(org.getPath());
  814. pdfLocalFileTempVo.setLocalFileName(localFileName);
  815. pdfLocalFileTempVo.setZipName(URLDecoder.decode(zipName, "UTF-8"));
  816. pdfLocalFileTempVo.setFileSize(changeUnit(fileSize));
  817. pdfLocalFileTempVo.setDownLoadTime(DateUtil.format(date, "yyyy-MM-dd HH:mm:ss"));
  818. pdfLocalFileTempVo.setIsRegisterBookPage(pdfDto.getIsRegisterBookPage());
  819. pdfLocalFileTempVo.setCreateTime(new Date());
  820. //此处localFileName 为文件的绝对路径,存在redis延迟队列中,一个小时后删除文件
  821. localFileName = localFileName.replace(this.localFilePath + File.separator, "");
  822. //此处localFileName 为文件名称,存入redis中,用于页面展示文件名称,下载
  823. //RedisUtils.setCacheObject(URLDecoder.decode(localFileName, "UTF-8"), JSON.toJSONString(pdfLocalFileTempVo),true);
  824. final BoundValueOperations ops = redisTemplate.boundValueOps(URLDecoder.decode(localFileName, "UTF-8"));
  825. ops.set(JSON.toJSONString(pdfLocalFileTempVo));
  826. ops.expireAt(DateUtil.endOfDay(new Date()));
  827. }
  828. private List<List<CoreRegisterBookPdfPageVo>> checkSubList(CoreRegisterBookPdfExportDto pdfDto) {
  829. List<List<CoreRegisterBookPdfPageVo>> list = new ArrayList<>();
  830. List<CoreRegisterBookPdfPageVo> registerBookPdfList = pdfDto.getDataList();
  831. if (CollectionUtil.isEmpty(registerBookPdfList)) {
  832. throw new RuntimeException("暂无可下载数据!");
  833. }
  834. //小于3000条直接导出
  835. if (registerBookPdfList.size() < 3000) {
  836. list.add(registerBookPdfList);
  837. return list;
  838. }
  839. List<SysOrgVO> orgList = getChildrenList(pdfDto.getOrgId());
  840. //大于3000条需要分片
  841. return splitList(registerBookPdfList, orgList, 3000);
  842. }
  843. public static List<List<CoreRegisterBookPdfPageVo>> splitList(List<CoreRegisterBookPdfPageVo> list, List<SysOrgVO> orgList, int size) {
  844. // 根据机构分组
  845. List<List<CoreRegisterBookPdfPageVo>> collect = orgList.stream().map(org -> {
  846. List<CoreRegisterBookPdfPageVo> arrayList = new ArrayList<>();
  847. for (CoreRegisterBookPdfPageVo pdf : list) {
  848. if (pdf.getOrgPath().startsWith(org.getPath())) {
  849. arrayList.add(pdf);
  850. }
  851. }
  852. return arrayList;
  853. }).filter(ObjectUtil::isNotEmpty).collect(Collectors.toList());
  854. // 合并不满3000的数组
  855. List<List<CoreRegisterBookPdfPageVo>> merged = new LinkedList<>();
  856. List<CoreRegisterBookPdfPageVo> current = new ArrayList<>();
  857. for (List<CoreRegisterBookPdfPageVo> group : collect) {
  858. if (group.size() >= size) {
  859. merged.add(group);
  860. } else {
  861. current.addAll(group);
  862. if (current.size() >= size) {
  863. merged.add(current);
  864. current = new ArrayList<>();
  865. }
  866. }
  867. }
  868. if (!current.isEmpty()) {
  869. merged.add(current);
  870. }
  871. return merged;
  872. //return merged.stream().limit(size).collect(Collectors.toList());
  873. }
  874. public String changeUnit(Long fileSize) {
  875. if (fileSize == null || fileSize == 0) {
  876. return "0";
  877. }
  878. //将fileSize转换为MB,保留整数
  879. BigDecimal fileSizeMB = new BigDecimal(fileSize).divide(new BigDecimal(1024 * 1024), 2, BigDecimal.ROUND_HALF_UP);
  880. return fileSizeMB.toString();
  881. }
  882. @Override
  883. public void deletedZipFile() {
  884. final File dir = FileUtils.getFile(this.localFilePath);
  885. final File[] files = dir.listFiles();
  886. if (ObjectUtil.isNull(files) || ObjectUtil.isEmpty(files)) {
  887. return;
  888. }
  889. Arrays.stream(files)
  890. .filter(file -> file.getName().startsWith(CacheConstants.REGISTER_PDF_FILE_KEY))
  891. .filter(file -> file.getName().endsWith(".zip"))
  892. .forEach(FileUtil::del);
  893. }
  894. }