浏览代码

url图片导出数据包正确转换为明确格式的base64

xiatian 4 月之前
父节点
当前提交
621e769f9e

+ 112 - 108
examcloud-core-questions-base/pom.xml

@@ -1,109 +1,113 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
-    <modelVersion>4.0.0</modelVersion>
-    <artifactId>examcloud-core-questions-base</artifactId>
-    <packaging>jar</packaging>
-
-    <parent>
-        <groupId>cn.com.qmth.examcloud</groupId>
-        <artifactId>examcloud-core-questions</artifactId>
-        <version>${revision}</version>
-    </parent>
-
-    <dependencies>
-        <dependency>
-            <groupId>cn.com.qmth.examcloud</groupId>
-            <artifactId>examcloud-support</artifactId>
-            <version>${project.version}</version>
-        </dependency>
-        <dependency>
-            <groupId>cn.com.qmth.examcloud.rpc</groupId>
-            <artifactId>examcloud-global-api-client</artifactId>
-            <version>${project.version}</version>
-        </dependency>
-        <dependency>
-            <groupId>cn.com.qmth.examcloud.rpc</groupId>
-            <artifactId>examcloud-core-basic-api-client</artifactId>
-            <version>${project.version}</version>
-        </dependency>
-        <dependency>
-            <groupId>cn.com.qmth.examcloud.rpc</groupId>
-            <artifactId>examcloud-core-examwork-api-client</artifactId>
-            <version>${project.version}</version>
-        </dependency>
-        <dependency>
-            <groupId>cn.com.qmth.examcloud.rpc</groupId>
-            <artifactId>examcloud-core-print-api-client</artifactId>
-            <version>${project.version}</version>
-        </dependency>
-        <dependency>
-            <groupId>cn.com.qmth.examcloud.rpc</groupId>
-            <artifactId>examcloud-core-oe-admin-api-client</artifactId>
-            <version>${project.version}</version>
-        </dependency>
-        <dependency>
-            <groupId>cn.com.qmth.examcloud.rpc</groupId>
-            <artifactId>examcloud-core-oe-student-api-client</artifactId>
-            <version>${project.version}</version>
-        </dependency>
-        <dependency>
-            <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-starter-data-mongodb</artifactId>
-            <exclusions>
-                <exclusion>
-                    <groupId>org.springframework.boot</groupId>
-                    <artifactId>spring-boot-starter-logging</artifactId>
-                </exclusion>
-            </exclusions>
-        </dependency>
-        <dependency>
-            <groupId>com.upyun</groupId>
-            <artifactId>java-sdk</artifactId>
-        </dependency>
-        <dependency>
-            <groupId>org.docx4j</groupId>
-            <artifactId>docx4j-ImportXHTML</artifactId>
-            <exclusions>
-                <exclusion>
-                    <groupId>org.slf4j</groupId>
-                    <artifactId>slf4j-api</artifactId>
-                </exclusion>
-                <exclusion>
-                    <groupId>org.slf4j</groupId>
-                    <artifactId>slf4j-log4j12</artifactId>
-                </exclusion>
-                <exclusion>
-                    <groupId>org.slf4j</groupId>
-                    <artifactId>jcl-over-slf4j</artifactId>
-                </exclusion>
-            </exclusions>
-        </dependency>
-        <dependency>
-            <groupId>net.sourceforge.jeuclid</groupId>
-            <artifactId>jeuclid-core</artifactId>
-            <exclusions>
-                <exclusion>
-                    <groupId>org.apache.xmlgraphics</groupId>
-                    <artifactId>xmlgraphics-commons</artifactId>
-                </exclusion>
-                <exclusion>
-                    <groupId>xml-apis</groupId>
-                    <artifactId>xml-apis</artifactId>
-                </exclusion>
-            </exclusions>
-        </dependency>
-        <dependency>
-            <groupId>net.sf.saxon</groupId>
-            <artifactId>Saxon-HE</artifactId>
-        </dependency>
-        <dependency>
-            <groupId>org.ansj</groupId>
-            <artifactId>ansj_seg</artifactId>
-        </dependency>
-        <dependency>
-            <groupId>com.esotericsoftware</groupId>
-            <artifactId>reflectasm</artifactId>
-        </dependency>
-    </dependencies>
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <artifactId>examcloud-core-questions-base</artifactId>
+    <packaging>jar</packaging>
+
+    <parent>
+        <groupId>cn.com.qmth.examcloud</groupId>
+        <artifactId>examcloud-core-questions</artifactId>
+        <version>${revision}</version>
+    </parent>
+
+    <dependencies>
+        <dependency>
+            <groupId>cn.com.qmth.examcloud</groupId>
+            <artifactId>examcloud-support</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>cn.com.qmth.examcloud.rpc</groupId>
+            <artifactId>examcloud-global-api-client</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>cn.com.qmth.examcloud.rpc</groupId>
+            <artifactId>examcloud-core-basic-api-client</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>cn.com.qmth.examcloud.rpc</groupId>
+            <artifactId>examcloud-core-examwork-api-client</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>cn.com.qmth.examcloud.rpc</groupId>
+            <artifactId>examcloud-core-print-api-client</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>cn.com.qmth.examcloud.rpc</groupId>
+            <artifactId>examcloud-core-oe-admin-api-client</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>cn.com.qmth.examcloud.rpc</groupId>
+            <artifactId>examcloud-core-oe-student-api-client</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-data-mongodb</artifactId>
+            <exclusions>
+                <exclusion>
+                    <groupId>org.springframework.boot</groupId>
+                    <artifactId>spring-boot-starter-logging</artifactId>
+                </exclusion>
+            </exclusions>
+        </dependency>
+        <dependency>
+            <groupId>com.upyun</groupId>
+            <artifactId>java-sdk</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.docx4j</groupId>
+            <artifactId>docx4j-ImportXHTML</artifactId>
+            <exclusions>
+                <exclusion>
+                    <groupId>org.slf4j</groupId>
+                    <artifactId>slf4j-api</artifactId>
+                </exclusion>
+                <exclusion>
+                    <groupId>org.slf4j</groupId>
+                    <artifactId>slf4j-log4j12</artifactId>
+                </exclusion>
+                <exclusion>
+                    <groupId>org.slf4j</groupId>
+                    <artifactId>jcl-over-slf4j</artifactId>
+                </exclusion>
+            </exclusions>
+        </dependency>
+        <dependency>
+            <groupId>net.sourceforge.jeuclid</groupId>
+            <artifactId>jeuclid-core</artifactId>
+            <exclusions>
+                <exclusion>
+                    <groupId>org.apache.xmlgraphics</groupId>
+                    <artifactId>xmlgraphics-commons</artifactId>
+                </exclusion>
+                <exclusion>
+                    <groupId>xml-apis</groupId>
+                    <artifactId>xml-apis</artifactId>
+                </exclusion>
+            </exclusions>
+        </dependency>
+        <dependency>
+            <groupId>net.sf.saxon</groupId>
+            <artifactId>Saxon-HE</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.ansj</groupId>
+            <artifactId>ansj_seg</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>com.esotericsoftware</groupId>
+            <artifactId>reflectasm</artifactId>
+        </dependency>
+        <dependency>
+			<groupId>org.apache.tika</groupId>
+			<artifactId>tika-core</artifactId>
+		</dependency>
+    </dependencies>
 </project>

