上传word文档,将word转换成html

it2024-10-22  39

因业务需要将上传的word文档(包含图片)转换成html在页面展示,所以编写以下代码,红色内容为主要内容,可忽略掉业务代码 /** * 新增活动,新闻,通过上传word文件的方式 * @throws BusinessException * @throws IOException */ @PostMapping(value = "add/{utype}") public JsonResult<ContentInfo> add( ContentInfo contentInfo, @PathVariable(name = "utype") Integer utype, MultipartFile file, @RequestParam(name = "preview",required = false) String preview //null不预览 ) throws BusinessException, IOException { JsonResult<ContentInfo> jsonResult = new JsonResult<>(); ExtUser curUser = usersService.findCurrentUser(); if (curUser == null) { throw new BusinessException(ErrorEnum.AUTH_NOT_LOGIN); } switch (utype){ case 1: if(contentInfo.getType() == 0){ throw new BusinessException(ErrorEnum.ERROR_PARAM, "内容类型不能为空!"); } if(null == contentInfo.getCategoryId()){ throw new BusinessException(ErrorEnum.ERROR_PARAM, "类别不能为空!"); } //活动,新闻,招聘 if(contentInfo.getType() == 1 || contentInfo.getType() == 2 || contentInfo.getType() == 3){ if(StringUtils.isEmpty(contentInfo.getTitle())){ throw new BusinessException(ErrorEnum.ERROR_PARAM, "标题不能为空!"); } if(null == file && StringUtils.isEmpty(contentInfo.getContent())){ throw new BusinessException(ErrorEnum.ERROR_PARAM, "内容不能为空!"); }else{ if(null != file && !file.isEmpty()){ FileUpload fileUpload = fileUploadService.upLoad(file); contentInfo.setFileId(fileUpload.getId()); String filePath = FilenameUtils.concat(UtilsFile.getUrl(), file.getOriginalFilename()); // 转存文件 file.transferTo(new File(filePath)); String htmlStr = UtilsWord.toHtml(filePath); if(htmlStr.contains("./word/media")){ htmlStr = htmlStr.replaceAll("./word/media",fileConfig.getServerUrl()+"/tempFanyu/word/media"); } contentInfo.setContent(htmlStr); } } } //活动,新闻,banner if(contentInfo.getType() == 1 || contentInfo.getType() == 2 || contentInfo.getType() == 4){ if(null == contentInfo.getCoverFileId()){ throw new BusinessException(ErrorEnum.ERROR_PARAM, "封面图片不能为空!"); } if(null == contentInfo.getSort()){ throw new BusinessException(ErrorEnum.ERROR_PARAM, "顺序值不能为空!"); } } //活动,新闻 if(contentInfo.getType() == 1 || contentInfo.getType() == 2){ if(StringUtils.isEmpty(contentInfo.getEditorCharge())){ throw new BusinessException(ErrorEnum.ERROR_PARAM, "责任编辑不能为空!"); } if(null == contentInfo.getBrowseNumber()){ throw new BusinessException(ErrorEnum.ERROR_PARAM, "浏览次数不能为空!"); } if(StringUtils.isEmpty(contentInfo.getSumInfo())){ throw new BusinessException(ErrorEnum.ERROR_PARAM, "摘要信息不能为空!"); } if(StringUtils.isEmpty(contentInfo.getDataSource())){ throw new BusinessException(ErrorEnum.ERROR_PARAM, "信息来源不能为空!"); } if(contentInfo.getContentSource() == 0){ throw new BusinessException(ErrorEnum.ERROR_PARAM, "内容来源不能为空!"); } } //banner if(contentInfo.getType() == 4){ if(StringUtils.isEmpty(contentInfo.getBannerLink())){ throw new BusinessException(ErrorEnum.ERROR_PARAM, "banner链接不能为空!"); } } //是否预览 if(StringUtils.isEmpty(preview)){ contentInfo.setCreateTime(new Date()); contentInfo.setState((byte) 1); contentInfoService.insert(contentInfo); } jsonResult.setSuccessData(contentInfo); break; case 2: Map<String,Object> param = new HashMap<>(); param.put("id",contentInfo.getId()); List<ExtraContentInfo> ExtraContentInfo = contentInfoService.selectByMap(param); if(ExtraContentInfo.isEmpty()){ throw new BusinessException(ErrorEnum.ERROR_PARAM, "没有该数据!"); } if(null != file &&!file.isEmpty()){ FileUpload fileUpload = fileUploadService.upLoad(file); contentInfo.setFileId(fileUpload.getId()); String filePath = FilenameUtils.concat(UtilsFile.getUrl(), file.getOriginalFilename()); // 转存文件 file.transferTo(new File(filePath)); String htmlStr = UtilsWord.toHtml(filePath); if(htmlStr.contains("./word/media")){ htmlStr = htmlStr.replaceAll("./word/media",fileConfig.getServerUrl()+"/tempFanyu/word/media"); } contentInfo.setContent(htmlStr); } //是否预览 if(StringUtils.isEmpty(preview)){ contentInfo.setUpdateTime(new Date()); contentInfoService.update(contentInfo); } jsonResult.setSuccessData(contentInfo); break; default: throw new BusinessException(ErrorEnum.ERROR_PARAM, "type值有误"); } return jsonResult; }

 

public interface FileUploadService { FileUpload selectById(Integer id); int insert(FileUpload fileUpload); FileUpload upLoad(MultipartFile file); } @Override public FileUpload upLoad(MultipartFile file) { FileUpload fu = null; try { FileObject fileObject = new FileObject(file); String filename = file.getOriginalFilename(); String fileInfoId = UUIDUtils.randReplacedLower() + filename.substring(filename.lastIndexOf('.')); String date = DateFormatUtils.format(new Date(), "yyyyMMdd"); String relativePath = "/" + fileConfig.getSysName() + "/" + date + "/" + fileInfoId; fu = new FileUpload(); fu.setFileName(filename); fu.setFilePath(fileConfig.getServerUrl() + relativePath); // 保存文件 String filePath = fileConfig.getFileBasePath() + relativePath; filePath = FilenameUtils.normalize(filePath, true); File fileDir = new File(FilenameUtils.getFullPath(filePath)); if (!fileDir.exists()) { fileDir.mkdirs(); } File saved = new File(filePath); FileUtils.writeByteArrayToFile(saved, fileObject.getBytes()); fileUploadDao.insert(fu); } catch (IOException e) { e.printStackTrace(); } FileUpload fileUpload = fileUploadDao.selectById(fu.getId()); return fileUpload; } public class UtilsWord { static String path = UtilsFile.getUrl(); public static String toHtml(String filepath){ if (filepath.indexOf(".") >= 0) { String fileType = filepath.substring(filepath.lastIndexOf(".") + 1, filepath.length()); if (fileType.equalsIgnoreCase("doc")) { try { return docToHtml(filepath); } catch (Exception e) { e.printStackTrace(); } } else if (fileType.equalsIgnoreCase("docx")) { try { return docxToHtml(filepath); } catch (Exception e) { e.printStackTrace(); } } } return ""; } /** * word07版本(.docx)转html * poi:word07在线预览 * */ public static String docxToHtml(String filepath) throws IOException { File f = new File(filepath); if (!f.exists()) { System.out.println("Sorry File does not Exists!"); } else { if (f.getName().endsWith(".docx") || f.getName().endsWith(".DOCX")) { //读取文档内容 InputStream in = new FileInputStream(f); XWPFDocument document = new XWPFDocument(in); File imageFolderFile = new File(path); //加载html页面时图片路径 XHTMLOptions options = XHTMLOptions.create().URIResolver(new BasicURIResolver("./")); //图片保存文件夹路径 options.setExtractor(new FileImageExtractor(imageFolderFile)); OutputStream out = new FileOutputStream(new File(path+"/templ.html")); XHTMLConverter.getInstance().convert(document, out, options); out.close(); System.out.println(path); return readFile(path+"/templ.html"); } else { System.out.println("Enter only MS Office 2007+ files"); } } return ""; } /** * word03版本(.doc)转html * poi:word03在线预览 * */ public static String docToHtml(String filepath) { try { InputStream input = new FileInputStream(filepath); HWPFDocument wordDocument = new HWPFDocument(input); WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter( DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument()); wordToHtmlConverter.setPicturesManager(new PicturesManager() { public String savePicture(byte[] content, PictureType pictureType, String suggestedName, float widthInches, float heightInches) { //图片在html页面加载路径 return "image\\"; } }); wordToHtmlConverter.processDocument(wordDocument); //获取文档中所有图片 List pics = wordDocument.getPicturesTable().getAllPictures(); if (pics != null) { for (int i = 0; i < pics.size(); i++) { Picture pic = (Picture) pics.get(i); try {//图片保存在文件夹的路径 pic.writeImageContent(new FileOutputStream(path + pic.suggestFullFileName())); } catch (FileNotFoundException e) { e.printStackTrace(); } } } //创建html页面并将文档中内容写入页面 Document htmlDocument = wordToHtmlConverter.getDocument(); ByteArrayOutputStream outStream = new ByteArrayOutputStream(); DOMSource domSource = new DOMSource(htmlDocument); StreamResult streamResult = new StreamResult(outStream); TransformerFactory tf = TransformerFactory.newInstance(); Transformer serializer = tf.newTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty(OutputKeys.METHOD, "html"); serializer.transform(domSource, streamResult); outStream.close(); String content = new String(outStream.toString("UTF-8")); FileUtils.writeStringToFile(new File(path, "templ.html"), content, "utf-8"); return readFile(path+"/templ.html"); }catch (Exception e){ } return ""; } public static String readFile(String path){ // 读 File file = new File(path); FileWriter out = null; try { FileReader in = new FileReader(file); BufferedReader bufIn = new BufferedReader(in); // 内存流, 作为临时流 CharArrayWriter tempStream = new CharArrayWriter(); // 替换 String line = null; StringBuffer sb=new StringBuffer(); while ( (line = bufIn.readLine()) != null) { // 替换每行中, 符合条件的字符串 line = line.replaceAll("<body>", ""); line = line.replaceAll("</body>", ""); line = line.replaceAll("<html(.*?)>", ""); line = line.replaceAll("</html>", ""); // line = line.replaceAll("&times;", "x"); // line = line.replaceAll("&hellip;", "..."); // font-family:' ' // ^123.*abc$ // font-family:\"(.*?)\" line=line.replaceAll("font-family:'(.*?)'","font-family:FangSong_GB2312"); line=line.replaceAll("<body(.*?)>",""); // 将该行写入内存 tempStream.write(line); // 添加换行符 tempStream.append(System.getProperty("line.separator")); sb.append(line); } System.out.println(line); // 关闭 输入流 bufIn.close(); // 将内存中的流 写入 文件 out = new FileWriter(file); tempStream.writeTo(out); out.close(); boolean delete = file.delete(); System.out.println("删除文件"+delete); System.out.println(sb.toString()); return StringEscapeUtils.unescapeHtml(sb.toString()); // return sb.toString(); }catch (Exception e){ e.printStackTrace(); if(out != null){ try { out.close(); } catch (IOException e1) { e1.printStackTrace(); } } } return ""; } public static void main(String[] args) { String s = UtilsWord.toHtml("E:\\系统接口文档V1.0.0.docx"); System.out.println(s); } }

 

最新回复(0)