From 63ca605019e0ef2e77b7b5f77f2e2ae40b13a8a4 Mon Sep 17 00:00:00 2001 From: limqhz <540344226@qq.com> Date: Sat, 4 Mar 2023 10:03:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=88=E5=B9=B6=E5=88=86=E6=94=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/pom.xml | 12 ++++- .../main/java/com/qn/utils/ExcelUtils.java | 44 +++++++++++++++++++ .../main/java/com/qn/utils/UUIDGenerator.java | 12 +++-- pom.xml | 6 +++ web/pom.xml | 5 +++ .../com/qn/controller/TestController.java | 9 ++-- .../com/qn/controller/utils/PDFUtils.java | 43 ++++++++++++++++++ 7 files changed, 123 insertions(+), 8 deletions(-) create mode 100644 common/src/main/java/com/qn/utils/ExcelUtils.java create mode 100644 web/src/main/java/com/qn/controller/utils/PDFUtils.java diff --git a/common/pom.xml b/common/pom.xml index 84d4dd5..a4f7023 100644 --- a/common/pom.xml +++ b/common/pom.xml @@ -23,7 +23,6 @@ 1.2 - org.codehaus.jackson jackson-xc 1.9.12 @@ -33,6 +32,17 @@ slf4j-api 1.7.25 + + + org.apache.poi + poi + 4.1.2 + + + org.apache.poi + poi-ooxml + 4.1.2 + diff --git a/common/src/main/java/com/qn/utils/ExcelUtils.java b/common/src/main/java/com/qn/utils/ExcelUtils.java new file mode 100644 index 0000000..ee59b2a --- /dev/null +++ b/common/src/main/java/com/qn/utils/ExcelUtils.java @@ -0,0 +1,44 @@ +package com.qn.utils; +import org.apache.poi.xssf.usermodel.XSSFCell; +import org.apache.poi.xssf.usermodel.XSSFSheet; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; + +import java.io.*; + +public class ExcelUtils { + private static final String OSS_STR = "http://smartvenue.oss-cn-beijing.aliyuncs.com/health-docs/"; + /** + * + * @param args + * @throws IOException + */ + public static void main(String[] args) throws IOException { + FileInputStream fis = new FileInputStream("/Users/limqhz/home/123.xlsx"); + XSSFWorkbook workBook = new XSSFWorkbook(fis); + + // 进行模板的克隆(接下来的操作都是针对克隆后的sheet) + XSSFSheet sheet = workBook.getSheetAt(0); + int lastRowNum = sheet.getLastRowNum(); + System.out.println("last row Num ===" + lastRowNum); + for (int i=1;i<=lastRowNum;i++){ + System.out.println(i); + String s = UUIDGenerator.randomUUID(); + XSSFCell c3 = sheet.getRow(i).getCell(3); + replaceCellValue(c3,OSS_STR + s + ".pdf"); + XSSFCell c4 = sheet.getRow(i).getCell(4); + replaceCellValue(c4, s); + } + // 输出为一个新的Excel,也就是动态修改完之后的excel + String fileName = "output" + System.currentTimeMillis() + ".xlsx"; + OutputStream out = new FileOutputStream("/Users/limqhz/home/" + fileName); + workBook.write(out); + fis.close(); + out.flush(); + out.close(); + } + + private static void replaceCellValue(XSSFCell cell, String value) { + cell.setCellValue(value); + } + +} diff --git a/common/src/main/java/com/qn/utils/UUIDGenerator.java b/common/src/main/java/com/qn/utils/UUIDGenerator.java index ee5eb3b..c9e09f9 100644 --- a/common/src/main/java/com/qn/utils/UUIDGenerator.java +++ b/common/src/main/java/com/qn/utils/UUIDGenerator.java @@ -18,10 +18,10 @@ public class UUIDGenerator { String result=""; UUID uuid = UUID.randomUUID(); String temp=uuid.toString(); - StringTokenizer token=new StringTokenizer(temp,"-"); + StringTokenizer token=new StringTokenizer(temp,"-"); while(token.hasMoreTokens()){ - result+=token.nextToken(); - } + result+=token.nextToken(); + } return result; } @@ -36,6 +36,10 @@ public class UUIDGenerator { result = result.substring(16); return result; } - + + public static void main(String[] args) { + System.out.println(UUIDGenerator.randomUUID()); + } + } diff --git a/pom.xml b/pom.xml index 27bdde7..0fd4b77 100644 --- a/pom.xml +++ b/pom.xml @@ -48,6 +48,12 @@ 1.18.20 + + org.apache.pdfbox + pdfbox + 2.0.20 + + diff --git a/web/pom.xml b/web/pom.xml index a960b20..87fccc9 100644 --- a/web/pom.xml +++ b/web/pom.xml @@ -46,6 +46,11 @@ provided + + org.apache.pdfbox + pdfbox + + diff --git a/web/src/main/java/com/qn/controller/TestController.java b/web/src/main/java/com/qn/controller/TestController.java index 8518bb4..7be491a 100644 --- a/web/src/main/java/com/qn/controller/TestController.java +++ b/web/src/main/java/com/qn/controller/TestController.java @@ -1,5 +1,6 @@ package com.qn.controller; +import org.apache.http.HttpResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; @@ -8,6 +9,7 @@ import org.springframework.web.bind.annotation.RequestMethod; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.IOException; +import java.io.PrintWriter; @Controller public class TestController { @@ -34,9 +36,10 @@ public class TestController { /** * 表白 */ - @RequestMapping(value = "/image", method = RequestMethod.GET) - public void image() throws IOException { - File file = new File("/home/type/job"); + @RequestMapping(value = "/pdf", method = RequestMethod.GET) + public void image(HttpServletResponse response) throws IOException { + PrintWriter writer = response.getWriter(); + writer.write("如需下载请先付款!"); } } diff --git a/web/src/main/java/com/qn/controller/utils/PDFUtils.java b/web/src/main/java/com/qn/controller/utils/PDFUtils.java new file mode 100644 index 0000000..1f8e776 --- /dev/null +++ b/web/src/main/java/com/qn/controller/utils/PDFUtils.java @@ -0,0 +1,43 @@ +package com.qn.controller.utils; + +import org.apache.pdfbox.pdmodel.PDDocument; +import org.apache.pdfbox.rendering.PDFRenderer; + +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; + +public class PDFUtils { + + private static final float DPI = 100; + + private static final String IMG_TYPE = "PNG"; + + public static void pdfToImg(String filePath) throws IOException { + File file = new File(filePath); + if (file.exists()){ + PDDocument document = PDDocument.load(file); + PDFRenderer renderer = new PDFRenderer(document); + int numberOfPages = document.getNumberOfPages(); + for (int i = 0; i < numberOfPages; i++){ + BufferedImage bufferedImage = renderer.renderImageWithDPI(i, DPI); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ImageIO.write(bufferedImage, IMG_TYPE, out); + File imgFile = new File("/Users/limqhz/home/test/pdf/img/" + i + ".png"); + FileOutputStream outputStream = new FileOutputStream(imgFile); + outputStream.write(out.toByteArray()); + outputStream.close(); + out.close(); + } + } + } + + public static void main(String[] args) throws IOException { + PDFUtils.pdfToImg("/Users/limqhz/Downloads/1.pdf"); + } + + +}