Spring Boot文件处理实战:从ZIP解压到图片信息识别
个人名片
🎓作者简介:java领域优质创作者
🌐个人主页:码农阿豪
📞工作室:新空间代码工作室(提供各种软件服务)
💌个人邮箱:[2435024119@qq.com]
📱个人微信:15279484656
🌐个人导航网站:www.forff.top
💡座右铭:总有人要赢。为什么不能是我呢?
- 专栏导航:
码农阿豪系列专栏导航
面试专栏:收集了java相关高频面试题,面试实战总结🍻🎉🖥️
Spring5系列专栏:整理了Spring5重要知识点与实战演练,有案例可直接使用🚀🔧💻
Redis专栏:Redis从零到一学习分享,经验总结,案例实战💐📝💡
全栈系列专栏:海纳百川有容乃大,可能你想要的东西里面都有🤸🌱🚀
目录
Spring Boot文件处理实战:从ZIP解压到图片信息识别
引言
在现代Web应用中,文件上传与处理是常见的需求。本文将通过一个实际案例,详细介绍如何使用Spring Boot构建一个文件处理工具,实现以下功能:
- 接收用户上传的ZIP压缩包
- 解压ZIP文件并提取其中的图片
- 使用OCR技术识别图片中的关键信息
- 将识别结果导出为Excel文件
- 同时保存结果到服务器本地
我们将从项目搭建、核心功能实现到错误处理等方面进行全面讲解,并提供完整的代码示例。
一、项目概述与搭建
1.1 功能需求
- 用户通过网页上传ZIP文件
- 后端解压ZIP,提取图片文件
- 对每张图片进行OCR识别(如快递单号、手机号)
- 将识别结果生成Excel并提供下载
- 在服务器
resources/output目录保留结果副本
1.2 技术栈
- 后端:Spring Boot 2.7+
- 模板引擎:Thymeleaf(前后端不分离)
- 文件处理:Apache Commons Compress(ZIP解压)
- OCR识别:百度OCR API(或其他OCR服务)
- Excel操作:Apache POI
1.3 初始化项目
使用Spring Initializr创建项目,添加依赖:
<dependencies>
<!-- Web支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 模板引擎 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- Excel操作 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.3</version>
</dependency>
<!-- 文件上传 -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
</dependencies>
二、核心功能实现
2.1 文件上传接口
@Controller
public class FileUploadController {
@GetMapping("/")
public String index() {
return "index"; // 对应templates/index.html
}
@PostMapping("/upload")
public ResponseEntity<byte[]> handleFileUpload(
@RequestParam("file") MultipartFile file) {
try {
Workbook workbook = imageProcessService.processZipFile(file);
// 保存到本地
saveToResourcesOutput(workbook);
// 返回Excel给用户
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
workbook.write(outputStream);
workbook.close();
return ResponseEntity.ok()
.header("Content-Disposition", "attachment; filename=result.xlsx")
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.body(outputStream.toByteArray());
} catch (Exception e) {
return ResponseEntity.status(500).body("处理失败".getBytes());
}
}
}
2.2 ZIP解压实现
private List<File> unzipFile(MultipartFile file) throws IOException {
List<File> extractedFiles = new ArrayList<>();
Path tempDir = Files.createTempDirectory("unzip_");
try (ZipInputStream zipIn = new ZipInputStream(file.getInputStream())) {
ZipEntry entry;
while ((entry = zipIn.getNextEntry()) != null) {
Path filePath = tempDir.resolve(entry.getName());
// 防止ZIP滑动攻击
if (!filePath.normalize().startsWith(tempDir)) {
throw new SecurityException("非法文件路径");
}
if (!entry.isDirectory() && isImageFile(entry.getName())) {
Files.copy(zipIn, filePath);
extractedFiles.add(filePath.toFile());
}
zipIn.closeEntry();
}
}
return extractedFiles;
}
private boolean isImageFile(String filename) {
String[] extensions = {".jpg", ".png", ".jpeg"};
return Arrays.stream(extensions).anyMatch(filename::endsWith);
}
2.3 OCR信息识别
public OrderInfo getPicInfo(String imagePath) {
OrderInfo info = new OrderInfo();
try {
String base64Image = imageToBase64(imagePath);
String ocrResult = callOcrApi(base64Image); // 调用OCR API
info.setExpressNumber(extractExpressNo(ocrResult));
info.setPhoneNumber(extractPhoneNo(ocrResult));
} catch (Exception e) {
info.setError(true);
}
return info;
}
2.4 生成Excel并保存
private void saveToResourcesOutput(Workbook workbook) throws IOException {
Path outputDir = Paths.get("src/main/resources/output");
if (!Files.exists(outputDir)) {
Files.createDirectories(outputDir);
}
String filename = "result_" + System.currentTimeMillis() + ".xlsx";
try (FileOutputStream out = new FileOutputStream(outputDir.resolve(filename).toFile())) {
workbook.write(out);
}
}
三、前端页面实现
3.1 Thymeleaf模板(index.html)
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>图片处理工具</title>
</head>
<body>
<h1>上传ZIP压缩包</h1>
<form method="post" action="/upload" enctype="multipart/form-data">
<input type="file" name="file" accept=".zip" required>
<button type="submit">提交</button>
</form>
<div th:if="${message}" th:text="${message}"></div>
</body>
</html>
四、错误处理与优化
4.1 常见错误解决
问题1:Thymeleaf模板找不到
Error resolving template [index], template might not exist
解决方案:
- 确认
index.html位于resources/templates/目录 - 检查是否添加了Thymeleaf依赖
- 确保控制器返回的视图名称匹配
问题2:ZIP解压失败
增强健壮性:
try {
unzipFile(file);
} catch (IOException e) {
log.error("解压失败", e);
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "无效的ZIP文件");
}
4.2 性能优化建议
- 大文件处理:使用
SXSSFWorkbook流式写入Excel - 并发处理:对多图片采用线程池并行OCR识别
- 临时文件清理:
@Scheduled(fixedRate = 86400000) // 每天清理 public void cleanTempFiles() { // 删除超过1天的临时文件 }
五、总结
本文实现了一个完整的Spring Boot文件处理流程,关键点包括:
- 使用
MultipartFile接收上传文件 - 安全的ZIP解压与路径校验
- 第三方OCR服务集成
- 动态Excel生成与双存储(下载+本地保存)
- 全面的异常处理机制
扩展方向:
- 添加用户系统,隔离不同用户的数据
- 支持更多文件格式(如RAR、7z)
- 集成更强大的OCR引擎(如Tesseract)
GitHub示例:完整代码可在 https://github.com/example/file-processor 获取
通过这个案例,读者可以掌握Spring Boot中文件处理的核心技术,并快速应用到实际项目中。


浙公网安备 33010602011771号