找回密码
 立即注册
首页 业界区 安全 Spring Boot + Apache Tika 实现文档内容解析

Spring Boot + Apache Tika 实现文档内容解析

魄柜 2025-9-26 11:49:11
一、Tika简介
Apache Tika 是由 Apache软件基金会 开发的开源文档处理工具,基于 Java 语言实现,支持自动检测并解析超过1000种文件
格式(如PDF、Office文档、多媒体文件等),提取元数据、结构化文本内容及语言属性,为搜索引擎和内容索引工具提供统一接口。 
核心功能


  • ‌文档类型识别‌:通过文件头字节、文件名扩展及容器格式深度解析多重检测机制确定文件类型。
  • ‌元数据提取‌:获取文件作者、创建时间、修改日期、标题等元数据信息。 ‌
  • ‌文本提取‌:去除HTML标记并提取纯文本内容,支持流式处理大文件以避免内存溢出。 ‌
  • ‌语言检测‌:识别文本使用的语言(如中文、英文、法文)。 ‌
  • ‌媒体元数据提取‌:支持音频/视频文件的分辨率、时长、编码格式等信息的提取。 
二、添加 Apache Tika 依赖
  1. <dependency>
  2.     <groupId>org.apache.tika</groupId>
  3.     tika-core</artifactId>
  4.     <version>1.26</version>
  5. </dependency>
  6. <dependency>
  7.     <groupId>org.apache.tika</groupId>
  8.     tika-parsers</artifactId>
  9.     <version>1.26</version>
  10. </dependency>
复制代码
三、创建文档解析服务
  1. import org.apache.tika.parser.AutoDetectParser;
  2. import org.apache.tika.sax.BodyContentHandler;
  3. import org.springframework.stereotype.Service;
  4. import org.apache.tika.metadata.Metadata;
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.InputStream;
  8. /**
  9. * @ClassName:DocumentParserService
  10. * @Description: 创建文档解析服务
  11. * @Author: songwp
  12. * @Date: 2025/8/5 16:27
  13. */
  14. @Service
  15. public class DocumentParserService {
  16.     /**
  17.      * 解析文档内容
  18.      * @param file
  19.      * @return
  20.      */
  21.     public String parseDocument(File file) {
  22.         StringBuilder content = new StringBuilder();
  23.         try (InputStream stream = new FileInputStream(file)) {
  24.             BodyContentHandler handler = new BodyContentHandler();
  25.             Metadata metadata = new Metadata();
  26.             AutoDetectParser parser = new AutoDetectParser();
  27.             parser.parse(stream, handler, metadata);
  28.             content.append(handler.toString());
  29.         } catch (Exception e) {
  30.             e.printStackTrace();
  31.             content.append("Error: ").append(e.getMessage());
  32.         }
  33.         return content.toString();
  34.     }
  35. }
复制代码
四、创建控制器类
  1. import com.ruoyi.web.controller.tika.DocumentParserService;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.http.ResponseEntity;
  4. import org.springframework.web.bind.annotation.PostMapping;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RequestParam;
  7. import org.springframework.web.bind.annotation.RestController;
  8. import org.springframework.web.multipart.MultipartFile;
  9. import java.io.File;
  10. import java.io.IOException;
  11. /**
  12. * @ClassName:DocumentParserController
  13. * @Description: 解析文档的控制器
  14. * @Author: songwp
  15. * @Date: 2025/8/5 16:29
  16. */
  17. @RestController
  18. @RequestMapping("/api/documents")
  19. public class DocumentParserController {
  20.     @Autowired
  21.     private DocumentParserService documentParserService;
  22.     @PostMapping("/parse")
  23.     public ResponseEntity<String> parseDocument(@RequestParam("file") MultipartFile file) {
  24.         if (file.isEmpty()) {
  25.             return ResponseEntity.badRequest().body("File is empty");
  26.         }
  27.         try {
  28.             // 将上传的文件转换为临时文件
  29.             File tempFile = File.createTempFile("document-", ".tmp");
  30.             file.transferTo(tempFile);
  31.             tempFile.deleteOnExit();
  32.             // 调用文档解析服务解析文档内容
  33.             String parsedContent = documentParserService.parseDocument(tempFile);
  34.             return ResponseEntity.ok(parsedContent);
  35.         } catch (IOException e) {
  36.             e.printStackTrace();
  37.             return ResponseEntity.status(500).body("Error: " + e.getMessage());
  38.         }
  39.     }
  40. }
复制代码
五、配置和运行
  1. 配置 Apache Tika 数据文件
  2. 确保你的项目根目录有一个 tessdata 文件夹,其中包含 Apache Tika 的数据文件。
  3. (1) Apache Tika 官方网站 下载合适的语言数据文件。
  4. (2) 云盘数据下载:https://pan.baidu.com/s/13oPR2r7qOE6lt6SgbpWOQA 提取码: uaaw
复制代码
六、测试与验证
(1)以下是准备的文档内容截图
1.png

 (2) tika文档内容解析如下:
2.png

 七、注意事项
  1. 1、 文件格式支持
  2. 确保上传的文件是 Apache Tika 支持的格式,如 PDF、Word 文档、Excel 表格等。
  3. 2、优化解析性能
  4. 针对大文件和复杂格式的文档,可能需要优化解析性能。可以考虑使用异步处理、文件流处理等技术来提升解析速度和稳定性。
复制代码
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

您需要登录后才可以回帖 登录 | 立即注册