SpringBoot實(shí)現(xiàn)PPT格式文件上傳并在線預(yù)覽功能
1、需要引入依賴
<dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.9</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.15</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.15</version> </dependency> <!--其他格式轉(zhuǎn)換為PDF --> <dependency> <groupId>fr.opensagres.xdocreport</groupId> <artifactId>xdocreport</artifactId> <version>1.0.6</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.11.0</version> </dependency>
2、上傳文件到本地文件夾中
@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public ResponseEntity<Object> uploadFileToLocal(@RequestParam("multipartFile") MultipartFile multipartFile) { if (multipartFile == null) { return ResponseEntity.status(HttpStatus.NO_CONTENT).build(); } File file = null; try { File dir = new File(basePath); if (!dir.exists()) { dir.mkdir(); } file = new File(basePath + File.separator + multipartFile.getOriginalFilename()); if (!file.exists()) { multipartFile.transferTo(file); } } catch (IOException e) { e.printStackTrace(); } return ResponseEntity.ok(FileVo.builder().size(multipartFile.getSize()).path(file.getAbsolutePath()).build()); }
basePath為定義的常量: private static final String basePath = “C:\tempFile”;
通過上傳接口,可在C盤的tempfile目錄下找到上傳的文件,首先我們先上傳一個PPT文件,上傳成功會返回文件的絕對路徑地址以及文件大小,絕對地址將作為在線預(yù)覽文件接口的參數(shù)。
3、在線預(yù)覽PPT文件
@GetMapping("/showPPT") public void showPPT(@RequestParam("path") String path,HttpServletResponse response) throws IOException { byte[] buffer = new byte[1024 * 4]; String type = path.substring(path.lastIndexOf(".") + 1); //轉(zhuǎn)換pdf文件,如存在則直接顯示pdf文件 String pdf = path.replace(type, "pdf"); File pdfFile = new File(pdf); if (pdfFile.exists()) { outFile(buffer, pdfFile, response); } else { FileInputStream in = new FileInputStream(path); ZipSecureFile.setMinInflateRatio(-1.0d); XMLSlideShow xmlSlideShow = new XMLSlideShow(in); in.close(); // 獲取大小 Dimension pgsize = xmlSlideShow.getPageSize(); // 獲取幻燈片 List<XSLFSlide> slides = xmlSlideShow.getSlides(); List<File> imageList = new ArrayList<>(); for (int i = 0; i < slides.size(); i++) { // 解決亂碼問題 List<XSLFShape> shapes = slides.get(i).getShapes(); for (XSLFShape shape : shapes) { if (shape instanceof XSLFTextShape) { XSLFTextShape sh = (XSLFTextShape) shape; List<XSLFTextParagraph> textParagraphs = sh.getTextParagraphs(); for (XSLFTextParagraph xslfTextParagraph : textParagraphs) { List<XSLFTextRun> textRuns = xslfTextParagraph.getTextRuns(); for (XSLFTextRun xslfTextRun : textRuns) { xslfTextRun.setFontFamily("宋體"); } } } } //根據(jù)幻燈片大小生成圖片 BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB); Graphics2D graphics = img.createGraphics(); graphics.setPaint(Color.white); graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height)); // 將PPT內(nèi)容繪制到img上 slides.get(i).draw(graphics); //圖片將要存放的路徑 String absolutePath = basePath + File.separator+ (i + 1) + ".jpg"; File jpegFile = new File(absolutePath); if (!jpegFile.exists()) { // 判斷如果圖片存在則不再重復(fù)創(chuàng)建,建議將圖片存放到一個特定目錄,后面會統(tǒng)一刪除 FileOutputStream fileOutputStream = new FileOutputStream(jpegFile); ImageIO.write(img, "jpg", fileOutputStream); } // 圖片路徑存放 imageList.add(jpegFile); } File file = png2Pdf(imageList, pdf); outFile(buffer, file, response); } } private void outFile(byte[] buffer, File pdfFile, HttpServletResponse response) throws IOException { ByteArrayOutputStream out; int n = 0; FileInputStream fileInputStream = new FileInputStream(pdfFile); out = new ByteArrayOutputStream(); ServletOutputStream outputStream = response.getOutputStream(); while ((n = fileInputStream.read(buffer)) != -1) { out.write(buffer, 0, n); } outputStream.write(out.toByteArray()); outputStream.flush(); } //將圖片列表轉(zhuǎn)換為PDF格式文件并存儲 public File png2Pdf(List<File> pngFiles, String pdfFilePath) { Document document = new Document(); File pdfFile = null; long startTime = System.currentTimeMillis(); try { pdfFile = new File(pdfFilePath); if (pdfFile.exists()) { return pdfFile; } PdfWriter.getInstance(document, new FileOutputStream(pdfFile)); document.open(); pngFiles.forEach(pngFile -> { try { Image png = Image.getInstance(pngFile.getCanonicalPath()); png.scalePercent(50); document.add(png); } catch (Exception e) { System.out.println("png2Pdf exception"); } }); document.close(); return pdfFile; } catch (Exception e) { System.out.println(String.format("png2Pdf %s exception", pdfFilePath)); } finally { if (document.isOpen()) { document.close(); } // 刪除臨時生成的png圖片 for (File pngFile : pngFiles) { try { FileUtils.delete(pngFile); } catch (IOException e) { e.printStackTrace(); } } long endTime = System.currentTimeMillis(); System.out.println("png2Pdf耗時:" + (endTime - startTime)); } return null; }
核心思路:將PPT文件讀取每一頁幻燈片,將幻燈片轉(zhuǎn)換為圖片格式,最后將所有圖片放到一個pdf文件中形成一個pdf文件用于在線預(yù)覽。預(yù)覽時會在同級目錄下創(chuàng)建一個相同文件名后綴為pdf的文件,每次預(yù)覽會先查找文件是否存在,存在則直接預(yù)覽,不存在則會走上面的處理。
4、預(yù)覽效果
到此這篇關(guān)于SpringBoot實(shí)現(xiàn)PPT格式文件上傳并在線預(yù)覽的文章就介紹到這了,更多相關(guān)SpringBoot PPT格式文件上傳內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot集成Prometheus實(shí)現(xiàn)監(jiān)控的過程
這篇文章主要介紹了SpringBoot集成Prometheus實(shí)現(xiàn)監(jiān)控,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-09-09Java 數(shù)據(jù)結(jié)構(gòu)與算法系列精講之紅黑樹
紅黑樹的應(yīng)用比較廣泛,主要是用它來存儲有序的數(shù)據(jù),它的時間復(fù)雜度是O(lgn),效率非常之高。例如,Java集合中的TreeSet和TreeMap,C++ STL中的set、map,以及Linux虛擬內(nèi)存的管理,都是通過紅黑樹去實(shí)現(xiàn)的2022-02-02Java的stream流多個字段排序的實(shí)現(xiàn)
本文主要介紹了Java的stream流多個字段排序的實(shí)現(xiàn),主要是兩種方法,第一種是固定多個字段排序和第二種動態(tài)字段進(jìn)行排序,具有一定的參考價值,感興趣的可以了解一下2023-10-10Spring Cloud EureKa Ribbon 服務(wù)注冊發(fā)現(xiàn)與調(diào)用
這篇文章主要介紹了Spring Cloud EureKa Ribbon 服務(wù)注冊發(fā)現(xiàn)與調(diào)用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02JAVA中的延遲隊(duì)列DelayQueue應(yīng)用解析
這篇文章主要介紹了JAVA中的延遲隊(duì)列DelayQueue應(yīng)用解析,DelayQueue是一個根據(jù)元素的到期時間來排序的隊(duì)列,而并非是一般的隊(duì)列那樣先進(jìn)先出,最快過期的元素排在隊(duì)首,越晚到期的元素排得越后,需要的朋友可以參考下2023-12-12SpringBoot詳細(xì)講解視圖整合引擎thymeleaf
這篇文章主要分享了Spring Boot整合使用Thymeleaf,Thymeleaf是新一代的Java模板引擎,類似于Velocity、FreeMarker等傳統(tǒng)引擎,關(guān)于其更多相關(guān)內(nèi)容,需要的小伙伴可以參考一下2022-06-06詳解SpringCloud Ribbon 負(fù)載均衡通過服務(wù)器名無法連接的神坑
這篇文章主要介紹了詳解SpringCloud Ribbon 負(fù)載均衡通過服務(wù)器名無法連接的神坑,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-06-06Springboot編寫CRUD時訪問對應(yīng)數(shù)據(jù)函數(shù)返回null的問題及解決方法
我在學(xué)習(xí)springboot,其中在編寫CRUD時發(fā)現(xiàn)訪問數(shù)據(jù)的函數(shù)執(zhí)行下去返回值是null但是其它部分正常,這篇文章主要介紹了Springboot在編寫CRUD時,訪問對應(yīng)數(shù)據(jù)函數(shù)返回null,需要的朋友可以參考下2024-02-02