|
|
@@ -0,0 +1,80 @@
|
|
|
+package space.core.apps.sys.service;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import org.jsoup.Jsoup;
|
|
|
+import org.jsoup.nodes.Document;
|
|
|
+import org.jsoup.select.Elements;
|
|
|
+
|
|
|
+import space.core.apps.common.BaseTransaction;
|
|
|
+import space.core.apps.sys.utils.LibreOfficeUtil;
|
|
|
+import space.core.apps.sys.utils.UploadFileUtil;
|
|
|
+import space.core.common.utils.FileUtil;
|
|
|
+import space.core.common.utils.GsonUtil;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 将后台已上传的 Word 文档转换为可直接插入富文本编辑器的 HTML。
|
|
|
+ */
|
|
|
+public class WordImportService extends BaseTransaction {
|
|
|
+ private static final long serialVersionUID = 1L;
|
|
|
+ private static final String UPLOAD_PREFIX = "/web/upload/modules/";
|
|
|
+
|
|
|
+ public void convertJson() throws IOException {
|
|
|
+ Map<String, Object> result = new HashMap<String, Object>();
|
|
|
+ getResponse().setContentType("application/json;charset=UTF-8");
|
|
|
+
|
|
|
+ try {
|
|
|
+ String path = getParameter("path");
|
|
|
+ String lowerPath = path == null ? "" : path.toLowerCase();
|
|
|
+ if (path == null || !path.startsWith(UPLOAD_PREFIX)
|
|
|
+ || path.contains("..") || !lowerPath.matches(".*\\.(doc|docx)$")) {
|
|
|
+ throw new IllegalArgumentException("无效的 Word 文件路径");
|
|
|
+ }
|
|
|
+
|
|
|
+ String webRoot = getWebRootPath();
|
|
|
+ File sourceFile = new File(webRoot + path);
|
|
|
+ if (!sourceFile.isFile()) {
|
|
|
+ throw new IllegalArgumentException("Word 文件不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ String extension = FileUtil.getFileExt(sourceFile.getName());
|
|
|
+ String targetRelativePath = UploadFileUtil.getUploadFilePath(extension);
|
|
|
+ File targetDir = new File(webRoot + targetRelativePath);
|
|
|
+ if (!targetDir.exists() && !targetDir.mkdirs()) {
|
|
|
+ throw new IOException("无法创建 Word 转换目录");
|
|
|
+ }
|
|
|
+
|
|
|
+ String htmlFileName = LibreOfficeUtil.convertHtml(sourceFile.getAbsolutePath(), targetDir.getAbsolutePath());
|
|
|
+ File htmlFile = new File(targetDir, htmlFileName);
|
|
|
+ String htmlContent = normalizeHtml(htmlFile, targetRelativePath);
|
|
|
+ FileUtil.writeFile(htmlFile.getAbsolutePath(), htmlContent);
|
|
|
+
|
|
|
+ result.put("code", "success");
|
|
|
+ result.put("msg", "转换成功");
|
|
|
+ result.put("htmlContent", htmlContent);
|
|
|
+ result.put("filePath", targetRelativePath + htmlFileName);
|
|
|
+ } catch (Exception e) {
|
|
|
+ result.put("code", "fail");
|
|
|
+ result.put("msg", "Word 转换失败,请确认文件未损坏且格式为 .doc 或 .docx");
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ getWriter().write(GsonUtil.toJson(result));
|
|
|
+ }
|
|
|
+
|
|
|
+ private String normalizeHtml(File htmlFile, String targetRelativePath) throws Exception {
|
|
|
+ Document document = Jsoup.parse(htmlFile, "UTF-8", "");
|
|
|
+ Elements images = document.select("img[src]");
|
|
|
+ for (org.jsoup.nodes.Element image : images) {
|
|
|
+ String src = image.attr("src");
|
|
|
+ if (src.length() > 0 && !src.startsWith("/") && !src.startsWith("data:")
|
|
|
+ && !src.startsWith("http://") && !src.startsWith("https://")) {
|
|
|
+ image.attr("src", targetRelativePath + src);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return document.select("body").eq(0).html();
|
|
|
+ }
|
|
|
+}
|