Ueditor二次编辑word(doc和docx格式),SpringBoot后端

vue+ueditor+springboot, 实现word文档上传编辑

前言

`前端导入word文档(doc和docx格式都支持),Ueditor富文本回显进行二次编辑,目前ueditor项目archived了,实现两种格式的相关材料相对稀缺。

`解决思路:

1.上传word文件

2.后台读取word内容(图片需要额外处理保存到服务器固定的地址,该地址能让浏览器直接访问),生成html文件

3.后台读取html文件内容返回给前端

一、目标

通过上传word文件,通过后台进行解析回显到前端。

Ueditor二次编辑word(doc和docx格式),SpringBoot后端

二、代码步骤

后端代码结构:

Ueditor二次编辑word(doc和docx格式),SpringBoot后端

1.maven依赖库

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>org.apache.poi.xwpf.converter.xhtml</artifactId>
<version>1.0.6</version>
</dependency>

2.vue页面读取ueditor的配置

代码如下:

/**
* 获取UE文件上传配置
* @param request
* @param response
* @throws IOException
*/
@GetMapping(value = "/config")
public void ueConfig(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
String urlPrefix = ueProperties.getSavepath();
log.info("urlPrefix = "+urlPrefix);
String exec = "{
" +
" /* 上传图片配置项 */
" +
" "imageActionName": "catcherImage", /* 执行上传图片的action名称 */
" +
" "imageFieldName": "upfile", /* 提交的图片表单名称 */
" +
" "imageMaxSize": 2048, /* 上传大小限制,单位B */
" +
" "imageAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], /* 上传图片格式显示 */
" +
" "imageCompressEnable": true, /* 是否压缩图片,默认是true */
" +
" "imageCompressBorder": 800, /* 图片压缩最长边限制 */
" +
" "imageInsertAlign": "none", /* 插入的图片浮动方式 */
" +
" "imageUrlPrefix": "" + urlPrefix + "", /* 图片访问路径前缀 */
" +
" "imagePathFormat": "/ueditor/image/{yyyy}{mm}{dd}/", /* 上传保存路径,可以自定义保存路径和文件名格式 */
" +
" /* {filename} 会替换成原文件名,配置这项需要注意中文乱码问题 */
" +
" /* {rand:6} 会替换成随机数,后面的数字是随机数的位数 */
" +
" /* {time} 会替换成时间戳 */
" +
" /* {yyyy} 会替换成四位年份 */
" +
" /* {yy} 会替换成两位年份 */
" +
" /* {mm} 会替换成两位月份 */
" +
" /* {dd} 会替换成两位日期 */
" +
" /* {hh} 会替换成两位小时 */
" +
" /* {ii} 会替换成两位分钟 */
" +
" /* {ss} 会替换成两位秒 */
" +
" /* 非法字符  : * ? " < > | */
" +
" /* 具请体看线上文档: fex.baidu.com/ueditor/#use-format_upload_filename */
" +
"
" +
" /* 涂鸦图片上传配置项 */
" +
" "scrawlActionName": "uploadscrawl", /* 执行上传涂鸦的action名称 */
" +
" "scrawlFieldName": "upfile", /* 提交的图片表单名称 */
" +
" "scrawlPathFormat": "/ueditor/image/{yyyy}{mm}{dd}/", /* 上传保存路径,可以自定义保存路径和文件名格式 */
" +
" "scrawlMaxSize": 2048000, /* 上传大小限制,单位B */
" +
" "scrawlUrlPrefix": "", /* 图片访问路径前缀 */
" +
" "scrawlInsertAlign": "none",
" +
"
" +
" /* 截图工具上传 */
" +
" "snapscreenActionName": "uploadimage", /* 执行上传截图的action名称 */
" +
" "snapscreenPathFormat": "/ueditor/image/{yyyy}{mm}{dd}/", /* 上传保存路径,可以自定义保存路径和文件名格式 */
" +
" "snapscreenUrlPrefix": "", /* 图片访问路径前缀 */
" +
" "snapscreenInsertAlign": "none", /* 插入的图片浮动方式 */
" +
"
" +
" /* 抓取远程图片配置 */
" +
" "catcherLocalDomain": ["127.0.0.1", "localhost", "img.baidu.com"],
" +
" "catcherActionName": "catchimage", /* 执行抓取远程图片的action名称 */
" +
" "catcherFieldName": "source", /* 提交的图片列表表单名称 */
" +
" "catcherPathFormat": "/ueditor/image/{yyyy}{mm}{dd}/", /* 上传保存路径,可以自定义保存路径和文件名格式 */
" +
" "catcherUrlPrefix": "" + urlPrefix + "", /* 图片访问路径前缀 */
" +
" "catcherMaxSize": 2048000, /* 上传大小限制,单位B */
" +
" "catcherAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], /* 抓取图片格式显示 */
" +
"
" +
" /* 上传视频配置 */
" +
" "videoActionName": "uploadvideo", /* 执行上传视频的action名称 */
" +
" "videoFieldName": "upfile", /* 提交的视频表单名称 */
" +
" "videoPathFormat": "/ueditor/video/{yyyy}{mm}{dd}/", /* 上传保存路径,可以自定义保存路径和文件名格式 */
" +
" "videoUrlPrefix": "", /* 视频访问路径前缀 */
" +
" "videoMaxSize": 10240000, /* 上传大小限制,单位B,默认10MB */
" +
" "videoAllowFiles": [
" +
" ".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg",
" +
" ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid"], /* 上传视频格式显示 */
" +
" /* 上传文件配置 */
" +
" "fileActionName": "uploadfile", /* controller里,执行上传视频的action名称 */
" +
" "fileFieldName": "upfile", /* 提交的文件表单名称 */
" +
" "filePathFormat": "/ueditor/file/{yyyy}{mm}{dd}/", /* 上传保存路径,可以自定义保存路径和文件名格式 */
" +
" "fileUrlPrefix": "", /* 文件访问路径前缀 */
" +
" "fileMaxSize": 10240000, /* 上传大小限制,单位B,默认10MB */
" +
" "fileAllowFiles": [
" +
" ".png", ".jpg", ".jpeg", ".gif", ".bmp",
" +
" ".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg",
" +
" ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid",
" +
" ".rar", ".zip", ".tar", ".gz", ".7z", ".bz2", ".cab", ".iso",
" +
" ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".txt", ".md", ".xml"
" +
" ], /* 上传文件格式显示 */
" +
" /* 列出指定目录下的图片 */
" +
" "imageManagerActionName": "listimage", /* 执行图片管理的action名称 */
" +
" "imageManagerListPath": "/ueditor/image/{yyyy}{mm}{dd}/", /* 指定要列出图片的目录 */
" +
" "imageManagerListSize": 20, /* 每次列出文件数量 */
" +
" "imageManagerUrlPrefix": "" + urlPrefix + "", /* 图片访问路径前缀 */
" +
" "imageManagerInsertAlign": "none", /* 插入的图片浮动方式 */
" +
" "imageManagerAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], /* 列出的文件类型 */
" +
" /* 列出指定目录下的文件 */
" +
" "fileManagerActionName": "listfile", /* 执行文件管理的action名称 */
" +
" "fileManagerListPath": "/ueditor/file/{yyyy}{mm}{dd}/", /* 指定要列出文件的目录 */
" +
" "fileManagerUrlPrefix": "", /* 文件访问路径前缀 */
" +
" "fileManagerListSize": 20, /* 每次列出文件数量 */
" +
" "fileManagerAllowFiles": [
" +
" ".png", ".jpg", ".jpeg", ".gif", ".bmp",
" +
" ".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg",
" +
" ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid",
" +
" ".rar", ".zip", ".tar", ".gz", ".7z", ".bz2", ".cab", ".iso",
" +
" ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".txt", ".md", ".xml"
" +
" ] /* 列出的文件类型 */
" +
"}";
PrintWriter writer = response.getWriter();
writer.write(exec);
writer.flush();
writer.close();
}

