java解析文件文本内容

所需依赖

 <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>5.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>5.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>5.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>2.0.23</version>
        </dependency>

代码

 @RequestMapping(value = "/addfile",method=RequestMethod.POST)
    public Map<String,Object> addfile(MultipartFile file) throws Exception {
        Map<String,Object> response = new HashMap<>();
        if (file.isEmpty()) throw new BadRequestException("上传的文件不能为空");
        String title="";
       String[] str=file.getOriginalFilename().split("\\.");
        for (int i=0;i<str.length;i++){
            if(i<str.length-1){
                title=title+str[i];
            }
        }
        String pref=str[str.length-1];
        String content="";
        if(pref.equals("txt")){
            byte[] bytes = file.getBytes();
            content = new String(bytes, StandardCharsets.UTF_8);
        }else if(pref.equals("docx")){
            XWPFDocument xdoc = new XWPFDocument(file.getInputStream());
            POIXMLTextExtractor extractor = new XWPFWordExtractor(xdoc);
            // result就是识别结果
            content = extractor.getText();
            extractor.close();
            System.out.println(content);
        }else if(pref.equals("doc")){
            WordExtractor ex = new WordExtractor(file.getInputStream());
            content = ex.getText();
            System.out.println(content);
        }else {
//multipartFile为multipartFile文件类型,将文件转化为文件流被PDDocument加载
            PDDocument document = PDDocument.load(file.getInputStream());
            document.getClass();
//使用PDFTextStripper 工具
            PDFTextStripper tStripper = new PDFTextStripper();
//设置文本排序,有规则输出
            tStripper.setSortByPosition(true);
//获取所有文字信息
            content = tStripper.getText(document);
            System.out.println(content);
        }

        response.put("code",200);
        return response;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值