所需依赖
<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;
}