LocalSysFileServiceImpl.java 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  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. PdfUtil.dealHeader(document, fs, "监控调阅情况登记簿", 14);
  285. String checkTime = data.getTaskStartTime();
  286. String checkUser = data.getCheckUser();
  287. Font titleFont = new Font(fs, 8, Font.NORMAL);
  288. PdfUtil.createPDFCell(titleFont, table, "调阅单位", Element.ALIGN_MIDDLE, 10, 0);
  289. PdfUtil.createPDFCell(titleFont, table, data.getOrgName(), Element.ALIGN_MIDDLE, 13, 0);
  290. PdfUtil.createPDFCell(titleFont, table, "调阅人", Element.ALIGN_MIDDLE, 8, 0);
  291. PdfUtil.createPDFCell(titleFont, table, checkUser, Element.ALIGN_MIDDLE, 15, 0);
  292. PdfUtil.createPDFCell(titleFont, table, "调阅时间", Element.ALIGN_MIDDLE, 10, 0);
  293. PdfUtil.createPDFCell(titleFont, table, checkTime, Element.ALIGN_MIDDLE, 36, 0);
  294. PdfUtil.createPDFCell(titleFont, table, "发现问题情况", Element.ALIGN_MIDDLE, 46, 0);
  295. PdfUtil.dealAccessPBody(document, table, tableFont, titleFont, data.getDataVos());
  296. document.close();
  297. writer.close();
  298. log.info("监控调阅登记簿生成结束,当前绝对地址为:{}", afterStr);
  299. //此处返回 /statics/edu/xxx.pdf
  300. return this.prefix + pathVo.getRelativePath();
  301. }
  302. @Override
  303. public String generateDrillPdf(Map<String, Object> data) throws Exception {
  304. PdfFilePathVo pathVo = getLocalFilePath("drill", data.get("fileName").toString());
  305. String afterStr = StringEscapeUtils.escapeEcmaScript(pathVo.getTempFileName());
  306. log.info("开始生成预案演练登记簿,当前绝对地址为:{}", afterStr);
  307. final ItextPdfTableVo pdfTableVo = PdfUtil.createTable(pathVo.getTempFileName(), 6, 10);
  308. final Document document = pdfTableVo.getDocument();
  309. final PdfWriter writer = pdfTableVo.getWriter();
  310. final PdfPTable table = pdfTableVo.getTable();
  311. final BaseFont fs = pdfTableVo.getFs();
  312. final Font tableFont = pdfTableVo.getTableFont();
  313. document.open();
  314. PdfUtil.dealHeader(document, fs, "预 案 演 练 记 录", 24);
  315. PdfUtil.dealDrillBody(document, table, tableFont, data);
  316. document.close();
  317. writer.close();
  318. log.info("预案演练登记簿生成结束,当前绝对地址为:{}", afterStr);
  319. PdfUtil.addPageNum(pathVo.getTempFileName(), pathVo.getAbsolutePath(), fs, tableFont);
  320. //此处返回 /statics/edu/xxx.pdf
  321. return this.prefix + pathVo.getRelativePath();
  322. }
  323. @Override
  324. public String generateOutInPdf(Map<String, Object> data) throws Exception {
  325. PdfFilePathVo pathVo = getLocalFilePath("visit", data.get("fileName").toString());
  326. String afterStr = StringEscapeUtils.escapeEcmaScript(pathVo.getAbsolutePath());
  327. log.info("开始生成来访管理登记簿,当前绝对地址为:{}", afterStr);
  328. final ItextPdfTableVo pdfTableVo = PdfUtil.createTable(pathVo.getAbsolutePath(), 6, 10);
  329. final Document document = pdfTableVo.getDocument();
  330. final PdfWriter writer = pdfTableVo.getWriter();
  331. final PdfPTable table = pdfTableVo.getTable();
  332. final BaseFont fs = pdfTableVo.getFs();
  333. final Font tableFont = pdfTableVo.getTableFont();
  334. PdfUtil.dealHeader(document, fs, "来 访 管 理 登 记 簿", 24);
  335. PdfUtil.dealOutInBody(document, table, tableFont, data);
  336. document.close();
  337. writer.close();
  338. log.info("来访管理登记簿生成结束,当前绝对地址为:{}", afterStr);
  339. //此处返回 /statics/edu/xxx.pdf
  340. return this.prefix + pathVo.getRelativePath();
  341. }
  342. @Override
  343. public String uploadFileBase64(FileBase64Vo file) throws Exception {
  344. String filePath = FileUploadUtils.uploadBase64(localFilePath, file);
  345. return filePath;
  346. }
  347. /**
  348. * 履职转pdf
  349. *
  350. * @param dest
  351. * @throws IOException
  352. * @throws DocumentException
  353. */
  354. public void dataToPdf(String dest) throws IOException, DocumentException {
  355. Document document = new Document();
  356. PdfWriter.getInstance(document, new FileOutputStream(dest));
  357. document.open();
  358. // 使用语言包字体
  359. BaseFont abf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
  360. //字体
  361. Font font = new Font(abf, 8);
  362. //段落
  363. Paragraph p = new Paragraph("测试结算单", new Font(abf, 12, Font.BOLD));
  364. p.setAlignment(Paragraph.ALIGN_CENTER);
  365. document.add(p);
  366. //表格
  367. PdfPTable table = new PdfPTable(8);//numcolumns:列数
  368. table.setSpacingBefore(16f);//表格与上面段落的空隙
  369. //表格列创建并赋值
  370. PdfPCell cell = new PdfPCell(new Phrase("单位名称:测试有限公司", font));
  371. cell.setHorizontalAlignment(Element.ALIGN_LEFT);//居中
  372. cell.disableBorderSide(13);//去除左右上边框,保留下边框
  373. cell.setColspan(4);//合并列数
  374. table.addCell(cell);
  375. cell = new PdfPCell(new Phrase("日期:2020-06-05", font));
  376. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  377. cell.disableBorderSide(13);
  378. cell.setColspan(3);
  379. table.addCell(cell);
  380. cell = new PdfPCell(new Phrase("单位(元)", font));
  381. cell.setHorizontalAlignment(Element.ALIGN_LEFT);
  382. cell.disableBorderSide(13);
  383. cell.setColspan(1);
  384. table.addCell(cell);
  385. //首行
  386. cell = new PdfPCell(new Phrase("期间", font));
  387. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  388. cell.setColspan(2);
  389. table.addCell(cell);
  390. cell = new PdfPCell(new Phrase("月份", font));
  391. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  392. table.addCell(cell);
  393. cell = new PdfPCell(new Phrase("分类", font));
  394. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  395. table.addCell(cell);
  396. cell = new PdfPCell(new Phrase("年利率", font));
  397. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  398. table.addCell(cell);
  399. cell = new PdfPCell(new Phrase("日利率", font));
  400. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  401. table.addCell(cell);
  402. cell = new PdfPCell(new Phrase("基数", font));
  403. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  404. table.addCell(cell);
  405. cell = new PdfPCell(new Phrase("利息", font));
  406. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  407. table.addCell(cell);
  408. cell = new PdfPCell(new Phrase("起始日:2020-03-26\n" +
  409. "结束日:2020-04-25", font));
  410. cell.setPadding(16f);
  411. cell.setVerticalAlignment(Element.ALIGN_CENTER);
  412. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  413. cell.setRowspan(3);
  414. cell.setColspan(2);
  415. table.addCell(cell);
  416. cell = new PdfPCell(new Phrase("4", font));
  417. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  418. table.addCell(cell);
  419. cell = new PdfPCell(new Phrase("资金", font));
  420. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  421. table.addCell(cell);
  422. cell = new PdfPCell(new Phrase("1.10%", font));
  423. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  424. table.addCell(cell);
  425. cell = new PdfPCell(new Phrase("0.000031", font));
  426. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  427. table.addCell(cell);
  428. cell = new PdfPCell(new Phrase("10598164.91", font));
  429. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  430. table.addCell(cell);
  431. cell = new PdfPCell(new Phrase("325.01", font));
  432. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  433. table.addCell(cell);
  434. cell = new PdfPCell(new Phrase("4", 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("1.10%", font));
  441. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  442. table.addCell(cell);
  443. cell = new PdfPCell(new Phrase("0.000031", 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("4", font));
  453. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  454. table.addCell(cell);
  455. cell = new PdfPCell(new Phrase("资金", font));
  456. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  457. table.addCell(cell);
  458. cell = new PdfPCell(new Phrase("1.10%", font));
  459. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  460. table.addCell(cell);
  461. cell = new PdfPCell(new Phrase("0.000031", font));
  462. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  463. table.addCell(cell);
  464. cell = new PdfPCell(new Phrase("-", font));
  465. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  466. table.addCell(cell);
  467. cell = new PdfPCell(new Phrase("-", font));
  468. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  469. table.addCell(cell);
  470. cell = new PdfPCell(new Phrase("合计", font));
  471. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  472. cell.setColspan(7);
  473. table.addCell(cell);
  474. cell = new PdfPCell(new Phrase("325.01", font));
  475. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  476. table.addCell(cell);
  477. cell = new PdfPCell(new Phrase("会计制单:", font));
  478. cell.setHorizontalAlignment(Element.ALIGN_LEFT);
  479. cell.disableBorderSide(14);
  480. cell.setColspan(4);
  481. table.addCell(cell);
  482. cell = new PdfPCell(new Phrase("复核:", font));
  483. cell.setHorizontalAlignment(Element.ALIGN_LEFT);
  484. cell.disableBorderSide(14);
  485. cell.setColspan(4);
  486. table.addCell(cell);
  487. table.setSpacingBefore(16f);
  488. document.add(table);
  489. //下一页
  490. document.newPage();
  491. //段落
  492. Paragraph p1 = new Paragraph("下一页测试结算单", new Font(abf, 12, Font.BOLD));
  493. p1.setAlignment(Paragraph.ALIGN_CENTER);
  494. document.add(p1);
  495. //表格
  496. table = new PdfPTable(8);//numcolumns:列数
  497. table.setSpacingBefore(16f);//表格与上面段落的空隙
  498. //表格列创建并赋值
  499. cell = new PdfPCell(new Phrase("单位名称:测试有限公司", font));
  500. cell.setHorizontalAlignment(Element.ALIGN_LEFT);//居中
  501. cell.disableBorderSide(13);//去除左右上边框,保留下边框
  502. cell.setColspan(4);//合并列数
  503. table.addCell(cell);
  504. cell = new PdfPCell(new Phrase("日期:2020-06-05", font));
  505. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  506. cell.disableBorderSide(13);
  507. cell.setColspan(3);
  508. table.addCell(cell);
  509. cell = new PdfPCell(new Phrase("单位(元)", font));
  510. cell.setHorizontalAlignment(Element.ALIGN_LEFT);
  511. cell.disableBorderSide(13);
  512. cell.setColspan(1);
  513. table.addCell(cell);
  514. //首行
  515. cell = new PdfPCell(new Phrase("期间", font));
  516. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  517. cell.setColspan(2);
  518. table.addCell(cell);
  519. cell = new PdfPCell(new Phrase("月份", font));
  520. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  521. table.addCell(cell);
  522. cell = new PdfPCell(new Phrase("分类", font));
  523. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  524. table.addCell(cell);
  525. cell = new PdfPCell(new Phrase("年利率", font));
  526. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  527. table.addCell(cell);
  528. cell = new PdfPCell(new Phrase("日利率", font));
  529. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  530. table.addCell(cell);
  531. cell = new PdfPCell(new Phrase("基数", font));
  532. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  533. table.addCell(cell);
  534. cell = new PdfPCell(new Phrase("利息", font));
  535. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  536. table.addCell(cell);
  537. cell = new PdfPCell(new Phrase("起始日:2020-04-26\n" +
  538. "结束日:2020-05-25", font));
  539. cell.setPadding(16f);
  540. cell.setVerticalAlignment(Element.ALIGN_CENTER);
  541. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  542. cell.setColspan(2);
  543. table.addCell(cell);
  544. cell = new PdfPCell(new Phrase("4", font));
  545. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  546. table.addCell(cell);
  547. cell = new PdfPCell(new Phrase("资金", font));
  548. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  549. table.addCell(cell);
  550. cell = new PdfPCell(new Phrase("1.10%", font));
  551. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  552. table.addCell(cell);
  553. cell = new PdfPCell(new Phrase("0.000031", font));
  554. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  555. table.addCell(cell);
  556. cell = new PdfPCell(new Phrase("10598164.91", font));
  557. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  558. table.addCell(cell);
  559. cell = new PdfPCell(new Phrase("325.01", font));
  560. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  561. table.addCell(cell);
  562. cell = new PdfPCell(new Phrase("合计", font));
  563. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  564. cell.setColspan(7);
  565. table.addCell(cell);
  566. cell = new PdfPCell(new Phrase("325.01", font));
  567. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  568. table.addCell(cell);
  569. cell = new PdfPCell(new Phrase("会计制单:", font));
  570. cell.setHorizontalAlignment(Element.ALIGN_LEFT);
  571. cell.disableBorderSide(14);
  572. cell.setColspan(4);
  573. table.addCell(cell);
  574. cell = new PdfPCell(new Phrase("复核:", font));
  575. cell.setHorizontalAlignment(Element.ALIGN_LEFT);
  576. cell.disableBorderSide(14);
  577. cell.setColspan(4);
  578. table.addCell(cell);
  579. table.setSpacingBefore(16f);
  580. document.add(table);
  581. document.close();
  582. }
  583. @Override
  584. public String absolutePath(String path) {
  585. if (ObjectUtil.isEmpty(path)) {
  586. return StringUtil.EMPTY_STRING;
  587. }
  588. if (path.startsWith(this.localFilePath)) {
  589. return path;
  590. }
  591. if (path.startsWith(this.prefix)) {
  592. return this.localFilePath + path.substring(this.prefix.length());
  593. }
  594. return StringUtil.EMPTY_STRING;
  595. }
  596. @Override
  597. public InputStream getFileStream(String path) throws IOException {
  598. String absolutePath = this.absolutePath(path);
  599. File file = FileUtils.getFile(absolutePath);
  600. return Files.newInputStream(file.toPath());
  601. }
  602. @Override
  603. public void registerBookCompressPdf(ExportPdfDto pdfDto, HttpServletResponse response) throws IOException {
  604. final List<CoreRegisterBookPdfPageVo> registerBookPdfList = pdfDto.getRegisterBookPdfList();
  605. try {
  606. String zipName = URLEncoder.encode(pdfDto.getFileName() + DateHelper.getDateString(new Date()) + ".zip", "UTF-8");
  607. final CountDownLatch count = new CountDownLatch(registerBookPdfList.size());
  608. ServletOutputStream outputStream = response.getOutputStream();
  609. ZipOutputStream zos = new ZipOutputStream(outputStream);
  610. response.setContentType("application/octet-stream");
  611. response.setHeader("Content-Disposition", "attachment; filename=" + zipName);
  612. List<PdfToZipTempVo> pdfToZipTempVoList = registerBookPdfList.parallelStream().map(pdf -> {
  613. return resolve(pdf, count);
  614. }).filter(Objects::nonNull)
  615. .collect(Collectors.toList());
  616. count.await();
  617. pdfToZipTempVoList.removeIf(pdfToZipTempVo -> !FileUtil.exist(pdfToZipTempVo.getFile()));
  618. log.info("登记簿全部下载完成,开始压缩文件,数量:{}", pdfToZipTempVoList.size());
  619. for (PdfToZipTempVo tempVo : pdfToZipTempVoList) {
  620. log.info("当前开始处理第{}个文件 ", pdfToZipTempVoList.indexOf(tempVo) + 1);
  621. deal(zos, tempVo);
  622. }
  623. zos.flush();
  624. zos.close();
  625. outputStream.close();
  626. log.info("登记簿批量导出压缩文件完成,文件数量:{}", pdfToZipTempVoList.size());
  627. } catch (Throwable e) {
  628. String errMsg = String.format("登记簿导出失败:%s", e);
  629. log.error(errMsg);
  630. } finally {
  631. final File temp = new File(TEMP_DIR_NAME);
  632. if (temp.exists()) {
  633. FileUtil.del(temp);
  634. log.info("临时目录已删除");
  635. }
  636. }
  637. }
  638. public PdfToZipTempVo resolve(CoreRegisterBookPdfPageVo pdf, CountDownLatch count) {
  639. final File temp = new File(TEMP_DIR_NAME);
  640. if (!temp.exists()) {
  641. temp.mkdirs();
  642. }
  643. InputStream inputStream;
  644. try {
  645. inputStream = getFileStream(pdf.getFileUrl());
  646. if (ObjectUtil.isEmpty(inputStream)) {
  647. log.error("登记簿导出失败,文件不存在,文件名:{}", pdf.getFileUrl());
  648. throw new RuntimeException("登记簿导出失败,文件不存在");
  649. }
  650. final String pdfFileName = pdf.getFileName();
  651. //pdfFileName==null的时候在下面会报错此处加个判断,要处理问题还需要在问题源头除处理
  652. // registerBookPdfBatchExportTempDir (Is a directory)
  653. if (StringUtils.isEmpty(pdfFileName)) {
  654. throw new RuntimeException("登记簿导出失败,文件不存在");
  655. }
  656. final PdfToZipTempVo tempVo = new PdfToZipTempVo();
  657. //tempVo.setBytes(bytes);
  658. tempVo.setFileName(pdfFileName);
  659. tempVo.setOrgId(pdf.getOrgId());
  660. tempVo.setOrgName(pdf.getOrgName());
  661. tempVo.setRegisterBookType(RegisterBookType.getEnums(pdf.getRegisterBookType()));
  662. final File file = new File(temp + File.separator + pdfFileName);
  663. final FileOutputStream outputStream = new FileOutputStream(file);
  664. byte[] buffer = new byte[4096];
  665. int len;
  666. while ((len = inputStream.read(buffer)) > 0) {
  667. outputStream.write(buffer, 0, len);
  668. }
  669. outputStream.close();
  670. inputStream.close();
  671. tempVo.setFile(file);
  672. final List<IdName<Long, String>> idNameList = pdf.getOrgList();
  673. if (ObjectUtil.isEmpty(idNameList)) {
  674. tempVo.setEntryName(File.separator + RegisterBookType.getEnums(pdf.getRegisterBookType()).getText() + File.separator + pdfFileName);
  675. return tempVo;
  676. }
  677. StringJoiner stringJoiner = new StringJoiner(File.separator);
  678. for (IdName<Long, String> org : idNameList) {
  679. stringJoiner.add(org.getName());
  680. if (idNameList.lastIndexOf(org) == idNameList.size() - 1) {
  681. stringJoiner.add(RegisterBookType.getEnums(pdf.getRegisterBookType()).getText() + File.separator + pdfFileName);
  682. }
  683. }
  684. tempVo.setEntryName(stringJoiner.toString());
  685. return tempVo;
  686. } catch (Exception e) {
  687. log.error("读取文件失败", e);
  688. return null;
  689. } finally {
  690. count.countDown();
  691. }
  692. }
  693. public void deal(ZipOutputStream zos, PdfToZipTempVo zipTempVo) throws Throwable {
  694. zos.setEncoding("GBK");
  695. FileInputStream fileInputStream = new FileInputStream(zipTempVo.getFile());
  696. zos.putNextEntry(new ZipEntry(zipTempVo.getEntryName()));
  697. byte[] buffer = new byte[4096];
  698. int len;
  699. while ((len = fileInputStream.read(buffer)) > 0) {
  700. zos.write(buffer, 0, len);
  701. }
  702. zos.closeEntry();
  703. fileInputStream.close();
  704. }
  705. private List<SysOrgVO> getChildrenList(Long orgId) {
  706. List<SysOrgVO> cacheList = RedisUtils.getCacheList(CacheConstants.ORG_CACHE_LIST_KEY);
  707. return cacheList.stream()
  708. .filter(org -> ObjectUtil.equal(org.getParentId(), orgId))
  709. .collect(Collectors.toList());
  710. }
  711. private SysOrgVO getCurOrg(Long orgId) {
  712. List<SysOrgVO> cacheList = RedisUtils.getCacheList(CacheConstants.ORG_CACHE_LIST_KEY);
  713. return cacheList.stream()
  714. .filter(org -> ObjectUtil.equal(org.getId(), orgId))
  715. .findFirst().get();
  716. }
  717. @Override
  718. public void cutFileCompress(CoreRegisterBookPdfExportDto pdfDto) {
  719. SysOrgVO org = getCurOrg(pdfDto.getOrgId());
  720. Date date = new Date();
  721. String fileName = pdfDto.getIsRegisterBookPage() ? org.getName() + "_登记簿_" : org.getName() + "_数据报表_";
  722. //判断需要分几片导出
  723. List<List<CoreRegisterBookPdfPageVo>> lists = checkSubList(pdfDto);
  724. int num = 1;
  725. try {
  726. for (List<CoreRegisterBookPdfPageVo> list : lists) {
  727. CountDownLatch count = new CountDownLatch(list.size());
  728. String zipName = null;
  729. String str = lists.size() == 1 ? "" : "_part_" + num;
  730. String fileNameStr = fileName + DateHelper.getDateString(new Date()) + str;
  731. zipName = URLEncoder.encode(fileNameStr + ".zip", "UTF-8");
  732. List<PdfToZipTempVo> pdfToZipTempVoList = list.parallelStream().map(pdf -> {
  733. return resolve(pdf, count);
  734. }).filter(Objects::nonNull)
  735. .collect(Collectors.toList());
  736. pdfToZipTempVoList.removeIf(pdfToZipTempVo -> !FileUtil.exist(pdfToZipTempVo.getFile()));
  737. log.info("登记簿全部下载完成,开始压缩文件,数量:{}", pdfToZipTempVoList.size());
  738. String encodedFileName = URLEncoder.encode(CacheConstants.REGISTER_PDF_FILE_KEY + DateHelper.getDateString(date) + str + ".zip", "UTF-8");
  739. String filePath = this.localFilePath + File.separator + encodedFileName;
  740. File file = FileUtils.getFile(filePath);
  741. FileOutputStream fos = new FileOutputStream(file);
  742. ZipOutputStream zos = new ZipOutputStream(fos);
  743. long fileSize = 0L;
  744. for (PdfToZipTempVo tempVo : pdfToZipTempVoList) {
  745. fileSize += tempVo.getFile().length();
  746. log.info("当前开始处理第{}个文件 ", pdfToZipTempVoList.indexOf(tempVo) + 1);
  747. deal(zos, tempVo);
  748. }
  749. zos.flush();
  750. fos.flush();
  751. zos.close();
  752. fos.close();
  753. num++;
  754. saveFileDataToRedis(org, date, zipName, filePath, fileSize, pdfDto);
  755. }
  756. } catch (Throwable e) {
  757. throw new RuntimeException(e);
  758. } finally {
  759. File file = new File(TEMP_DIR_NAME);
  760. if (file.exists()) {
  761. FileUtil.del(file);
  762. }
  763. }
  764. }
  765. private void saveFileDataToRedis(SysOrgVO org, Date date, String zipName, String localFileName, long fileSize, CoreRegisterBookPdfExportDto pdfDto) throws UnsupportedEncodingException {
  766. PdfLocalFileTempVo pdfLocalFileTempVo = new PdfLocalFileTempVo();
  767. pdfLocalFileTempVo.setOrgId(pdfDto.getOrgId());
  768. pdfLocalFileTempVo.setOrgName(org.getName());
  769. pdfLocalFileTempVo.setOrgPath(org.getPath());
  770. pdfLocalFileTempVo.setLocalFileName(localFileName);
  771. pdfLocalFileTempVo.setZipName(URLDecoder.decode(zipName, "UTF-8"));
  772. pdfLocalFileTempVo.setFileSize(changeUnit(fileSize));
  773. pdfLocalFileTempVo.setDownLoadTime(DateUtil.format(date, "yyyy-MM-dd HH:mm:ss"));
  774. pdfLocalFileTempVo.setIsRegisterBookPage(pdfDto.getIsRegisterBookPage());
  775. pdfLocalFileTempVo.setCreateTime(new Date());
  776. //此处localFileName 为文件的绝对路径,存在redis延迟队列中,一个小时后删除文件
  777. localFileName = localFileName.replace(this.localFilePath + File.separator, "");
  778. //此处localFileName 为文件名称,存入redis中,用于页面展示文件名称,下载
  779. //RedisUtils.setCacheObject(URLDecoder.decode(localFileName, "UTF-8"), JSON.toJSONString(pdfLocalFileTempVo),true);
  780. final BoundValueOperations ops = redisTemplate.boundValueOps(URLDecoder.decode(localFileName, "UTF-8"));
  781. ops.set(JSON.toJSONString(pdfLocalFileTempVo));
  782. ops.expireAt(DateUtil.endOfDay(new Date()));
  783. }
  784. private List<List<CoreRegisterBookPdfPageVo>> checkSubList(CoreRegisterBookPdfExportDto pdfDto) {
  785. List<List<CoreRegisterBookPdfPageVo>> list = new ArrayList<>();
  786. List<CoreRegisterBookPdfPageVo> registerBookPdfList = pdfDto.getDataList();
  787. if (CollectionUtil.isEmpty(registerBookPdfList)) {
  788. throw new RuntimeException("暂无可下载数据!");
  789. }
  790. //小于3000条直接导出
  791. if (registerBookPdfList.size() < 3000) {
  792. list.add(registerBookPdfList);
  793. return list;
  794. }
  795. List<SysOrgVO> orgList = getChildrenList(pdfDto.getOrgId());
  796. //大于3000条需要分片
  797. return splitList(registerBookPdfList, orgList, 3000);
  798. }
  799. public static List<List<CoreRegisterBookPdfPageVo>> splitList(List<CoreRegisterBookPdfPageVo> list, List<SysOrgVO> orgList, int size) {
  800. // 根据机构分组
  801. List<List<CoreRegisterBookPdfPageVo>> collect = orgList.stream().map(org -> {
  802. List<CoreRegisterBookPdfPageVo> arrayList = new ArrayList<>();
  803. for (CoreRegisterBookPdfPageVo pdf : list) {
  804. if (pdf.getOrgPath().startsWith(org.getPath())) {
  805. arrayList.add(pdf);
  806. }
  807. }
  808. return arrayList;
  809. }).filter(ObjectUtil::isNotEmpty).collect(Collectors.toList());
  810. // 合并不满3000的数组
  811. List<List<CoreRegisterBookPdfPageVo>> merged = new LinkedList<>();
  812. List<CoreRegisterBookPdfPageVo> current = new ArrayList<>();
  813. for (List<CoreRegisterBookPdfPageVo> group : collect) {
  814. if (group.size() >= size) {
  815. merged.add(group);
  816. } else {
  817. current.addAll(group);
  818. if (current.size() >= size) {
  819. merged.add(current);
  820. current = new ArrayList<>();
  821. }
  822. }
  823. }
  824. if (!current.isEmpty()) {
  825. merged.add(current);
  826. }
  827. return merged;
  828. //return merged.stream().limit(size).collect(Collectors.toList());
  829. }
  830. public String changeUnit(Long fileSize) {
  831. if (fileSize == null || fileSize == 0) {
  832. return "0";
  833. }
  834. //将fileSize转换为MB,保留整数
  835. BigDecimal fileSizeMB = new BigDecimal(fileSize).divide(new BigDecimal(1024 * 1024), 2, BigDecimal.ROUND_HALF_UP);
  836. return fileSizeMB.toString();
  837. }
  838. @Override
  839. public void deletedZipFile() {
  840. final File dir = FileUtils.getFile(this.localFilePath);
  841. final File[] files = dir.listFiles();
  842. if (ObjectUtil.isNull(files) || ObjectUtil.isEmpty(files)) {
  843. return;
  844. }
  845. Arrays.stream(files)
  846. .filter(file -> file.getName().startsWith(CacheConstants.REGISTER_PDF_FILE_KEY))
  847. .filter(file -> file.getName().endsWith(".zip"))
  848. .forEach(FileUtil::del);
  849. }
  850. }