Procházet zdrojové kódy

导出数据包url图片转base64

xiatian před 4 měsíci
rodič
revize
85d69ede16

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

@@ -1,480 +1,521 @@
-/*
- * *************************************************
- * Copyright (c) 2018 QMTH. All Rights Reserved.
- * Created by Deason on 2018-07-12 15:31:10.
- * *************************************************
- */
-
-package cn.com.qmth.examcloud.core.questions.base.converter.utils;
-
-import cn.com.qmth.examcloud.core.questions.base.IoUtils;
-import org.apache.commons.io.IOUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.io.*;
-import java.nio.charset.Charset;
-import java.text.SimpleDateFormat;
-import java.util.*;
-import java.util.zip.ZipEntry;
-import java.util.zip.ZipFile;
-import java.util.zip.ZipOutputStream;
-
-public class FileUtil {
-    private static final Logger log = LoggerFactory.getLogger(FileUtil.class);
-
-    /**
-     * 分隔文件
-     *
-     * @param sourcePath 原文件
-     * @param targetPath 目标文件
-     * @param n          跳过的字节数
-     * @return
-     */
-    public static File cutFile(String sourcePath, String targetPath, int n) {
-        File file = new File(sourcePath);
-        File newFile = new File(targetPath);
-
-        try (
-                FileInputStream fis = new FileInputStream(file);
-                InputStream is = new BufferedInputStream(fis);
-                OutputStream os = new FileOutputStream(newFile);
-        ) {
-
-            //从n个字节开始读,注意中文是两个字节
-            fis.skip(n);
-
-            //指定文件位置读取的文件流,存入新文件
-            byte buffer[] = new byte[4 * 1024];
-            int len;
-            while ((len = is.read(buffer)) != -1) {
-                os.write(buffer, 0, len);
-            }
-
-            os.flush();
-            return newFile;
-        } catch (FileNotFoundException e) {
-            log.error(e.getMessage(), e);
-        } catch (IOException e) {
-            log.error(e.getMessage(), e);
-        }
-        return null;
-    }
-
-    /**
-     * 读取文件前面部分N个字节
-     *
-     * @param path       文件路径
-     * @param headerSize 头信息字节数(必须2的倍数)
-     * @param signSize   签名信息字节数
-     * @return
-     */
-    public static String[] readFileHeader(String path, int headerSize, int signSize) {
-        int n = headerSize / 2;
-        String[] codes = new String[n + 1];
-
-        File file = new File(path);
-        try (
-                FileInputStream fis = new FileInputStream(file);
-                DataInputStream ois = new DataInputStream(fis);
-        ) {
-            //分n次读取文件(n * 2)个字节
-            for (int i = 0; i < n; i++) {
-                codes[i] = String.valueOf(ois.readShort());
-            }
-
-            if (signSize > 0) {
-                StringBuilder ss = new StringBuilder();
-                for (int i = 0; i < signSize; i++) {
-                    ss.append((char) ois.readByte());
-                }
-                codes[2] = ss.toString();
-            }
-        } catch (FileNotFoundException e) {
-            log.error(e.getMessage(), e);
-        } catch (IOException e) {
-            log.error(e.getMessage(), e);
-        }
-
-        return codes;
-    }
-
-    /**
-     * 读取文件内容
-     *
-     * @param file
-     * @return
-     */
-    public static String readFileContent(File file) {
-        StringBuilder content = new StringBuilder();
-        InputStreamReader streamReader = null;
-        BufferedReader bufferedReader = null;
-        try {
-            String encoding = "UTF-8";
-            if (file.exists() && file.isFile()) {
-                streamReader = new InputStreamReader(new FileInputStream(file), encoding);
-                bufferedReader = new BufferedReader(streamReader);
-                String line;
-                while ((line = bufferedReader.readLine()) != null) {
-                    content.append(line);
-                }
-            }
-        } catch (Exception e) {
-            log.error(e.getMessage(), e);
-        } finally {
-            IOUtils.closeQuietly(streamReader);
-            IOUtils.closeQuietly(bufferedReader);
-        }
-        return content.toString();
-    }
-
-    /**
-     * 在文件流前面追加头信息和签名信息,并生成新的“.tk”文件
-     */
-    public static boolean appendHeader(File file, short[] headers, String sign) {
-        if (file == null || !file.exists()) {
-            return false;
-        }
-
-        if (!file.isFile()) {
-            return false;
-        }
-
-        FileInputStream fis = null;
-        InputStream is = null;
-        FileOutputStream fos = null;
-        DataOutputStream dos = null;
-        try {
-            //创建临时文件
-            String baseFilePath = file.getAbsolutePath();
-            String targetFilePath = getFilePathName(baseFilePath) + ".tk";
-            File newFile = new File(targetFilePath);
-            fos = new FileOutputStream(newFile);
-            dos = new DataOutputStream(fos);
-
-            //写入头信息
-            for (short s : headers) {
-                dos.writeShort(s);
-            }
-            if (sign != null && !"".equals(sign)) {
-                //写入签名信息
-                dos.write(sign.getBytes("ISO-8859-1"));
-            }
-
-            //在临时文件中追加原始文件内容
-            fis = new FileInputStream(file);
-            is = new BufferedInputStream(fis);
-
-            byte buffer[] = new byte[4 * 1024];
-            int len;
-            while ((len = is.read(buffer)) != -1) {
-                dos.write(buffer, 0, len);
-            }
-            dos.flush();
-            return true;
-        } catch (FileNotFoundException e) {
-            log.error(e.getMessage(), e);
-        } catch (IOException e) {
-            log.error(e.getMessage(), e);
-        } finally {
-            IOUtils.closeQuietly(is);
-            IOUtils.closeQuietly(fis);
-            IOUtils.closeQuietly(dos);
-            IOUtils.closeQuietly(fos);
-        }
-        return false;
-    }
-
-    /**
-     * 生成日期目录路径
-     */
-    public static String generateDateDir() {
-        return "/" + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + "/";
-    }
-
-    public static String generateFileName() {
-        return UUID.randomUUID().toString().replaceAll("-", "");
-    }
-
-    public static String generateDateName() {
-        return new SimpleDateFormat("yyMMddHHmmss").format(new Date());
-    }
-
-    /**
-     * 获取文件后缀名(包含".")
-     */
-    public static String getFileSuffix(String fileName) {
-        if (fileName == null) {
-            return "";
-        }
-        int index = fileName.lastIndexOf(".");
-        if (index > -1) {
-            return fileName.substring(index).toLowerCase();
-        }
-        return "";
-    }
-
-    /**
-     * 获取无后缀的文件名
-     *
-     * @param fileName 示例:../xxx/abc.xx
-     * @return 示例:../xxx/abc
-     */
-    public static String getFilePathName(String fileName) {
-        if (fileName != null && fileName.length() > 0) {
-            int index = fileName.lastIndexOf(".");
-            if (index != -1) {
-                return fileName.substring(0, index);
-            }
-        }
-        return "";
-    }
-
-    /**
-     * 创建文件目录
-     */
-    public static boolean makeDirs(String path) {
-        if (path == null || "".equals(path)) {
-            return false;
-        }
-        File folder = new File(path);
-        if (!folder.exists()) {
-            return folder.mkdirs();
-        }
-        return true;
-    }
-
-    /**
-     * 保存字符串到文件中
-     */
-    public static void saveAsFile(String path, String content) {
-        saveAsFile(path, content, null);
-    }
-
-    public static void saveAsFile(String path, String content, String encoding) {
-        if (path == null || content == null) {
-            return;
-        }
-
-        if (encoding == null) {
-            encoding = "UTF-8";
-        }
-
-        File file = new File(path);
-        if (!file.exists()) {
-            if (FileUtil.makeDirs(file.getParent())) {
-                boolean ok = IoUtils.createFile(file);
-                if (!ok) {
-                    throw new RuntimeException("文件创建失败!");
-                }
-            }
-        }
-
-        try (
-                FileOutputStream fos = new FileOutputStream(file);
-                OutputStreamWriter write = new OutputStreamWriter(fos, encoding);
-                BufferedWriter bw = new BufferedWriter(write);
-        ) {
-            bw.write(content);
-            bw.flush();
-            log.info("save as file success. " + path);
-        } catch (IOException e) {
-            log.error("save as file error. " + path);
-        }
-    }
-
-    /**
-     * 解压文件
-     *
-     * @param targetDir 解压目录
-     * @param zipFile   待解压的ZIP文件
-     */
-    public static List<File> unZip(File targetDir, File zipFile) {
-        if (targetDir == null) {
-            log.error("解压目录不能为空!");
-            return null;
-        }
-
-        if (zipFile == null) {
-            log.error("待解压的文件不能为空!");
-            return null;
-        }
-
-        if (!zipFile.exists()) {
-            log.error("待解压的文件不存在!" + zipFile.getAbsolutePath());
-            return null;
-        }
-
-        String zipName = zipFile.getName().toLowerCase();
-        if (zipFile.isDirectory() || zipName.indexOf(".zip") < 0) {
-            log.error("待解压的文件格式错误!");
-            return null;
-        }
-
-        if (!targetDir.exists()) {
-            targetDir.mkdir();
-        }
-
-        List<File> result = new LinkedList<>();
-
-        try (ZipFile zip = new ZipFile(zipFile, Charset.forName("UTF-8"));) {
-
-            Enumeration entries = zip.entries();
-            while (entries.hasMoreElements()) {
-                ZipEntry entry = (ZipEntry) entries.nextElement();
-
-                //Linux中需要替换掉路径的反斜杠
-                String entryName = (File.separator + entry.getName()).replaceAll("\\\\", "/");
-
-                String filePath = targetDir.getAbsolutePath() + entryName;
-                File target = new File(filePath);
-                if (entry.isDirectory()) {
-                    target.mkdirs();
-                } else {
-                    File dir = target.getParentFile();
-                    if (!dir.exists()) {
-                        dir.mkdirs();
-                    }
-
-                    try (OutputStream os = new FileOutputStream(target);
-                         InputStream is = zip.getInputStream(entry);) {
-                        IOUtils.copy(is, os);
-                        os.flush();
-                    } catch (IOException e) {
-                        log.error(e.getMessage(), e);
-                    }
-                    result.add(target);
-                }
-            }
-
-        } catch (IOException e) {
-            log.error(e.getMessage(), e);
-        }
-
-        return result;
-    }
-
-    /**
-     * 文件压缩
-     *
-     * @param target  目录或文件
-     * @param zipFile 压缩后的ZIP文件
-     */
-    public static boolean doZip(File target, File zipFile) {
-        if (target == null || !target.exists()) {
-            log.error("目录或文件不能为空!");
-            return false;
-        }
-
-        if (zipFile == null) {
-            log.error("待压缩的文件不能为空!");
-            return false;
-        }
-
-        try (
-                OutputStream outStream = new FileOutputStream(zipFile);
-                ZipOutputStream zipOutStream = new ZipOutputStream(outStream, Charset.forName("UTF-8"));
-        ) {
-            if (!zipFile.exists()) {
-                boolean ok = zipFile.createNewFile();
-                if (!ok) {
-                    log.error("压缩的文件创建失败!");
-                    return false;
-                }
-            }
-
-            if (target.isDirectory()) {
-                File[] files = target.listFiles();
-                if (files.length == 0) {
-                    log.error("文件夹内未找到任何文件!");
-                    return false;
-                }
-
-                for (File file : files) {
-                    doZip(zipOutStream, file, null);
-                }
-            } else {
-                doZip(zipOutStream, target, null);
-            }
-        } catch (IOException e) {
-            log.error(e.getMessage(), e);
-        }
-
-        return true;
-    }
-
-    private static void doZip(ZipOutputStream zipOutStream, File target, String parentDir) throws IOException {
-        //log.info("Zip:" + parentDir);
-        if (parentDir == null) {
-            parentDir = "";
-        }
-
-        if (!"".equals(parentDir) && !parentDir.endsWith(File.separator)) {
-            parentDir += File.separator;
-        }
-
-        if (target.isDirectory()) {
-            File[] files = target.listFiles();
-            if (files.length > 0) {
-                for (File file : files) {
-                    doZip(zipOutStream, file, parentDir + target.getName());
-                }
-            } else {
-                zipOutStream.putNextEntry(new ZipEntry(parentDir + target.getName()));
-                zipOutStream.closeEntry();
-            }
-        } else {
-            try (InputStream is = new FileInputStream(target);) {
-                zipOutStream.putNextEntry(new ZipEntry(parentDir + target.getName()));
-                int len;
-                byte[] bytes = new byte[1024];
-                while ((len = is.read(bytes)) > 0) {
-                    zipOutStream.write(bytes, 0, len);
-                }
-            } catch (IOException e) {
-                log.error(e.getMessage(), e);
-            }
-            zipOutStream.closeEntry();
-        }
-    }
-    public static void deleteFolder(String path) {
-
-		File file = new File(path);
-		if (file.exists()) {
-			if (file.isFile()) {
-				deleteFile(path);
-			} else {
-				deleteDirectory(path);
-			}
-		}
-	}
-
-	public static void deleteFile(String path) {
-		File file = new File(path);
-		if (file.isFile() && file.exists()) {
-			file.delete();
-		}
-	}
-
-	public static void deleteDirectory(String path) {
-		if (!path.endsWith(File.separator)) {
-			path = path + File.separator;
-		}
-		File dirFile = new File(path);
-		if (!dirFile.exists() || !dirFile.isDirectory()) {
-			return;
-		}
-		File[] files = dirFile.listFiles();
-		if (files != null) {
-			for (int i = 0; i < files.length; i++) {
-				if (files[i].isFile()) {
-					deleteFile(files[i].getAbsolutePath());
-				} else {
-					deleteDirectory(files[i].getAbsolutePath());
-				}
-			}
-		}
-
-		dirFile.delete();
-	}
+/*
+ * ************************************************* Copyright (c) 2018 QMTH.
+ * All Rights Reserved. Created by Deason on 2018-07-12 15:31:10.
+ * *************************************************
+ */
+
+package cn.com.qmth.examcloud.core.questions.base.converter.utils;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.nio.charset.Charset;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Enumeration;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.UUID;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
+import java.util.zip.ZipOutputStream;
+
+import org.apache.commons.codec.binary.Base64;
+import org.apache.commons.io.IOUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import cn.com.qmth.examcloud.core.questions.base.IoUtils;
+
+public class FileUtil {
+
+    private static final Logger log = LoggerFactory.getLogger(FileUtil.class);
+
+    public static String fileToBase64(InputStream is) {
+        byte[] base64Byte;
+        try {
+            base64Byte = new byte[0];
+            byte[] imgByte;
+            imgByte = IOUtils.toByteArray(is);
+            base64Byte = Base64.encodeBase64(imgByte);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        } finally {
+            if (is != null) {
+                try {
+                    is.close();
+                } catch (IOException e) {
+                }
+            }
+        }
+        return "data:image/jpeg;base64," + new String(base64Byte);
+    }
+
+    /**
+     * 分隔文件
+     *
+     * @param sourcePath
+     *            原文件
+     * @param targetPath
+     *            目标文件
+     * @param n
+     *            跳过的字节数
+     * @return
+     */
+    public static File cutFile(String sourcePath, String targetPath, int n) {
+        File file = new File(sourcePath);
+        File newFile = new File(targetPath);
+
+        try (FileInputStream fis = new FileInputStream(file);
+                InputStream is = new BufferedInputStream(fis);
+                OutputStream os = new FileOutputStream(newFile);) {
+
+            // 从n个字节开始读,注意中文是两个字节
+            fis.skip(n);
+
+            // 指定文件位置读取的文件流,存入新文件
+            byte buffer[] = new byte[4 * 1024];
+            int len;
+            while ((len = is.read(buffer)) != -1) {
+                os.write(buffer, 0, len);
+            }
+
+            os.flush();
+            return newFile;
+        } catch (FileNotFoundException e) {
+            log.error(e.getMessage(), e);
+        } catch (IOException e) {
+            log.error(e.getMessage(), e);
+        }
+        return null;
+    }
+
+    /**
+     * 读取文件前面部分N个字节
+     *
+     * @param path
+     *            文件路径
+     * @param headerSize
+     *            头信息字节数(必须2的倍数)
+     * @param signSize
+     *            签名信息字节数
+     * @return
+     */
+    public static String[] readFileHeader(String path, int headerSize, int signSize) {
+        int n = headerSize / 2;
+        String[] codes = new String[n + 1];
+
+        File file = new File(path);
+        try (FileInputStream fis = new FileInputStream(file); DataInputStream ois = new DataInputStream(fis);) {
+            // 分n次读取文件(n * 2)个字节
+            for (int i = 0; i < n; i++) {
+                codes[i] = String.valueOf(ois.readShort());
+            }
+
+            if (signSize > 0) {
+                StringBuilder ss = new StringBuilder();
+                for (int i = 0; i < signSize; i++) {
+                    ss.append((char) ois.readByte());
+                }
+                codes[2] = ss.toString();
+            }
+        } catch (FileNotFoundException e) {
+            log.error(e.getMessage(), e);
+        } catch (IOException e) {
+            log.error(e.getMessage(), e);
+        }
+
+        return codes;
+    }
+
+    /**
+     * 读取文件内容
+     *
+     * @param file
+     * @return
+     */
+    public static String readFileContent(File file) {
+        StringBuilder content = new StringBuilder();
+        InputStreamReader streamReader = null;
+        BufferedReader bufferedReader = null;
+        try {
+            String encoding = "UTF-8";
+            if (file.exists() && file.isFile()) {
+                streamReader = new InputStreamReader(new FileInputStream(file), encoding);
+                bufferedReader = new BufferedReader(streamReader);
+                String line;
+                while ((line = bufferedReader.readLine()) != null) {
+                    content.append(line);
+                }
+            }
+        } catch (Exception e) {
+            log.error(e.getMessage(), e);
+        } finally {
+            IOUtils.closeQuietly(streamReader);
+            IOUtils.closeQuietly(bufferedReader);
+        }
+        return content.toString();
+    }
+
+    /**
+     * 在文件流前面追加头信息和签名信息,并生成新的“.tk”文件
+     */
+    public static boolean appendHeader(File file, short[] headers, String sign) {
+        if (file == null || !file.exists()) {
+            return false;
+        }
+
+        if (!file.isFile()) {
+            return false;
+        }
+
+        FileInputStream fis = null;
+        InputStream is = null;
+        FileOutputStream fos = null;
+        DataOutputStream dos = null;
+        try {
+            // 创建临时文件
+            String baseFilePath = file.getAbsolutePath();
+            String targetFilePath = getFilePathName(baseFilePath) + ".tk";
+            File newFile = new File(targetFilePath);
+            fos = new FileOutputStream(newFile);
+            dos = new DataOutputStream(fos);
+
+            // 写入头信息
+            for (short s : headers) {
+                dos.writeShort(s);
+            }
+            if (sign != null && !"".equals(sign)) {
+                // 写入签名信息
+                dos.write(sign.getBytes("ISO-8859-1"));
+            }
+
+            // 在临时文件中追加原始文件内容
+            fis = new FileInputStream(file);
+            is = new BufferedInputStream(fis);
+
+            byte buffer[] = new byte[4 * 1024];
+            int len;
+            while ((len = is.read(buffer)) != -1) {
+                dos.write(buffer, 0, len);
+            }
+            dos.flush();
+            return true;
+        } catch (FileNotFoundException e) {
+            log.error(e.getMessage(), e);
+        } catch (IOException e) {
+            log.error(e.getMessage(), e);
+        } finally {
+            IOUtils.closeQuietly(is);
+            IOUtils.closeQuietly(fis);
+            IOUtils.closeQuietly(dos);
+            IOUtils.closeQuietly(fos);
+        }
+        return false;
+    }
+
+    /**
+     * 生成日期目录路径
+     */
+    public static String generateDateDir() {
+        return "/" + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + "/";
+    }
+
+    public static String generateFileName() {
+        return UUID.randomUUID().toString().replaceAll("-", "");
+    }
+
+    public static String generateDateName() {
+        return new SimpleDateFormat("yyMMddHHmmss").format(new Date());
+    }
+
+    /**
+     * 获取文件后缀名(包含".")
+     */
+    public static String getFileSuffix(String fileName) {
+        if (fileName == null) {
+            return "";
+        }
+        int index = fileName.lastIndexOf(".");
+        if (index > -1) {
+            return fileName.substring(index).toLowerCase();
+        }
+        return "";
+    }
+
+    /**
+     * 获取无后缀的文件名
+     *
+     * @param fileName
+     *            示例:../xxx/abc.xx
+     * @return 示例:../xxx/abc
+     */
+    public static String getFilePathName(String fileName) {
+        if (fileName != null && fileName.length() > 0) {
+            int index = fileName.lastIndexOf(".");
+            if (index != -1) {
+                return fileName.substring(0, index);
+            }
+        }
+        return "";
+    }
+
+    /**
+     * 创建文件目录
+     */
+    public static boolean makeDirs(String path) {
+        if (path == null || "".equals(path)) {
+            return false;
+        }
+        File folder = new File(path);
+        if (!folder.exists()) {
+            return folder.mkdirs();
+        }
+        return true;
+    }
+
+    /**
+     * 保存字符串到文件中
+     */
+    public static void saveAsFile(String path, String content) {
+        saveAsFile(path, content, null);
+    }
+
+    public static void saveAsFile(String path, String content, String encoding) {
+        if (path == null || content == null) {
+            return;
+        }
+
+        if (encoding == null) {
+            encoding = "UTF-8";
+        }
+
+        File file = new File(path);
+        if (!file.exists()) {
+            if (FileUtil.makeDirs(file.getParent())) {
+                boolean ok = IoUtils.createFile(file);
+                if (!ok) {
+                    throw new RuntimeException("文件创建失败!");
+                }
+            }
+        }
+
+        try (FileOutputStream fos = new FileOutputStream(file);
+                OutputStreamWriter write = new OutputStreamWriter(fos, encoding);
+                BufferedWriter bw = new BufferedWriter(write);) {
+            bw.write(content);
+            bw.flush();
+            log.info("save as file success. " + path);
+        } catch (IOException e) {
+            log.error("save as file error. " + path);
+        }
+    }
+
+    /**
+     * 解压文件
+     *
+     * @param targetDir
+     *            解压目录
+     * @param zipFile
+     *            待解压的ZIP文件
+     */
+    public static List<File> unZip(File targetDir, File zipFile) {
+        if (targetDir == null) {
+            log.error("解压目录不能为空!");
+            return null;
+        }
+
+        if (zipFile == null) {
+            log.error("待解压的文件不能为空!");
+            return null;
+        }
+
+        if (!zipFile.exists()) {
+            log.error("待解压的文件不存在!" + zipFile.getAbsolutePath());
+            return null;
+        }
+
+        String zipName = zipFile.getName().toLowerCase();
+        if (zipFile.isDirectory() || zipName.indexOf(".zip") < 0) {
+            log.error("待解压的文件格式错误!");
+            return null;
+        }
+
+        if (!targetDir.exists()) {
+            targetDir.mkdir();
+        }
+
+        List<File> result = new LinkedList<>();
+
+        try (ZipFile zip = new ZipFile(zipFile, Charset.forName("UTF-8"));) {
+
+            Enumeration entries = zip.entries();
+            while (entries.hasMoreElements()) {
+                ZipEntry entry = (ZipEntry) entries.nextElement();
+
+                // Linux中需要替换掉路径的反斜杠
+                String entryName = (File.separator + entry.getName()).replaceAll("\\\\", "/");
+
+                String filePath = targetDir.getAbsolutePath() + entryName;
+                File target = new File(filePath);
+                if (entry.isDirectory()) {
+                    target.mkdirs();
+                } else {
+                    File dir = target.getParentFile();
+                    if (!dir.exists()) {
+                        dir.mkdirs();
+                    }
+
+                    try (OutputStream os = new FileOutputStream(target); InputStream is = zip.getInputStream(entry);) {
+                        IOUtils.copy(is, os);
+                        os.flush();
+                    } catch (IOException e) {
+                        log.error(e.getMessage(), e);
+                    }
+                    result.add(target);
+                }
+            }
+
+        } catch (IOException e) {
+            log.error(e.getMessage(), e);
+        }
+
+        return result;
+    }
+
+    /**
+     * 文件压缩
+     *
+     * @param target
+     *            目录或文件
+     * @param zipFile
+     *            压缩后的ZIP文件
+     */
+    public static boolean doZip(File target, File zipFile) {
+        if (target == null || !target.exists()) {
+            log.error("目录或文件不能为空!");
+            return false;
+        }
+
+        if (zipFile == null) {
+            log.error("待压缩的文件不能为空!");
+            return false;
+        }
+
+        try (OutputStream outStream = new FileOutputStream(zipFile);
+                ZipOutputStream zipOutStream = new ZipOutputStream(outStream, Charset.forName("UTF-8"));) {
+            if (!zipFile.exists()) {
+                boolean ok = zipFile.createNewFile();
+                if (!ok) {
+                    log.error("压缩的文件创建失败!");
+                    return false;
+                }
+            }
+
+            if (target.isDirectory()) {
+                File[] files = target.listFiles();
+                if (files.length == 0) {
+                    log.error("文件夹内未找到任何文件!");
+                    return false;
+                }
+
+                for (File file : files) {
+                    doZip(zipOutStream, file, null);
+                }
+            } else {
+                doZip(zipOutStream, target, null);
+            }
+        } catch (IOException e) {
+            log.error(e.getMessage(), e);
+        }
+
+        return true;
+    }
+
+    private static void doZip(ZipOutputStream zipOutStream, File target, String parentDir) throws IOException {
+        // log.info("Zip:" + parentDir);
+        if (parentDir == null) {
+            parentDir = "";
+        }
+
+        if (!"".equals(parentDir) && !parentDir.endsWith(File.separator)) {
+            parentDir += File.separator;
+        }
+
+        if (target.isDirectory()) {
+            File[] files = target.listFiles();
+            if (files.length > 0) {
+                for (File file : files) {
+                    doZip(zipOutStream, file, parentDir + target.getName());
+                }
+            } else {
+                zipOutStream.putNextEntry(new ZipEntry(parentDir + target.getName()));
+                zipOutStream.closeEntry();
+            }
+        } else {
+            try (InputStream is = new FileInputStream(target);) {
+                zipOutStream.putNextEntry(new ZipEntry(parentDir + target.getName()));
+                int len;
+                byte[] bytes = new byte[1024];
+                while ((len = is.read(bytes)) > 0) {
+                    zipOutStream.write(bytes, 0, len);
+                }
+            } catch (IOException e) {
+                log.error(e.getMessage(), e);
+            }
+            zipOutStream.closeEntry();
+        }
+    }
+
+    public static void deleteFolder(String path) {
+
+        File file = new File(path);
+        if (file.exists()) {
+            if (file.isFile()) {
+                deleteFile(path);
+            } else {
+                deleteDirectory(path);
+            }
+        }
+    }
+
+    public static void deleteFile(String path) {
+        File file = new File(path);
+        if (file.isFile() && file.exists()) {
+            file.delete();
+        }
+    }
+
+    public static void deleteDirectory(String path) {
+        if (!path.endsWith(File.separator)) {
+            path = path + File.separator;
+        }
+        File dirFile = new File(path);
+        if (!dirFile.exists() || !dirFile.isDirectory()) {
+            return;
+        }
+        File[] files = dirFile.listFiles();
+        if (files != null) {
+            for (int i = 0; i < files.length; i++) {
+                if (files[i].isFile()) {
+                    deleteFile(files[i].getAbsolutePath());
+                } else {
+                    deleteDirectory(files[i].getAbsolutePath());
+                }
+            }
+        }
+
+        dirFile.delete();
+    }
 }

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

@@ -290,7 +290,7 @@ public class ExportPaperServiceImpl implements ExportPaperService {
                 try {
                     pa = exportThemisPaperService.buildPaperAndAnswer(paper);
                 } catch (StatusException e) {
-                    throw new StatusException("试卷 " + paper.getName() + " " + e.getDesc());
+                    throw new StatusException("500", "试卷 " + paper.getName() + " " + e.getDesc(), e);
                 }
                 String paperDirectory = zipDirectory + File.separator + paper.getCourseNo() + File.separator
                         + paper.getId();

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

@@ -4,6 +4,8 @@ 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;
@@ -11,8 +13,6 @@ import java.util.Map;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
-import cn.com.qmth.examcloud.support.fss.FssFactory;
-import cn.com.qmth.examcloud.support.fss.FssHelper;
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.jsoup.Jsoup;
@@ -47,7 +47,6 @@ import cn.com.qmth.examcloud.core.questions.service.themispaper.ThemisAnswer;
 import cn.com.qmth.examcloud.core.questions.service.themispaper.ThemisAnswerContent;
 import cn.com.qmth.examcloud.core.questions.service.themispaper.ThemisAnswerDetail;
 import cn.com.qmth.examcloud.core.questions.service.themispaper.ThemisBlock;
-import cn.com.qmth.examcloud.core.questions.service.themispaper.ThemisSubjectiveAnswer;
 import cn.com.qmth.examcloud.core.questions.service.themispaper.ThemisOption;
 import cn.com.qmth.examcloud.core.questions.service.themispaper.ThemisPaper;
 import cn.com.qmth.examcloud.core.questions.service.themispaper.ThemisPaperAndAnswer;
@@ -55,6 +54,9 @@ import cn.com.qmth.examcloud.core.questions.service.themispaper.ThemisPaperDetai
 import cn.com.qmth.examcloud.core.questions.service.themispaper.ThemisQuestion;
 import cn.com.qmth.examcloud.core.questions.service.themispaper.ThemisSection;
 import cn.com.qmth.examcloud.core.questions.service.themispaper.ThemisSections;
+import cn.com.qmth.examcloud.core.questions.service.themispaper.ThemisSubjectiveAnswer;
+import cn.com.qmth.examcloud.support.fss.FssFactory;
+import cn.com.qmth.examcloud.support.fss.FssHelper;
 
 @Service("exportThemisPaperService")
 public class ExportThemisPaperServiceImpl implements ExportThemisPaperService {
@@ -307,8 +309,9 @@ public class ExportThemisPaperServiceImpl implements ExportThemisPaperService {
                     questions.add(computerTestQuestion);
                     answers.add(themisAnswerContent);
                 } catch (StatusException e) {
-                    throw new StatusException(
-                            "第" + paperDetail.getNumber() + "大题,第" + paperDetailUnit.getNumber() + "小题 " + e.getDesc());
+                    throw new StatusException("500",
+                            "第" + paperDetail.getNumber() + "大题,第" + paperDetailUnit.getNumber() + "小题 " + e.getDesc(),
+                            e);
                 }
             }
             computerTestPaperDetail.setQuestions(questions);
@@ -651,6 +654,7 @@ public class ExportThemisPaperServiceImpl implements ExportThemisPaperService {
                     }
                 }
                 block.setParam(param);
+                disposeIamgeUrlToBase64(block);
                 blocks.add(block);
             } else if (rowStr.startsWith("a")) { // 处理音频
                 if (rowStr.contains("id") && rowStr.contains("name")) { // 处理音频
@@ -686,6 +690,23 @@ public class ExportThemisPaperServiceImpl implements ExportThemisPaperService {
         return blocks;
     }
 
+    private void disposeIamgeUrlToBase64(ThemisBlock block) {
+        if (!"image".equals(block.getType())) {
+            return;
+        }
+        String src = block.getValue().toString().trim().toLowerCase();
+        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);
+        }
+    }
+
     /**
      * 获取图片里面的路径,长度,宽度
      */