linux系统读取PDF文件中的文本并存储到数据库中

项目场景:

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");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值