Java读取上传的word文档内容

@RequestMapping(value = "save", method = RequestMethod.POST)
public R save(@RequestParam("imgFile") MultipartFile file) {
    String fileName = file.getOriginalFilename();
    System.out.println("原始文件名:" + fileName);
    String content = "";
    try {
        byte [] byteArr   = file.getBytes();
        InputStream inputStream = new ByteArrayInputStream(byteArr);
        if (fileName.endsWith(".doc")) {
            WordExtractor ex =  new WordExtractor(inputStream);
            content = ex.getText();
        } else if (fileName.endsWith("docx")) {
            OPCPackage opcPackage =OPCPackage.open(inputStream);
            POIXMLTextExtractor extractor =  new XWPFWordExtractor(opcPackage);
            content = extractor.getText();
            opcPackage.close();
        } else {
            System.out.println("此文件不是word文件!");
        }
        inputStream.close();
        System.out.println("文件内容::"+content);
    } catch (XmlException e) {
        e.printStackTrace();
    } catch (OpenXML4JException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return R.ok();
}

  

posted @ 2022-07-21 09:58    阅读(484)  评论(0编辑  收藏  举报