From 2a13304e89d6b31c3b21a851f817899b6c0b85a2 Mon Sep 17 00:00:00 2001 From: whb <17803890193@163.com> Date: Sat, 14 Sep 2024 14:55:33 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=AE=9E=E9=AA=8C=E6=8A=A5?= =?UTF-8?q?=E5=91=8A=E4=B8=8A=E4=BC=A0=E6=96=87=E6=A1=A3=E5=92=8C=E5=9B=BE?= =?UTF-8?q?=E7=89=87=E9=BB=98=E8=AE=A4=E8=BD=AC=E4=B8=BApdf=E5=92=8C?= =?UTF-8?q?=E6=95=99=E5=B8=88=E7=AB=AF=E5=B1=95=E7=A4=BA=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 71 ++- .../impl/StuConceptBlockServiceImpl.java | 84 ++- .../util/convertPDF/PDFConvertUtil.java | 538 ++++++++++++++++++ src/main/resources/application-dev.yml | 4 +- 4 files changed, 663 insertions(+), 34 deletions(-) create mode 100644 src/main/java/com/sztzjy/marketing/util/convertPDF/PDFConvertUtil.java diff --git a/pom.xml b/pom.xml index 92428ca..bd2e0bf 100644 --- a/pom.xml +++ b/pom.xml @@ -57,7 +57,7 @@ - + org.apache.commons commons-math3 @@ -66,19 +66,30 @@ - - com.squareup.okhttp3 - okhttp - 4.12.0 + + com.squareup.okhttp3 + okhttp + 4.12.0 + + + org.apache.poi + poi-scratchpad + 4.1.2 + compile + + + com.itextpdf + itextpdf + 5.5.13.3 + compile - - - - - - - + + + + + + @@ -93,15 +104,15 @@ 0.0.12 - - - - - - + + + + + + - + org.ujmp @@ -334,7 +345,7 @@ 9.1.22 - + com.kennycason kumo-core @@ -347,17 +358,27 @@ - + com.squareup.okhttp3 okhttp 4.9.3 + + + com.aspose + aspose-words + 18.10 + - com.ibeetl - admin-convert-pdf - 1.3.1 - compile + com.aspose + aspose-pdf + 18.2 + + + com.aspose + aspose-cells + 20.7 diff --git a/src/main/java/com/sztzjy/marketing/service/impl/StuConceptBlockServiceImpl.java b/src/main/java/com/sztzjy/marketing/service/impl/StuConceptBlockServiceImpl.java index 9f3228a..9e3dbf8 100644 --- a/src/main/java/com/sztzjy/marketing/service/impl/StuConceptBlockServiceImpl.java +++ b/src/main/java/com/sztzjy/marketing/service/impl/StuConceptBlockServiceImpl.java @@ -1,6 +1,7 @@ package com.sztzjy.marketing.service.impl; +import cn.hutool.core.io.FileUtil; import cn.hutool.core.util.IdUtil; import com.sztzjy.marketing.config.exception.handler.ServiceException; import com.sztzjy.marketing.entity.*; @@ -13,11 +14,13 @@ import com.sztzjy.marketing.util.ConvertUtil; import com.sztzjy.marketing.util.PdfUtil; import com.sztzjy.marketing.util.ResultEntity; import com.sztzjy.marketing.util.compute.ScoringUtil; +import com.sztzjy.marketing.util.convertPDF.PDFConvertUtil; import com.sztzjy.marketing.util.file.IFileUtil; import org.apache.poi.util.IOUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpStatus; +import org.springframework.mock.web.MockMultipartFile; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.Assert; @@ -277,6 +280,19 @@ public class StuConceptBlockServiceImpl implements StuConceptBlockService { if (stuBlockProductWithBLOBs == null) { throw new RuntimeException("模块或用户ID不能为空"); } + String upload = null; + String originalFilename = file.getOriginalFilename(); + int i = file.getOriginalFilename().lastIndexOf("."); + String substring = file.getOriginalFilename().substring(i + 1); + + boolean flag = false; + //将文件转为PDF + if ("doc".equals(substring) || "docx".equals(substring) + || "jpeg".equals(substring) || "png".equals(substring) || "jpg".equals(substring)) { + + flag = true; + + } //查询是否已有分数 StuPracticalTrainingReportExample stuPracticalTrainingReportExample = new StuPracticalTrainingReportExample(); @@ -292,11 +308,17 @@ public class StuConceptBlockServiceImpl implements StuConceptBlockService { } } //多次上传 更新 上限就六个 + StuPracticalTrainingReport report = stuPracticalTrainingReports.get(0); + if (flag) { + upload = convertFile(substring, file); + } - StuPracticalTrainingReport report = stuPracticalTrainingReports.get(0); //更新操作 - String upload = fileUtil.upload(file); + if (upload == null) { + upload = fileUtil.upload(file); + } + report.setUrl(upload); report.setUploadTime(new Date()); report.setSize((int) file.getSize()); @@ -306,8 +328,13 @@ public class StuConceptBlockServiceImpl implements StuConceptBlockService { return report; } else { + if (flag) { + upload = convertFile(substring, file); + } //第一次上传 - String upload = fileUtil.upload(file); + if (upload == null) { + upload = fileUtil.upload(file); + } StuPracticalTrainingReport report = new StuPracticalTrainingReport(); int uuid = UUID.randomUUID().hashCode(); @@ -359,7 +386,7 @@ public class StuConceptBlockServiceImpl implements StuConceptBlockService { // if (stuPracticalTrainingReportList.size() == 6) { // stuScoreDetails.setCompletionStatus("1"); // } - ArrayList arrayList = new ArrayList<>(); + List arrayList = new ArrayList<>(10); arrayList.add("AI文生图"); arrayList.add("AI修图"); arrayList.add("AI视频"); @@ -396,6 +423,47 @@ public class StuConceptBlockServiceImpl implements StuConceptBlockService { } + private String convertFile(String substring, MultipartFile file) { + String originalFilename = file.getOriginalFilename(); + + //格式不为pdf,转换 + try { + BufferedInputStream inputStream = FileUtil.getInputStream(convertMultipartFileToFile(file)); + + + String s = IdUtil.simpleUUID(); +// String filePath = "D:\\home"; + // 创建 File 对象 +// File infoFile = new File(filePath); +// if (!infoFile.exists()) { +// infoFile.mkdir(); +// } + FileOutputStream fileOutputStream = new FileOutputStream(filePath + "/pdf/" + s + ".pdf"); + if ("doc".equals(substring) || "docx".equals(substring)) { + PDFConvertUtil.convertToPDFByFileNameSuffix(originalFilename, inputStream, fileOutputStream); + } else if ("jpeg".equals(substring) || "png".equals(substring) || "jpg".equals(substring)) { + PDFConvertUtil.imgToPdf(inputStream, fileOutputStream); + } + FileInputStream fileInputStream = new FileInputStream(filePath + "/pdf/" + s + ".pdf"); + + MultipartFile multipartFile = new MockMultipartFile( + "example.txt", // 文件名 + s + ".pdf", // 原始文件名 + "pdf", // 文件类型 + fileInputStream + ); + String upload = localFileUtil.upload(multipartFile); + + fileInputStream.close(); + fileOutputStream.close(); + return upload; + } catch (FileNotFoundException e) { + throw new RuntimeException(e); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + /* * @author whb @@ -686,7 +754,8 @@ public class StuConceptBlockServiceImpl implements StuConceptBlockService { } @Override - public ResultEntity downReportByZip(HttpServletResponse response, String userId, String module, String reportName) { + public ResultEntity downReportByZip(HttpServletResponse response, String userId, String module, String + reportName) { StuPracticalTrainingReportExample example = new StuPracticalTrainingReportExample(); StuPracticalTrainingReportExample.Criteria criteria = example.createCriteria() .andModuleEqualTo(module) @@ -748,7 +817,8 @@ public class StuConceptBlockServiceImpl implements StuConceptBlockService { return null; } - private StuPracticalTrainingReport uploadByReport(MultipartFile file, StuBlockProductWithDTO stuBlockProductWithBLOBs) { + private StuPracticalTrainingReport uploadByReport(MultipartFile file, StuBlockProductWithDTO + stuBlockProductWithBLOBs) { StuPracticalTrainingReport stuPracticalTrainingReport = new StuPracticalTrainingReport(); String upload = fileUtil.upload(file); @@ -768,7 +838,7 @@ public class StuConceptBlockServiceImpl implements StuConceptBlockService { } - public File convertMultipartFileToFile(MultipartFile multipartFile) throws IOException { + public static File convertMultipartFileToFile(MultipartFile multipartFile) throws IOException { // 创建一个临时文件 File tempFile = File.createTempFile("temp", null); diff --git a/src/main/java/com/sztzjy/marketing/util/convertPDF/PDFConvertUtil.java b/src/main/java/com/sztzjy/marketing/util/convertPDF/PDFConvertUtil.java new file mode 100644 index 0000000..5e64927 --- /dev/null +++ b/src/main/java/com/sztzjy/marketing/util/convertPDF/PDFConvertUtil.java @@ -0,0 +1,538 @@ +package com.sztzjy.marketing.util.convertPDF; + +import com.aspose.words.FontSettings; +import com.aspose.words.License; +import com.aspose.words.SaveFormat; +import com.itextpdf.text.Document; +import com.itextpdf.text.Image; +import com.itextpdf.text.PageSize; +import com.itextpdf.text.pdf.PdfPCell; +import com.itextpdf.text.pdf.PdfPTable; +import com.itextpdf.text.pdf.PdfWriter; +import org.apache.poi.hslf.usermodel.*; +import org.apache.poi.xslf.usermodel.*; + +import java.awt.*; +import java.awt.image.BufferedImage; +import java.io.*; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static com.sztzjy.marketing.util.convertPDF.PDFConvertUtil.TypeEnum.*; + + +/** + * 功能描述:
+ * PDF 转换工具 + * + * @Author: lx + * @Date: 2022/12/13 16:26 + */ +public final class PDFConvertUtil { + + final private static Map> types = new HashMap<>(); + + public static enum TypeEnum { + WORD, EXCEL, PPT, PPTX, IMG, OTHER + } + + static { + types.put(WORD, Arrays.asList("DOC", "DOCX")); + types.put(EXCEL, Arrays.asList("XLS", "XLSX")); + types.put(PPT, Arrays.asList("PPT")); + types.put(PPTX, Arrays.asList("PPTX")); +// types.put(IMG, Arrays.asList("JPEG", "JPG", "PNG")); + types.put(OTHER, Arrays.asList("OOXML", "RTF HTML", "OpenDocument", "EPUB", "XPS", "SWF")); + + } + + /** + * @param inputStream 源文件输入流 + * @param outputStream pdf文件输出流 + **/ + public static boolean imgToPdf(InputStream inputStream, OutputStream outputStream) { + + Document document = null; PdfWriter pdfWriter = null; + try { + //此处处理乱码和小方块 + //如果在本地运行,此处报错,请注释这个这是字体,主要是为了解决linux环境下面运行jar时找不到中文字体的问题 + //指定文件库内容路径 + FontSettings.getDefaultInstance().setFontsFolders( + new String[] {"/usr/share/fonts", "/usr/share/fonts/chinese"} + , true); + + // 创建文档,设置PDF页面的大小 A2-A9, 个人觉得A3最合适 + document = new Document(PageSize.A6, 0, 0, 0, 0); + + // 新建pdf文档,具体逻辑看.getInstance方法 + pdfWriter = PdfWriter.getInstance(document, outputStream); + + document.open(); + document.newPage(); + + // 将文件流转换为字节流,便于格式转换 + BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream); + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + byte[] bytes = new byte[1024]; + int length = 0 ; + while (-1 != (length = bufferedInputStream.read(bytes))) { + byteArrayOutputStream.write(bytes, 0, length); + } + + // 处理img图片 + Image image = Image.getInstance(byteArrayOutputStream.toByteArray()); + + float height = image.getHeight(); + float width = image.getWidth(); + + float percent = 0.0f; + // 设置像素或者长宽高,将会影响图片的清晰度,因为只是对图片放大或缩小 + if (height > width) { + // A4 - A9 + percent = PageSize.A6.getHeight() / height * 100; + } else { + percent = PageSize.A6.getWidth() / width * 100; + } + + image.setAlignment(Image.MIDDLE); + image.scalePercent(percent); + + // 将图片放入文档中,完成pdf转换 + document.add(image); + System.out.println("image转换完毕"); + } catch (Exception e) { + e.printStackTrace(); + return false; + } finally { + close(document, pdfWriter); + closeOutput(outputStream); + closeInput(inputStream); + } + + return true; + } + + /** + * @param inputStream 源文件输入流 + * @param outputStream pdf文件输出流 + **/ + public static boolean wordToPdfByAspose(InputStream inputStream, OutputStream outputStream) { + + // 验证License 若不验证则转化出的pdf文档会有水印产生 + if (!getLicense()) { + return false; + } + try { + //此处处理乱码和小方块 + //如果在本地运行,此处报错,请注释这个这是字体,主要是为了解决linux环境下面运行jar时找不到中文字体的问题 + //指定文件库内容路径 + FontSettings.getDefaultInstance().setFontsFolders( + new String[] {"/usr/share/fonts", "/usr/share/fonts/win"} + , true); + // 将源文件保存在com.aspose.words.Document中,具体的转换格式依靠里面的save方法 + com.aspose.words.Document doc = new com.aspose.words.Document(inputStream); + + // 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,EPUB, XPS, SWF 相互转换 + doc.save(outputStream, SaveFormat.PDF); + + System.out.println("word转换完毕"); + } catch (Exception e) { + e.printStackTrace(); + return false; + }finally { + closeOutput(outputStream); + closeInput(inputStream); + } + return true; + } + + /** + * 关闭输出流 + * @param outputStream + */ + private static void closeOutput(OutputStream outputStream) { + if (outputStream != null) { + try { + outputStream.flush(); + outputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + /** + * 关闭输出流 + * @param inputStream + */ + private static void closeInput(InputStream inputStream) { + if (inputStream != null) { + try { + inputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + private static void close(Document document, PdfWriter pdfWriter) { + if (document != null) { + document.close(); + } + if (pdfWriter != null) { + pdfWriter.close(); + } + } + + // 官方文档的要求 无需理会 + public static boolean getLicense() { + boolean result = false; + try { + String s = "\n" + + " \n" + + " \n" + + " Aspose.Total for Java\n" + + " Aspose.Words for Java\n" + + " \n" + + " Enterprise\n" + + " 20991231\n" + + " 20991231\n" + + " 8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7\n" + + " \n" + + " sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=\n" + + ""; + ByteArrayInputStream is = new ByteArrayInputStream(s.getBytes()); + License aposeLic = new License(); + aposeLic.setLicense(is); + result = true; + } catch (Exception e) { + e.printStackTrace(); + } + return result; + } + /** + * @param inputStream 源文件输入流 + * @param outputStream pdf文件输出流 + **/ + public static boolean excelToPdf(InputStream inputStream, OutputStream outputStream) { + // 验证License 若不验证则转化出的pdf文档会有水印产生 + if (!getExcelLicense()) { + return false; + } + try { + //此处处理乱码和小方块 + //如果在本地运行,此处报错,请注释这个这是字体,主要是为了解决linux环境下面运行jar时找不到中文字体的问题 + //指定文件库内容路径 + FontSettings.getDefaultInstance().setFontsFolders( + new String[] {"/usr/share/fonts", "/usr/share/fonts/win"} + , true); + // 原始excel路径 + com.aspose.cells.Workbook wb = new com.aspose.cells.Workbook(inputStream); + + com.aspose.cells.PdfSaveOptions pdfSaveOptions = new com.aspose.cells.PdfSaveOptions(); + pdfSaveOptions.setOnePagePerSheet(false); + + int[] autoDrawSheets={3}; + //当excel中对应的sheet页宽度太大时,在PDF中会拆断并分页。此处等比缩放。 + autoDraw(wb,autoDrawSheets); + + int[] showSheets={0}; + //隐藏workbook中不需要的sheet页。 + printSheetPage(wb,showSheets); + wb.save(outputStream, pdfSaveOptions); + System.out.println("excel转换完毕"); + } catch (IOException e) { + e.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); + }finally { + closeOutput(outputStream); + closeInput(inputStream); + } + return true; + } + + + /** + * 设置打印的sheet 自动拉伸比例 + * @param wb + * @param page 自动拉伸的页的sheet数组 + */ + public static void autoDraw(com.aspose.cells.Workbook wb,int[] page){ + if(null!=page&&page.length>0){ + for (int i = 0; i < page.length; i++) { + wb.getWorksheets().get(i).getHorizontalPageBreaks().clear(); + wb.getWorksheets().get(i).getVerticalPageBreaks().clear(); + } + } + } + + /** + * 隐藏workbook中不需要的sheet页。 + * + * @param wb + * @param page 显示页的sheet数组 + */ + public static void printSheetPage(com.aspose.cells.Workbook wb, int[] page) { + for (int i = 1; i < wb.getWorksheets().getCount(); i++) { + wb.getWorksheets().get(i).setVisible(false); + } + if (null == page || page.length == 0) { + wb.getWorksheets().get(0).setVisible(true); + } else { + for (int i = 0; i < page.length; i++) { + wb.getWorksheets().get(i).setVisible(true); + } + } + } + + public static boolean getExcelLicense() { + boolean result = false; + try { + String s = "\n" + + " \n" + + " \n" + + " Aspose.Cells for Java\n" + + " \n" + + " Enterprise\n" + + " 29991231\n" + + " 29991231\n" + + " evilrule\n" + + " \n" + + " evilrule\n" + + ""; + ByteArrayInputStream is = new ByteArrayInputStream(s.getBytes()); + com.aspose.cells.License aposeLic = new com.aspose.cells.License(); + aposeLic.setLicense(is); + result = true; + } catch (Exception e) { + e.printStackTrace(); + } + return result; + } + + /** + * pptToPdf + * @param inputStream + * @param outputStream + * @return + */ + public static boolean pptToPdf(InputStream inputStream, OutputStream outputStream) { + + Document document = null; + HSLFSlideShow hslfSlideShow = null; + PdfWriter pdfWriter = null; + + try { + //此处处理乱码和小方块 + //如果在本地运行,此处报错,请注释这个这是字体,主要是为了解决linux环境下面运行jar时找不到中文字体的问题 + //指定文件库内容路径 + FontSettings.getDefaultInstance().setFontsFolders( + new String[] {"/usr/share/fonts", "/usr/share/fonts/win"} + , true); + hslfSlideShow = new HSLFSlideShow(inputStream); + + // 获取ppt文件页面 + Dimension dimension = hslfSlideShow.getPageSize(); + + document = new Document(); + + // pdfWriter实例 + pdfWriter = PdfWriter.getInstance(document, outputStream); + + document.open(); + + PdfPTable pdfPTable = new PdfPTable(1); + + List hslfSlideList = hslfSlideShow.getSlides(); + + for (int i=0; i < hslfSlideList.size(); i++) { + HSLFSlide hslfSlide = hslfSlideList.get(i); + // 设置字体, 解决中文乱码 + for (HSLFShape shape : hslfSlide.getShapes()) { + if (shape instanceof HSLFTextShape) { + HSLFTextShape textShape = (HSLFTextShape) shape; + + for (HSLFTextParagraph textParagraph : textShape.getTextParagraphs()) { + for (HSLFTextRun textRun : textParagraph.getTextRuns()) { + textRun.setFontFamily("宋体"); + } + } + } + } + BufferedImage bufferedImage = new BufferedImage((int)dimension.getWidth(), (int)dimension.getHeight(), BufferedImage.TYPE_INT_RGB); + + Graphics2D graphics2d = bufferedImage.createGraphics(); + + graphics2d.setPaint(Color.white); + graphics2d.setFont(new Font("宋体", Font.PLAIN, 12)); + + hslfSlide.draw(graphics2d); + + graphics2d.dispose(); + + Image image = Image.getInstance(bufferedImage, null); + image.scalePercent(56f); + + // 写入单元格 + pdfPTable.addCell(new PdfPCell(image, true)); + document.add(image); + + } + } catch (Exception e) { + e.printStackTrace(); + return false; + } finally { + close(document, pdfWriter); + closeOutput(outputStream); + closeInput(inputStream); + } + System.out.println("ppt转换完毕"); + return true; + } + + /** + * pptxToPdf + * @param inputStream + * @param outputStream + * @return + */ + public static boolean pptxToPdf(InputStream inputStream, OutputStream outputStream) { + + Document document = null; + + XMLSlideShow slideShow = null; + + PdfWriter pdfWriter = null; + + try { + //此处处理乱码和小方块 + //如果在本地运行,此处报错,请注释这个这是字体,主要是为了解决linux环境下面运行jar时找不到中文字体的问题 + //指定文件库内容路径 + FontSettings.getDefaultInstance().setFontsFolders( + new String[] {"/usr/share/fonts", "/usr/share/fonts/win"} + , true); + + slideShow = new XMLSlideShow(inputStream); + + Dimension dimension = slideShow.getPageSize(); + + document = new Document(); + + pdfWriter = PdfWriter.getInstance(document, outputStream); + + document.open(); + + PdfPTable pdfPTable = new PdfPTable(1); + + List slideList = slideShow.getSlides(); + + for (int i = 0, row = slideList.size(); i < row; i++) { + + XSLFSlide slide = slideList.get(i); + + // 设置字体, 解决中文乱码 + for (XSLFShape shape : slide.getShapes()) { + if (shape instanceof XSLFTextShape) { + XSLFTextShape textShape = (XSLFTextShape) shape; + + for (XSLFTextParagraph textParagraph : textShape.getTextParagraphs()) { + for (XSLFTextRun textRun : textParagraph.getTextRuns()) { + textRun.setFontFamily("宋体"); + } + } + } + } + + BufferedImage bufferedImage = new BufferedImage((int)dimension.getWidth(), (int)dimension.getHeight(), BufferedImage.TYPE_INT_RGB); + + Graphics2D graphics2d = bufferedImage.createGraphics(); + + graphics2d.setPaint(Color.white); + graphics2d.setFont(new Font("宋体", Font.PLAIN, 12)); + + slide.draw(graphics2d); + + graphics2d.dispose(); + + Image image = Image.getInstance(bufferedImage, null); + //image.scalePercent(50f); + image.scalePercent(56f); + + // 写入单元格 + pdfPTable.addCell(new PdfPCell(image, true)); + document.add(image); + } + } catch (Exception e) { + e.printStackTrace(); + return false; + } finally { + close(document, pdfWriter); + closeOutput(outputStream); + closeInput(inputStream); + } + System.out.println("pptx转换完毕"); + return true; + } + + /** + * 功能描述:
+ * 通过文件名后缀来进行转换 + * + * @return {@link boolean} + * @Author: lx + * @Date: 2022/12/13 16:37 + */ + public static boolean convertToPDFByFileNameSuffix(String fileName, InputStream inputStream, OutputStream outputStream) { + + String fileSuffix = getFileSuffix(fileName).toUpperCase(); + + if (types.get(WORD).contains(fileSuffix)) { + return wordToPdfByAspose(inputStream, outputStream); + } + if (types.get(EXCEL).contains(fileSuffix)) { + return excelToPdf(inputStream, outputStream); + } + if (types.get(PPT).contains(fileSuffix)) { + return pptToPdf(inputStream, outputStream); + } + if (types.get(PPTX).contains(fileSuffix)) { + return pptxToPdf(inputStream, outputStream); + } +// if (types.get(IMG).contains(fileSuffix)) { +// return imgToPdf(inputStream, outputStream); +// } + if (types.get(OTHER).contains(fileSuffix)) { + return wordToPdfByAspose(inputStream, outputStream); + } + return false; + } + + /** + * 判断是否可以生成PDF文件 + * @param fileName + * @return + */ + public static boolean isCreatePdf(String fileName) { + return types.values().stream().anyMatch(item -> item.contains(getFileSuffix(fileName).toUpperCase())); + } + + /** + * 功能描述:
+ * 取文件名后缀 + * + * @param fileName + * @return {@link String} + * @Author: lx + * @Date: 2022/12/13 21:51 + */ + private static String getFileSuffix(String fileName) { + if (!fileName.contains(".")) { + throw new RuntimeException("非法的文件名,文件名必须要有文件类型!"); + } + String[] split = fileName.split("\\."); + return split[split.length - 1]; + } +} \ No newline at end of file diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index 4c4fdbb..1b9df88 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -21,8 +21,8 @@ spring: file: type: local path: /usr/local/tianzeProject/digitalMarketing/uploadFile -# path: D:\home -# path: /usr/local/tianzeProject/blockFinance/uploadFile + #path: D:\home + bigModule: accessKey: 4zXteUiZO56bkIXfcIypUVsS