项目场景:
linux系统读取PDF文件中的文本并存储到数据库中
问题描述
提示:Linux无法识别\r\n换行符
例如:pdf数据读取出来后需要通过\r\n换行符来分割每行的数据进行处理,但是\r\n时windows系统中的换行符,Linux无法识别
读取pdf文件关键代码
//加载pdf文件,创建PDDocument对象
File file1 = ImageUtil.multipartFileToFile(file);
PDDocument document = null;
//创建pdf文本获取对象PDFTextStripper
PDFTextStripper pdfStripper = null;
//获取pdf中所有信息,text中包含的就是当前pdf文档中所有信息
String text = null;
try {
document = PDDocument.load(file1);
pdfStripper = new PDFTextStripper();
text = pdfStripper.getText(document);
document.close();
} catch (IOException e) {
throw new InsertException(e.getMessage());
}
file1.delete();
/**
* MultipartFile 转 File
*
* @param file
* @throws Exception
*/
public static File multipartFileToFile(MultipartFile file) {
Assert.notNull(file, "身份证不能为空!");
File toFile;
if (file.equals("") || file.getSize() <= 0) {
return null;
} else {
InputStream ins = null;
try {
ins = file.getInputStream();
toFile = new File(file.getOriginalFilename());
inputStreamToFile(ins, toFile);
ins.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return toFile;
}
依赖
<!-- 读取pdf -->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>fontbox</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>jempbox</artifactId>
<version>1.8.11</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>xmpbox</artifactId>
<version>2.0.0</version>
</dependency>
原因分析:
提示:unix的换行符是\n
解决方案:
提示:括号内修改为\n即可解决
String[] strs = text.split("\n");