+ 45 - 2
examcloud-core-questions-base/src/main/java/cn/com/qmth/examcloud/core/questions/base/converter/utils/FileUtil.java

@@ -20,6 +20,7 @@ import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
+import java.net.URL;
 import java.nio.charset.Charset;
 import java.text.SimpleDateFormat;
 import java.util.Date;
@@ -32,7 +33,9 @@ import java.util.zip.ZipFile;
 import java.util.zip.ZipOutputStream;
 
 import org.apache.commons.codec.binary.Base64;
+import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.IOUtils;
+import org.apache.tika.Tika;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -42,11 +45,39 @@ public class FileUtil {
 
     private static final Logger log = LoggerFactory.getLogger(FileUtil.class);
 
-    public static String fileToBase64(InputStream is) {
+    public static String imageUrlToBase64(String src) {
+        File image = null;
+        try {
+            URL url = new URL(src);
+            InputStream is = url.openStream();
+            String newFileName = UUID.randomUUID().toString();
+            image = new File("temp/" + newFileName);
+            if (!image.getParentFile().exists()) {
+                image.mkdirs();
+            }
+            FileUtils.copyInputStreamToFile(is, image);
+            String imageType = checkImageType(image);
+            newFileName = newFileName + "." + imageType;
+            image.renameTo(new File("temp/" + newFileName));
+            return fileToBase64(image);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        } finally {
+            if (image != null) {
+                image.delete();
+            }
+        }
+    }
+
+    public static String fileToBase64(File imgFile) {
+        String fileName = imgFile.getName();
+        String suff = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
+        InputStream is = null;
         byte[] base64Byte;
         try {
             base64Byte = new byte[0];
             byte[] imgByte;
+            is = new FileInputStream(imgFile);
             imgByte = IOUtils.toByteArray(is);
             base64Byte = Base64.encodeBase64(imgByte);
         } catch (IOException e) {
@@ -59,7 +90,19 @@ public class FileUtil {
                 }
             }
         }
-        return "data:image/jpeg;base64," + new String(base64Byte);
+        return "data:image/" + suff + ";base64," + new String(base64Byte);
+    }
+
+    public static String checkImageType(File file) {
+        Tika tika = new Tika();
+        String mimeType = null;
+        try {
+            mimeType = tika.detect(file);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+        String type = mimeType.substring(mimeType.indexOf("/") + 1);
+        return type;
     }
 
     /**

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

@@ -4,8 +4,6 @@ import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
-import java.io.InputStream;
-import java.net.URL;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -708,13 +706,7 @@ public class ExportThemisPaperServiceImpl implements ExportThemisPaperService {
         if (!src.startsWith("http://") && !src.startsWith("https://")) {
             return;
         }
-        try {
-            URL url = new URL(src);
-            InputStream is = url.openStream();
-            block.setValue(FileUtil.fileToBase64(is));
-        } catch (IOException e) {
-            throw new StatusException("500", e.getMessage(), e);
-        }
+        block.setValue(FileUtil.imageUrlToBase64(src));
     }
 
     /**

+ 30 - 22
pom.xml

@@ -1,23 +1,31 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
-    <modelVersion>4.0.0</modelVersion>
-    <artifactId>examcloud-core-questions</artifactId>
-    <packaging>pom</packaging>
-
-    <parent>
-        <groupId>cn.com.qmth.examcloud</groupId>
-        <artifactId>examcloud-parent</artifactId>
-        <version>${revision}</version>
-        <relativePath>../examcloud-parent/pom.xml</relativePath>
-    </parent>
-
-    <modules>
-        <module>examcloud-core-questions-base</module>
-        <module>examcloud-core-questions-dao</module>
-        <module>examcloud-core-questions-service</module>
-        <module>examcloud-core-questions-api-provider</module>
-        <module>examcloud-core-questions-starter</module>
-    </modules>
-
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <artifactId>examcloud-core-questions</artifactId>
+    <packaging>pom</packaging>
+
+    <parent>
+        <groupId>cn.com.qmth.examcloud</groupId>
+        <artifactId>examcloud-parent</artifactId>
+        <version>${revision}</version>
+        <relativePath>../examcloud-parent/pom.xml</relativePath>
+    </parent>
+
+    <modules>
+        <module>examcloud-core-questions-base</module>
+        <module>examcloud-core-questions-dao</module>
+        <module>examcloud-core-questions-service</module>
+        <module>examcloud-core-questions-api-provider</module>
+        <module>examcloud-core-questions-starter</module>
+    </modules>
+     <dependencyManagement>
+	        <dependencies>
+				<dependency>
+					<groupId>org.apache.tika</groupId>
+					<artifactId>tika-core</artifactId>
+					<version>1.18</version>
+				</dependency>
+			</dependencies>
+		</dependencyManagement>   
 </project>