请编写springboot实现文件预览的代码
时间: 2023-04-04 19:01:33 浏览: 171
file-online-preview-master.zipspringboot实现文件预览包含在线预览的
好的,以下是使用 Spring Boot 实现文件预览的代码示例:
1. 首先在 pom.xml 中添加以下依赖:
```
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2..24</version>
</dependency>
```
2. 创建一个 Controller 类,添加以下代码:
```
@Controller
public class FilePreviewController {
@GetMapping("/preview")
public String previewFile(@RequestParam("file") MultipartFile file, Model model) throws IOException {
String fileName = file.getOriginalFilename();
String fileExtension = fileName.substring(fileName.lastIndexOf(".") + 1);
if (fileExtension.equalsIgnoreCase("pdf")) {
PDDocument document = PDDocument.load(file.getInputStream());
PDFRenderer pdfRenderer = new PDFRenderer(document);
BufferedImage image = pdfRenderer.renderImageWithDPI(, 300);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "png", baos);
byte[] bytes = baos.toByteArray();
String base64 = Base64.getEncoder().encodeToString(bytes);
model.addAttribute("fileData", "data:image/png;base64," + base64);
document.close();
return "pdf-preview";
} else if (fileExtension.equalsIgnoreCase("doc") || fileExtension.equalsIgnoreCase("docx")) {
XWPFDocument document = new XWPFDocument(file.getInputStream());
XWPFWordExtractor extractor = new XWPFWordExtractor(document);
String text = extractor.getText();
model.addAttribute("fileData", text);
document.close();
return "text-preview";
} else if (fileExtension.equalsIgnoreCase("xls") || fileExtension.equalsIgnoreCase("xlsx")) {
Workbook workbook = WorkbookFactory.create(file.getInputStream());
Sheet sheet = workbook.getSheetAt();
StringBuilder sb = new StringBuilder();
for (Row row : sheet) {
for (Cell cell : row) {
sb.append(cell.toString()).append("\t");
}
sb.append("\n");
}
model.addAttribute("fileData", sb.toString());
workbook.close();
return "text-preview";
} else {
return "error";
}
}
}
```
3. 创建两个 Thymeleaf 模板文件,分别为 pdf-preview.html 和 text-preview.html,代码如下:
pdf-preview.html:
```
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>PDF Preview</title>
</head>
<body>
<img th:src="${fileData}" alt="PDF Preview"/>
</body>
</html>
```
text-preview.html:
```
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Text Preview</title>
</head>
<body>
<pre th:text="${fileData}"></pre>
</body>
</html>
```
4. 最后,在页面中添加一个表单,用于上传文件并预览,代码如下:
```
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>File Preview</title>
</head>
<body>
<form method="post" action="/preview" enctype="multipart/form-data">
<input type="file" name="file"/>
<button type="submit">Preview</button>
</form>
</body>
</html>
```
以上就是使用 Spring Boot 实现文件预览的代码示例。
阅读全文