|
1 | | -# FileParseUtil |
2 | | -可以将word(doc、docx)、excel、pdf、ppt、caj、txt文件的文本内容提取出来,同时能够提取出word、pdf文件的目录 |
| 1 | +# 开源项目(小工具) fileparse 文件解析工具 |
3 | 2 |
|
4 | | -> 因为需求,需要提取各种文件的文本,以及部分文件类型的目录,我搜索和调试了两天发现网上的案例比较少且乱,所以针对我这个需求,我将这些代码都集成在了 |
5 | | -一个项目里,一方面是方便自己以后使用,另一方面也是希望有类似需求的同学有幸能够从我整合的案例里参考或指教些什么。 |
| 3 | +### 我的联系方式(欢迎交流啊): |
| 4 | +邮箱:deepz2019@163.com |
| 5 | +[博客园]: https://www.cnblogs.com/deepSleeping/ |
6 | 6 |
|
7 | | -> 同时,因为今天是周五(第一次提交),所以匆忙提交的,对于很多异常,代码的冗余等都没有处理。以后有时间会更新的 |
| 7 | +### 为什么编写与总结这个工具 |
| 8 | +因为需求,需要做到对各类文件的解析,并且考虑到未来新增文件类型的拓展问题。所以我编写了这个工具类,途中遇到了很多困难, |
| 9 | +从最开始的博客寥寥无几导致无从下手,到后来会使用关键字去更好地搜索自己想要的东西,最后提升了自己阅读官方文档、阅读官方API、阅读官方example的能力, |
| 10 | +最后东拼西凑可算是完成了1.0版本,写这篇文档也是希望有同样需求的人如果不知道如何使用,可以借鉴我的代码去使用或者给我一些建议或意见。因为当初迷茫寻找各种零散博客时候的我是非常痛苦的,当然还有很多不完善的,而且代码质量也不高,请多多担待。 |
| 11 | + |
| 12 | +### 提示(最新版本) |
| 13 | +最新版本在**分支1.0**里,使用了策略设计模式+反射框架Reflections,更好地处理了if else、文件类型扩展的问题 |
| 14 | + |
| 15 | + |
| 16 | +### 功能一览: |
| 17 | +> 对各种文件可以进行解析,提取出文本内容,针对word、pdf等文件可以提取出目录 |
| 18 | +
|
| 19 | +### 目前所支持的文件类型: |
| 20 | +* .doc .docx .eml .xls .xlsx .ppt .pptx .pdf .txt .json .csv 截止(2019/7/30) |
| 21 | + |
| 22 | +### 所使用的的第三方依赖 |
| 23 | + 1. Apache Tika [教程]: https://www.yiibai.com/tika |
| 24 | +> 用于文件类型检测和从各种格式的文件内容提取到库。(本工具中用它来讲文件内容提取成文本,用于后续针对不同输出要求来解析) |
| 25 | + 2. Apache POI [官方文档]: https://poi.apache.org/components/index.html |
| 26 | +> 提供API给Java程式对Microsoft Office格式档案读和写的功能 最新的稳定版是Version 3.15 (2016-09-19) |
| 27 | + 3. commons-email [官网]: http://commons.apache.org/proper/commons-email/ |
| 28 | + > 处理邮件文件,可以读写,发送等(本工具用它来解析邮件文件,提取出主题、收件人等信息) |
| 29 | + 4. Apache PDFBox [官网]: https://pdfbox.apache.org/ |
| 30 | + > 处理pdf文件的依赖,因为当时在其他API中没有找到或者不会使用,所以就使用了这个,暂时解决功能空缺问题 |
| 31 | + |
| 32 | + |
| 33 | +### 增加新的文件解析类 |
| 34 | +1. 创建对应文件的vo类,根据情况可选择是否继承**StructableFileVo** |
| 35 | +2. 创建对应的parse解析类,并且该解析类必须在**com.deepz.fileparse.parse**包下,否则无法发现。 |
| 36 | +3. 给新建的parse解析类添加注解**@Parse(com.deepz.fileparse.annotation.Parse)** |
| 37 | +4. 给添加的注解设置fileType属性,代表文件类型(如fileType = "doc"、fileType={"xls","xlsx"}) |
| 38 | +5. 编写解析类中的代码即可 |
| 39 | + |
| 40 | +### 核心代码展示(与上文需求相关)2019/7/30 |
| 41 | +```java |
| 42 | +public class FileParser { |
| 43 | + |
| 44 | + /** |
| 45 | + * 文件解析策略 |
| 46 | + */ |
| 47 | + private Parser parser; |
| 48 | + |
| 49 | + /** |
| 50 | + * bean类型容器 key:文件后缀 value 对应解析类实现类 |
| 51 | + */ |
| 52 | + private static Map<String, Object> beanDefinitions = new ConcurrentHashMap<>(); |
| 53 | + |
| 54 | + static { |
| 55 | + Reflections reflections = new Reflections("com.deepz.fileparse.parse", new TypeAnnotationsScanner(), new SubTypesScanner()); |
| 56 | + Set<Class<?>> typeClass = reflections.getTypesAnnotatedWith(com.deepz.fileparse.annotation.Parser.class, true); |
| 57 | + Iterator<Class<?>> iterator = typeClass.iterator(); |
| 58 | + while (iterator.hasNext()) { |
| 59 | + Class<?> clazz = iterator.next(); |
| 60 | + Object parserImpl = null; |
| 61 | + try { |
| 62 | + parserImpl = clazz.getConstructor().newInstance(); |
| 63 | + } catch (Exception e) { |
| 64 | + e.printStackTrace(); |
| 65 | + } |
| 66 | + com.deepz.fileparse.annotation.Parser annotation = clazz.getAnnotation(com.deepz.fileparse.annotation.Parser.class); |
| 67 | + String[] fileTypes = annotation.fileType(); |
| 68 | + for (int i = 0; i < fileTypes.length; i++) { |
| 69 | + beanDefinitions.put(fileTypes[i], parserImpl); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + public <T> T parse(FileDto fileDto) { |
| 76 | + //String suffix = path.substring(path.lastIndexOf('.') + 1, path.length()); |
| 77 | + this.parser = (Parser) beanDefinitions.get(fileDto.getSuffx()); |
| 78 | + |
| 79 | + return (T) parser.parse(fileDto); |
| 80 | + } |
| 81 | + |
| 82 | + public Parser getParser() { |
| 83 | + return parser; |
| 84 | + } |
| 85 | + |
| 86 | + public void setParser(Parser parser) { |
| 87 | + this.parser = parser; |
| 88 | + } |
| 89 | + |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +### 使用 |
| 94 | +> 创建FileParser(com.deepz.fileparse.main),将文件上传后的输入流、文件名后缀(suffix)封装成FileDto对象作为入参,调用parse方法即可 |
0 commit comments