SpringBoot实现PPT格式文件上传并在线预览功能

网友投稿 1110 2022-10-28

SpringBoot实现PPT格式文件上传并在线预览功能

SpringBoot实现PPT格式文件上传并在线预览功能

1、需要引入依赖

com.itextpdf

itextpdf

5.5.9

org.apache.poi

poi

3.15

org.apache.poi

poi-ooxml

3.15

fr.opensagres.xdocreport

xdocreport

1.0.6

commons-io

commons-io

2.11.0

2、上传文件到本地文件夹中

@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)

public ResponseEntity 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文件,上传成功会返回文件的绝对路径地址以及文件大小,绝对地址将作为在线预览文件接口的参数

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 slides = xmlSlideShow.getSlides();

List imageList = new ArrayList<>();

for (int i = 0; i < slides.size(); i++) {

// 解决乱码问题

List shapes = slides.get(i).getShapes();

for (XSLFShape shape : shapes) {

if (shape instanceof XSLFTextShape) {

XSLFTextShape sh = (XSLFTextShape) shape;

List textParagraphs = sh.getTextParagraphs();

for (XSLFTextParagraph xslfTextParagraph : textParagraphs) {

List textRuns = xslfTextParagraph.getTextRuns();

for (XSLFTextRun xslfTextRun : textRuns) {

xslfTextRun.setFontFamily("宋体");

}

}

}

}

//根据幻灯片大小生成图片

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()) {

// 判断如果图片存在则不再重复创建,建议将图片存放到一个特定目录,后面会统一删除

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 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文件用于在线预览。预览http://时会在同级目录下创建一个相同文件名后缀为pdf的文件,每次预览会先查找文件是否存在,存在则直接预览,不存在则会走上面的处理。

4、预览效果

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:GraphEDM:图机器学习全面分类和统一框架
下一篇:NoHttp:支持多种Http缓存模式Android网络框架
相关文章

 发表评论

暂时没有评论,来抢沙发吧~