“`

3.前端导入文件

代码如下:

methods: {
ready(editorInstance) {
  this.editorInstance=editorInstance
  async uploadWordFile(event) {
  const file = event.target.files[0];
  if (!file) return;
  // 将Word文件转换为HTML
  const htmlContent = await this.convertWordToHtml(file);
  const jsonData = JSON.parse(htmlContent)
  // 设置UEditor的内容
  console.log(jsonData)
  this.editorInstance.execCommand('inserthtml',jsonData.data)
},
async convertWordToHtml(wordFile) {
  // 这里应该是Word文件转HTML的后端接口调用代码
  // 假设有一个转换Word为HTML的后端API
  const formData = new FormData();
  formData.append('file', wordFile);
  const response = await fetch('/api/ue/uploadFile',{
  method:'POST',
  body:formData
})
if (response.ok) {
	return await response.text();
}
throw new Error('转换失败');
}
}

4.后端读取文件生成html

代码如下:


/** word文档上传
*
* @param file
* @return
*/
@PostMapping("/uploadFile")
public Object uploadFile(@RequestParam(name = "file") MultipartFile file){
  String filename = file.getOriginalFilename();
  JSONObject result = new JSONObject();
  String visitHtml = "";
  try {
    if (filename.endsWith(".docx")) {
    //TODO 处理docx格式的
    visitHtml = WordConverHtmlUtils.docxToHtmlText(file, ueProperties);
    } else if (filename.endsWith(".doc")) {
    //TODO 处理doc格式的
    visitHtml = WordConverHtmlUtils.docToHtmlText(file, ueProperties);
  } else {
  	log.error("不支持的文件格式!");
  }
  result.put("state", "SUCCESS");
  result.put("data", visitHtml);
  log.info("result: {}", result.toString());
  } catch (Exception e) {
 		 log.error("文件找不到异常!");
  	 e.printStackTrace();
  }
  return result;
}

5.WordConverHtmlUtils工具类

⚠️⚠️⚠️⚠️⚠️

options.URIResolver(new BasicURIResolver(picUri));

picUri地址,必须能通过浏览器直接访问,否则编辑器中无法渲染出来图片; 列如作者:
http://localhost:8000/resource/ueditor/file/20240404/1712220732312.png(本地搭建NG测试)

⚠️⚠️⚠️⚠️⚠️

代码如下:
package com.ue.demo.utils;
import cn.hutool.core.lang.UUID;
import com.ue.demo.config.UeProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.converter.PicturesManager;
import org.apache.poi.hwpf.converter.WordToHtmlConverter;
import org.apache.poi.hwpf.usermodel.PictureType;
import org.apache.poi.xwpf.converter.core.BasicURIResolver;
import org.apache.poi.xwpf.converter.core.FileImageExtractor;
import org.apache.poi.xwpf.converter.xhtml.XHTMLConverter;
import org.apache.poi.xwpf.converter.xhtml.XHTMLOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.web.multipart.MultipartFile;
import org.w3c.dom.Document;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* @author:Peanut
* @create: 2024-04-05 10:22
* @version: 1.0.0
* @description:
*/
@Slf4j
  public class WordConverHtmlUtils {
        private final static String FILE_URL_PRE = "/ueditor/file/";
        /**
        * 上传docx文档,返回解析后的Html
        */
        public static String docxToHtmlText(MultipartFile file, UeProperties ueProperties) throws Exception {
        try {
        String fileName = UUID.fastUUID().toString();
        //图片存放地址
        String imagePath = ueProperties.getSavepath().concat(FILE_URL_PRE).concat("/");
        String fileOutName = imagePath.concat(fileName).concat(".html");
        log.info("上传docx文档解析");
        log.info("上传docx文档,返回解析后的Html, imagePath:{}", imagePath);
        log.info("fileOutName:{}", fileOutName);
        //获取一个用操作Word的对象
        XWPFDocument document = new XWPFDocument(file.getInputStream());
        //导出为html时的一些基本设置类
        XHTMLOptions options = null;
        //判断word文件中是否有图片
        if(document.getAllPictures().size() > 0) {
        //获取默认的对象,设置缩进indent
        options = XHTMLOptions.getDefault().indent(4);
        // 如果包含图片的话,要设置图片的导出位置
        File imageFolder = new File(imagePath);
        //设置图片抽取器的目的地文件夹 用于存放图片文件
        options.setExtractor(new FileImageExtractor(imageFolder));
        // URI resolver word的html中图片的目录路径
        //⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️
        //⚠️⚠️⚠️⚠️⚠️ 这里需要设置为前端能过直接访问到的图片地址, 列如作者:http://localhost:8000/resource/ueditor/file/20240404/1712220732312.png,
        //⚠️⚠️⚠️⚠️⚠️ 否则,ueditor编辑器无法显示word文档中的图片
        String picUri = ueProperties.getShowpath().concat(imagePath.substring(imagePath.indexOf("ueditor")));
        options.URIResolver(new BasicURIResolver(picUri));
      }
      //获取输出的html文件对象
      File outFile = new File(fileOutName);
      if(!outFile.getParentFile().exists()){
      outFile.getParentFile().mkdirs();
      }
      //创建所有的父路径,如果不存在父目录的话
      outFile.getParentFile().mkdirs();
      //创建一个输出流
      OutputStream out = new FileOutputStream(outFile);
      //html转换器
      XHTMLConverter.getInstance().convert(document, out, options);
      log.info("html转换器 success");
      //处理生成的html,字符串形式给前端
      return readHtmlStr(fileOutName);
      } catch (Exception e) {
      log.error("docxToHtmlText 解析异常", e);
      }
      return "";
}
/**
* 上传doc格式Word文档,返回解析后的Html
* @param file
* @param ueProperties
* @return
* @throws Exception
*/
public static String docToHtmlText(MultipartFile file, UeProperties ueProperties) throws Exception {
      //使用字符数组流获取解析的内容
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      OutputStream outStream = new BufferedOutputStream(baos);
      try {
      String fileName = UUID.fastUUID().toString();
      //将上传的文件传入Document转换
      //图片存放地址
      String docPath = ueProperties.getSavepath().concat(FILE_URL_PRE).concat("/");
      String imagePath = docPath.concat("image/");
      String fileOutName = docPath.concat(fileName).concat(".html");
      log.info("上传doc文档,返回解析 ");
      log.info("fileOutName:{}", fileOutName);
      //创建图片文件的存储目录
      new File(imagePath).mkdirs();
      //poi中doc文档对应的实体类
      HWPFDocument hwpfDocument = new HWPFDocument(file.getInputStream());
      //使用空的文档对象构建一个转换对象
      WordToHtmlConverter converter = new WordToHtmlConverter(DocumentBuilderFactory
      .newInstance()
      .newDocumentBuilder()
      .newDocument());
      //设置存储图片的管理者--使用匿名内部类实现 该类实现了PicturesManager接口,实现了其中的savePicture方法
      converter.setPicturesManager(new PicturesManager() {
      FileOutputStream out = null;
      //在下面的processDocument方法内部会调用该方法 用于存储word中的图片文件
      @Override
      public String savePicture(byte[] bytes, PictureType pictureType, String name, float width, float height) {
      try {
      //单个照片的保存
      out = new FileOutputStream(imagePath + name);
      out.write(bytes);
      } catch (IOException exception) {
      exception.printStackTrace();
      }finally {
      if(out != null) {
      try {
      out.close();
      } catch (IOException e) {
      e.printStackTrace();
      }
      }
}
//这里要返回给操作者(HtmlDocumentFacade)一个存储的路径 用于生成Html时定位到图片资源
//⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️
//⚠️⚠️⚠️⚠️⚠️ 这里需要设置为前端能过直接访问到的图片地址, 列如作者:http://localhost:8000/resource/ueditor/file/20240404/1712220732312.png,
//⚠️⚠️⚠️⚠️⚠️ 否则,ueditor编辑器无法显示word文档中的图片
			return ueProperties.getShowpath().concat(imagePath.substring(imagePath.indexOf("ueditor"))).concat(name);
      }
      });
      //使用外观模式,将hwpfDocument文档对象设置给HtmlDocumentFacade中的Document属性
      converter.processDocument(hwpfDocument);
      //获取转换器中的document文档
      Document htmlDocument = converter.getDocument();
      //充当文档对象模型 (DOM) 树形式的转换源树的持有者 -- 源树
      DOMSource domSource = new DOMSource(htmlDocument);
      //转换器 该对象用于将源树转换为结果树
      Transformer transformer = TransformerFactory.newInstance().newTransformer();
      //设置输出时的以什么方式输出,也可说是结果树的文件类型 可以是html/xml/text或者是一些扩展前三者的扩展类型
      transformer.setOutputProperty(OutputKeys.METHOD , "html");
      //设置一些必要的属性 设置输出时候的编码为utf-8
      transformer.setOutputProperty(OutputKeys.ENCODING , "utf-8");
      //转换 将输入的源树转换为结果树并且输出到streamResult中
      transformer.transform(domSource , new StreamResult(new File(fileOutName)));
      log.info("html转换器 success");
      //处理生成的html,字符串形式给前端
      return readHtmlStr(fileOutName);
      } catch (Exception e) {
      log.error("docToHtmlText 异常", e);
      } finally {
      baos.close();
      outStream.close();
      }
      return null;
}
/**
* 读取html文件,转成字符串返回给前端
* 去除换行,以及连续两个空格
* @param htmlDirPath html文件路径
* @return
* @throws IOException
*/
private static String readHtmlStr(String htmlDirPath) throws IOException {
log.info("处理生成的html,字符串形式给前端:{} ...Start..", htmlDirPath);
String htmlStr = "";
try {
Path htmlPath = Paths.get(htmlDirPath);
htmlStr = new String(Files.readAllBytes(htmlPath));
htmlStr = htmlStr.replaceAll("n", "");
htmlStr = htmlStr.replaceAll("s{2,}", " ");
log.info("处理生成的html,字符串形式给前端。。。end");
} catch (IOException e) {
log.error("处理生成的html,字符串形式出错了, {}", e.getMessage());
}
return htmlStr;
}
}

6.后端配置文件

代码如下:

spring.application.name=ue
server.port=8000
##UE编辑器配置
#编辑器访问服务器的图片地址
ue.showpath=http://localhost:8000/resource
#ue文件存储路径前缀
ue.savepath=/Users/cookie/Documents/coding/uedemo
```
!!! ue.showpath=生产上有nginx需要在nginx.conf进行配置

三、实现效果

成功通过导入word文章,回显内容到ueditor编辑器

Ueditor二次编辑word(doc和docx格式),SpringBoot后端

总结

*赠人玫瑰,手留余香*

源码地址:

https://gitee.com/gwancookie/uedemo

**读取word文档生成html借鉴:**

https://blog.csdn.net/qq_44717657/article/details/124497326

© 版权声明
THE END
如果内容对您有所帮助,就支持一下吧!
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容