deason пре 7 година
родитељ
комит
30483ca705

+ 102 - 0
examcloud-core-questions-base/src/main/java/cn/com/qmth/examcloud/core/questions/base/ImageUtils.java

@@ -0,0 +1,102 @@
+/*
+ * *************************************************
+ * Copyright (c) 2018 QMTH. All Rights Reserved.
+ * Created by Deason on 2018-06-19 14:24:38.
+ * *************************************************
+ */
+
+package cn.com.qmth.examcloud.core.questions.base;
+
+import org.jsoup.Jsoup;
+import org.jsoup.nodes.Document;
+import org.jsoup.nodes.Element;
+import org.jsoup.select.Elements;
+
+public class ImageUtils {
+    private static final String TAG_IMG = "img";
+    private static final String UNIT = "px";
+    private static final int MAX_WIDTH = 350;
+    private static final int MAX_HEIGHT = 300;
+    private static final String STYLE_WIDTH = "width:%spx;";
+    private static final String STYLE_HEIGHT = "height:%spx;";
+
+    /**
+     * 如果图片尺寸过大,则限制图片最大宽度高度
+     */
+    public static String reSizeImg(String html) {
+        if (html == null) {
+            return "";
+        }
+        Document document = Jsoup.parse(html);
+        Elements elements = document.getElementsByTag(TAG_IMG);
+        for (Element element : elements) {
+            int width = ImageUtils.parseValue(element.attr("style"), "width");
+            int height = ImageUtils.parseValue(element.attr("style"), "height");
+            //System.out.println(width + " - " + height);
+            element.removeAttr("width");
+            element.removeAttr("height");
+            String widthStr = "";
+            String heightStr = "";
+            if (width > 0) {
+                if (width > MAX_WIDTH) {
+                    widthStr = String.format(STYLE_WIDTH, MAX_WIDTH);
+                } else {
+                    widthStr = String.format(STYLE_WIDTH, width);
+                }
+            }
+            if (height > 0) {
+                if (height > MAX_HEIGHT) {
+                    heightStr = String.format(STYLE_HEIGHT, MAX_HEIGHT);
+                } else {
+                    heightStr = String.format(STYLE_HEIGHT, height);
+                }
+            }
+            element.attr("style", widthStr + heightStr);
+        }
+        String bodyHtml = document.body().html();
+        //System.out.println(bodyHtml);
+        return bodyHtml;
+    }
+
+    public static int parseValue(String styleValue, String attrName) {
+        if (styleValue == null || "".equals(styleValue)) {
+            return 0;
+        }
+        try {
+            styleValue = styleValue.replaceAll(" ", "");
+            String[] attrs = styleValue.toLowerCase().split(";");
+            for (String attr : attrs) {
+                if (!attr.startsWith(attrName)) {
+                    continue;
+                }
+                String[] values = attr.split(":");
+                if (values.length > 1) {
+                    String value = values[1].toLowerCase().replaceAll(UNIT, "");
+                    return Integer.parseInt(value);
+                }
+            }
+        } catch (Exception e) {
+            //do nothing
+        }
+        return 0;
+    }
+
+    public static int parseValue(String attrValue) {
+        if (attrValue == null || "".equals(attrValue)) {
+            return 0;
+        }
+        try {
+            attrValue = attrValue.replaceAll(" ", "");
+            String value = attrValue.toLowerCase().replaceAll(UNIT, "");
+            return Integer.parseInt(value);
+        } catch (Exception e) {
+            return 0;
+        }
+    }
+
+    public static void main(String[] args) {
+        String html = "<p><IMG src=\"abc\" style=\"width: 500PX; height:500PX;\"/><img src=\"xyz\" style=\"width:300px;\"/></p>";
+        ImageUtils.reSizeImg(html);
+    }
+
+}

+ 3 - 1
examcloud-core-questions-service/src/main/java/cn/com/qmth/examcloud/core/questions/service/impl/ExtractConfigFileServiceImpl.java

@@ -13,6 +13,7 @@ import java.util.Set;
 
 import javax.servlet.http.HttpServletResponse;
 
+import cn.com.qmth.examcloud.core.questions.base.ImageUtils;
 import main.java.com.UpYun;
 
 import org.apache.commons.io.FileUtils;
@@ -283,7 +284,8 @@ public class ExtractConfigFileServiceImpl implements ExtractConfigFileService {
 		//1.考试说明html转成word
 		String title = "<p style=\"text-align:center\"><span style=\"font-size:26px\"><span style=\"font-family:宋体\">考&nbsp;试&nbsp;说&nbsp;明</span></span></p>";
 		WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
-		DocxProcessUtil.html2Docx(wordMLPackage, CommonUtils.formatHtml(title+paper.getExamRemark()));
+		String html = title + ImageUtils.reSizeImg(paper.getExamRemark());
+		DocxProcessUtil.html2Docx(wordMLPackage, CommonUtils.formatHtml(html));
 		//2.导出考试说明word	
 		File file = new File(zipFileName+File.separator+paper.getCourse().getCode()+TEMP_FILE_NAME);
 		Docx4J.save(wordMLPackage, file);