• 主页
  • 读书笔记
  • 备案号:滇ICP备19009238号
所有文章 其他站点 关于我

  • 主页
  • 读书笔记
  • 备案号:滇ICP备19009238号

提取txt或word内容

2023-10-29

提取记事本文件或word文件的内容,保留空格、换行等。

依赖

1
2
3
4
5
// 提取Word文本
implementation 'org.apache.poi:poi:5.2.3'
implementation 'org.apache.poi:poi-ooxml:5.2.3'
implementation 'org.apache.poi:poi-scratchpad:5.2.3'
implementation 'org.apache.xmlbeans:xmlbeans:5.1.1'

工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.UUID;

/**
* @author 曳戈泰尔
* @version 1.0
* @description 读文件内容 工具
* @date 2023/8/16 3:29 PM
*/
@Slf4j
public class ReadFileContentUtil {

/**
* 读取文件内容
*
* <p>记事本文件
*
* @param file 文件
* @return 文件内容
*/
public static String readTextFileContent(MultipartFile file) {

try {

// 定义内容字符串Buffer
StringBuilder content = new StringBuilder();

// 转字节流
InputStream inputStream = file.getInputStream();
// 输入流Reader
InputStreamReader inputStreamReader =
new InputStreamReader(inputStream, StandardCharsets.UTF_8);
// Buffer对象
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
// 循环按行读取
while (bufferedReader.ready()) {
String line = bufferedReader.readLine();
// 结尾增加换行符
content.append(line).append("\n");
}

// 关闭流
bufferedReader.close();
inputStreamReader.close();
inputStream.close();

// 返回内容
return content.toString();

} catch (Exception e) {
log.error("读取文件内容失败,原因:{}", e.getMessage(), e);

return null;
}
}

/**
* 读取文件内容
*
* <p>Word文件
*
* @param multipartFile 文件
* @param wordType word类型
* @return 文件内容
*/
public static String readWordFileContent(MultipartFile multipartFile, String wordType) {

// MultipartFile 转 File
File file = multipartFileToFile(multipartFile);
// 文件为空
if (ObjectUtils.isEmpty(file)) {
log.error("文件转换失败,文件为空");

return null;
}

try {
// 提取word文档内容
String content;

// 读取文件流
try (FileInputStream fis = new FileInputStream(file)) {

// doc文件
if (StringUtils.equals(wordType, "doc")) {
HWPFDocument doc = new HWPFDocument(fis);
content = doc.getDocumentText();

// \r 修改为 \n
content = content.replaceAll("\r", "\n");

// 关闭流
doc.close();
}
// docx文件
else {
XWPFDocument document = new XWPFDocument(fis);
XWPFWordExtractor extractor = new XWPFWordExtractor(document);
content = extractor.getText();

// 关闭流
extractor.close();
document.close();
}
}

// 返回内容
return content;

} catch (Exception e) {
log.error("读取文件内容失败,原因:{}", e.getMessage(), e);

return null;
} finally {
// 删除File文件
FileUtils.deleteQuietly(file);
}
}

/**
* MultipartFile 转 File
*
* @param multiFile MultipartFile
* @return File
*/
public static File multipartFileToFile(MultipartFile multiFile) {

// 获取文件名
String fileName = multiFile.getOriginalFilename();
if (StringUtils.isBlank(fileName)) {
return null;
}
// 获取文件后缀
String suffix = fileName.substring(fileName.lastIndexOf("."));
if (StringUtils.isBlank(suffix)) {
return null;
}

// 生成新文件名
String newFileName = UUID.randomUUID().toString().replace("-", "");

try {
File file = File.createTempFile(newFileName, suffix, new File("/tmp"));
multiFile.transferTo(file);

return file;
} catch (Exception e) {
log.error("MultipartFile 转 File 失败,异常:{}", e.getMessage());
}

return null;
}
}
  • 提取txt 提取word
读取音频时长
256哈希计算工具类
  1. 1. 依赖
  2. 2. 工具类
