LocalSysFileServiceImpl.java 38 KB

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