LocalSysFileServiceImpl.java 39 KB

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