SpringBoot實現(xiàn)PPT格式文件上傳并在線預覽功能
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>
<!--其他格式轉換為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文件,上傳成功會返回文件的絕對路徑地址以及文件大小,絕對地址將作為在線預覽文件接口的參數(shù)。
3、在線預覽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);
//轉換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內容繪制到img上
slides.get(i).draw(graphics);
//圖片將要存放的路徑
String absolutePath = basePath + File.separator+ (i + 1) + ".jpg";
File jpegFile = new File(absolutePath);
if (!jpegFile.exists()) {
// 判斷如果圖片存在則不再重復創(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();
}
//將圖片列表轉換為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文件讀取每一頁幻燈片,將幻燈片轉換為圖片格式,最后將所有圖片放到一個pdf文件中形成一個pdf文件用于在線預覽。預覽時會在同級目錄下創(chuàng)建一個相同文件名后綴為pdf的文件,每次預覽會先查找文件是否存在,存在則直接預覽,不存在則會走上面的處理。
4、預覽效果

到此這篇關于SpringBoot實現(xiàn)PPT格式文件上傳并在線預覽的文章就介紹到這了,更多相關SpringBoot PPT格式文件上傳內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot集成Prometheus實現(xiàn)監(jiān)控的過程
這篇文章主要介紹了SpringBoot集成Prometheus實現(xiàn)監(jiān)控,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-09-09
Spring Cloud EureKa Ribbon 服務注冊發(fā)現(xiàn)與調用
這篇文章主要介紹了Spring Cloud EureKa Ribbon 服務注冊發(fā)現(xiàn)與調用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02
詳解SpringCloud Ribbon 負載均衡通過服務器名無法連接的神坑
這篇文章主要介紹了詳解SpringCloud Ribbon 負載均衡通過服務器名無法連接的神坑,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-06-06
Springboot編寫CRUD時訪問對應數(shù)據(jù)函數(shù)返回null的問題及解決方法
我在學習springboot,其中在編寫CRUD時發(fā)現(xiàn)訪問數(shù)據(jù)的函數(shù)執(zhí)行下去返回值是null但是其它部分正常,這篇文章主要介紹了Springboot在編寫CRUD時,訪問對應數(shù)據(jù)函數(shù)返回null,需要的朋友可以參考下2024-02-02