© 2023 曳戈泰尔
Hexo Theme Yilia by Litten
  • 所有文章
  • 其他站点
  • 关于我

tag:

  • SHA256
  • AI换脸
  • VITS
  • APM
  • Anaconda
  • AnsibleAWX
  • ArrayList
  • Bean
  • BigDecimal
  • Blender three.js
  • API开放接口
  • xmlRPC
  • Ice
  • MySQL
  • BeanUtils
  • Tomcat
  • Caffeine
  • gradle
  • Docker
  • jdk
  • maven
  • JDK
  • localtime
  • Minio
  • PostgreSQL
  • RT_Linux
  • kafka
  • geany
  • CentOS
  • Elasticsearch
  • Node
  • FastDFS
  • Nginx
  • CompletableFutures
  • CompletableFuture
  • CountDownLatch
  • queue
  • Conflux
  • DefaultIdentifierGenerator
  • gdb
  • Deepin
  • Deferred
  • 自动化部署
  • Nacos
  • Redis
  • RocketMQ
  • docker-compose
  • docker日志
  • Docker部署
  • Drools
  • Vue
  • 持有者作为调用者
  • Error
  • ES
  • 签名上链
  • FISCO
  • Prometheus-Grafana-nodeExporter
  • FISCO开放平台
  • 解析input
  • ForkJoinPool
  • GateWay
  • Git
  • GeoServer
  • GitLab
  • Gradle
  • Spring
  • Gitlab
  • HTTP
  • Hexo GitHub-Pages
  • HttpUtil
  • IDEA
  • Gson
  • 热部署
  • HttpClientUtil
  • 搜索
  • Stream
  • foreach
  • Graphics2D
  • Synchronized
  • 循环
  • Integer
  • base64
  • JAVA
  • Excel
  • openID
  • NowTime
  • MD5
  • 字节流
  • 手机号
  • 支付宝
  • Object
  • 行政区划
  • 序列化
  • telnet
  • 枚举
  • 限流
  • 配置文件
  • 规则执行器
  • cmd
  • netty websocket
  • JAVE2
  • 线程池
  • 分治
  • JMH
  • JVM
  • Jenkins
  • Java调用AI输入输出
  • JWT
  • Kindle
  • Knif4j
  • jar
  • UDP
  • SonarQube
  • 部署
  • Ansible
  • IP
  • socket
  • List排序
  • MQ
  • MapStruct
  • Maven
  • MyBatis-Plus
  • MyBatis
  • 跳板机
  • event
  • trigger
  • 全文索引
  • 扣费 线程安全
  • MybatisPlus
  • LambdaQueryWrapper
  • Navicat
  • Explain
  • 私人助理
  • nacos
  • Nexus
  • WebSocket
  • OSS
  • OkHttpClient
  • OA
  • PicGo
  • 可视化监控
  • Optional
  • SpringBoot
  • 关键词
  • TSV
  • 性能指标
  • json
  • Pycharm
  • 文件夹遍历
  • TCP
  • Qt
  • QueryWrapper
  • Quartz
  • RSA
  • RabbitMQ
  • RateLimiter
  • Redisson
  • 阻塞等待锁
  • ZSET
  • incr
  • 频率限制
  • SAE
  • RESTful API
  • SCP
  • RuoYi
  • SM3
  • SKU
  • SQL
  • SQL前n天
  • SSL
  • Shell脚本自动化
  • Sleuth
  • Socket
  • PageHelper
  • Solidity
  • gateway
  • Batch
  • Spring Boot
  • build.gradle
  • Schedule
  • 循环重试
  • Undertow
  • 多筛选
  • IPFS
  • Jasypt
  • logback
  • 加解密
  • 幂等性
  • Result
  • log
  • Mail
  • 滑块
  • Druid
  • knife4j
  • 注入
  • Full Lite
  • 权限
  • 跨域访问
  • starter
  • 防刷
  • XSS
  • Event
  • 多数据库
  • Scheduled
  • yml
  • Async
  • AOP
  • CurrentUser
  • AutoGenerator
  • netty
  • Openfeign
  • Sentinel
  • Shiro
  • Swagger
  • XMLRPC
  • captcha
  • OAuth
  • 文件上传
  • 验证码登录
  • Response
  • Exception
  • 文件下载
  • 自定义注解
  • Thread
  • 观察者
  • 音视频
  • dll
  • StopWatch
  • String
  • Transactional
  • ThreadLocal
  • TLS
  • 挂载
  • VMware
  • VPN
  • VSAT
  • VScode
  • VS
  • Valid
  • 二维码
  • three.js
  • ECDSA
  • Tornado
  • WorkBench
  • VxWorks
  • select
  • taskSpawn
  • WPS
  • WeBase
  • JavaScript
  • COOKIE
  • 消息推送
  • 开启启动
  • VxWorks WorkBench
  • XStream
  • ab
  • appId appKey
  • printStackTrace
  • gitlab
  • excel2word
  • git
  • 经纬度
  • isNull isEmpty isBlank
  • mybatisplus
  • SSH
  • nohup日志
  • phpstudy
  • npm
  • 图片
  • nginx
  • url
  • xml
  • 去背景
  • 提取学号
  • 一键登录
  • xxl-job
  • 并发
  • 接口
  • 一致性HASH
  • 责任链
  • 两层请求体
  • 二次支付
  • 个人支付
  • 设计模式
  • 代理
  • MERGE
  • 保存MultipartFile
  • PDF
  • 链间数据迁移
  • session
  • 鉴权
  • 证书生成
  • 单例
  • 压测
  • shell
  • 发布Jar
  • sms
  • 升级代理合约
  • 支付
  • 图片转PDF
  • 拍平JSON
  • SSO
  • property
  • 内容安全
  • 循环分页
  • crontab
  • 日志清理
  • 实名
  • 绩效
  • 读书笔记
  • 歌词识别
  • component初始化
  • 抽奖
  • 数据脱敏
  • 验证码
  • 网络攻防
  • 慢排查
  • Native支付
  • 裁剪字符串
  • WebView
  • 文本折叠
  • 上拉加载数据
  • 弹窗输入框
  • 图片裁剪
  • banner
  • 局部刷新
  • 弹窗
  • StorageSync
  • 标签id值
  • openId
  • 角标
  • globalData
  • url传值
  • Feign
  • 懒加载
  • 订阅消息
  • 设备交接
  • 提取txt 提取word
  • 回调
  • 支付超时
  • Assert
  • 乐观锁
  • 服务器
  • 监控
  • 运维
  • 方案
  • Enum
  • 测试
  • 校招
  • 死锁
  • 兑换码
  • 订单
  • 余额扣减
  • sku gson
  • 电商
  • 短信验证码
  • 伏羲PDF
  • 秒杀
  • 后台
  • 不可破解key
  • 排查
  • 线程安全 Map List
  • 上下电
  • CRUD拆分的宽表
  • ip2region
  • 行政
  • 文件校验FileSize
  • ParameterMap
  • EventBus
  • 微信手机号
  • 购买掉单
  • resources
  • 音频时长
  • IDCardUtils
  • Ghelper
  • Forest
  • 邀请
  • 过滤器 拦截器
  • 通信
  • Retry
  • 人脸融合
  • 时间差
  • 短信
  • 集合
  • 长安链

    缺失模块。
    1、请确保node版本大于6.2
    2、在博客根目录(注意不是yilia根目录)执行以下命令:
    npm i hexo-generator-json-content --save

    3、在根目录_config.yml里添加配置:

      jsonContent:
        meta: false
        pages: false
        posts:
          title: true
          date: true
          path: true
          text: false
          raw: false
          content: false
          slug: false
          updated: false
          comments: false
          link: false
          permalink: false
          excerpt: false
          categories: false
          tags: true
    

  • 区块链
  • GPT
  • gitee1
  • gitee2
  • github
  • Other
徘徊在原子与字节的边缘

将更好的自己呈现给世界



修心至要,事上磨练

一蓑衣,一壶酒,一曲长歌,一剑天涯

见自己,见天地,见众生