/**
* 把附件上传至服务器目录下
*
* @param files
* @param req
* @return
*/
@ResponseBody
@RequestMapping(value = "readFile", method = RequestMethod.POST)
public Map<String, Object> readUploadFileCommon(@RequestParam("readFile") MultipartFile file,
HttpServletRequest request) throws Exception {
Map<String, Object> result = new HashMap<String, Object>();
if (file == null) {
return null;
}
String txt = "";
String fileName = file.getOriginalFilename();
String type = fileName.substring(fileName.lastIndexOf(".")+1, fileName.length());
if (type.equals("doc")) {
try {
//2003
HWPFDocument doc = new HWPFDocument(file.getInputStream());
txt = doc.getText().toString();
} catch (OfficeXmlFileException e) {
//2007
XWPFDocument doc = new XWPFDocument(file.getInputStream());// 创建Word文件
List<XWPFParagraph> paras = doc.getParagraphs();
for(XWPFParagraph para: paras) {
txt += para.getText().toString();
}
}
} else if (type.equals("docx")) {
File uFile = new File("tempFile.docx");
if (!uFile.exists()) {
uFile.createNewFile();
}
FileCopyUtils.copy(file.getBytes(), uFile);
OPCPackage opcPackage = POIXMLDocument.openPackage("tempFile.docx");
POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);
txt = extractor.getText();
uFile.delete();
}
result.put("message", txt);
return result;
}