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”;
通過(guò)上傳接口,可在C盤的tempfile目錄下找到上傳的文件,首先我們先上傳一個(gè)PPT文件,上傳成功會(huì)返回文件的絕對(duì)路徑地址以及文件大小,絕對(duì)地址將作為在線預(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)建,建議將圖片存放到一個(gè)特定目錄,后面會(huì)統(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格式文件并存儲(chǔ)
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();
}
// 刪除臨時(shí)生成的png圖片
for (File pngFile : pngFiles) {
try {
FileUtils.delete(pngFile);
} catch (IOException e) {
e.printStackTrace();
}
}
long endTime = System.currentTimeMillis();
System.out.println("png2Pdf耗時(shí):" + (endTime - startTime));
}
return null;
}
核心思路:將PPT文件讀取每一頁(yè)幻燈片,將幻燈片轉(zhuǎn)換為圖片格式,最后將所有圖片放到一個(gè)pdf文件中形成一個(gè)pdf文件用于在線預(yù)覽。預(yù)覽時(shí)會(huì)在同級(jí)目錄下創(chuàng)建一個(gè)相同文件名后綴為pdf的文件,每次預(yù)覽會(huì)先查找文件是否存在,存在則直接預(yù)覽,不存在則會(huì)走上面的處理。
4、預(yù)覽效果

到此這篇關(guān)于SpringBoot實(shí)現(xiàn)PPT格式文件上傳并在線預(yù)覽的文章就介紹到這了,更多相關(guān)SpringBoot PPT格式文件上傳內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Springboot 整合 Java DL4J 打造文本摘要生成系統(tǒng)
本文介紹了如何使用SpringBoot整合JavaDeeplearning4j構(gòu)建文本摘要生成系統(tǒng),該系統(tǒng)能夠自動(dòng)從長(zhǎng)篇文本中提取關(guān)鍵信息,生成簡(jiǎn)潔的摘要,幫助用戶快速了解文本的主要內(nèi)容,技術(shù)實(shí)現(xiàn)包括使用LSTM神經(jīng)網(wǎng)絡(luò)進(jìn)行模型構(gòu)建和訓(xùn)練,并通過(guò)SpringBoot集成RESTfulAPI接口2024-11-11
Java Scanner的使用和hasNextXXX()的用法說(shuō)明
這篇文章主要介紹了Java Scanner的使用和hasNextXXX()的用法說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
OpenFeign無(wú)法遠(yuǎn)程調(diào)用問題及解決
文章介紹了在使用Feign客戶端時(shí)遇到的讀超時(shí)問題,并分析了原因是系統(tǒng)啟動(dòng)時(shí)未先加載Nacos配置,為了解決這個(gè)問題,建議將Nacos配置放在`bootstrap.yml`文件中,以便項(xiàng)目啟動(dòng)時(shí)優(yōu)先加載Nacos配置2024-11-11
淺談SpringBoot之開啟數(shù)據(jù)庫(kù)遷移的FlyWay使用
這篇文章主要介紹了淺談SpringBoot之開啟數(shù)據(jù)庫(kù)遷移的FlyWay使用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
解決spring結(jié)合mybatis時(shí)一級(jí)緩存失效的問題
這篇文章主要介紹了解決spring結(jié)合mybatis時(shí)一級(jí)緩存失效的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11
Java實(shí)現(xiàn)鏈表中元素的獲取、查詢和修改方法詳解
這篇文章主要介紹了Java實(shí)現(xiàn)鏈表中元素的獲取、查詢和修改方法,結(jié)合實(shí)例形式詳細(xì)分析了Java針對(duì)鏈表中元素的獲取、查詢和修改相關(guān)原理、實(shí)現(xiàn)方法及操作注意事項(xiàng),需要的朋友可以參考下2020-03-03
springMVC幾種頁(yè)面跳轉(zhuǎn)方式小結(jié)
本篇文章主要介紹了springMVC 幾種頁(yè)面跳轉(zhuǎn)方式,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02

