deason 1 年之前
父節點
當前提交
fddca8c5af

+ 28 - 3
src/main/java/cn/com/qmth/examcloud/tool/service/export_student_photo/ExportStudentPhotoTask.java

@@ -3,17 +3,20 @@ package cn.com.qmth.examcloud.tool.service.export_student_photo;
 import cn.com.qmth.examcloud.tool.config.SysProperty;
 import cn.com.qmth.examcloud.tool.service.CommonService;
 import cn.com.qmth.examcloud.tool.service.export_student_photo.vo.StudentVO;
+import cn.com.qmth.examcloud.tool.utils.FileHelper;
 import cn.com.qmth.examcloud.tool.utils.HttpHelper;
 import cn.com.qmth.examcloud.tool.utils.JsonMapper;
 import cn.com.qmth.examcloud.tool.vo.PageInfo;
 import cn.com.qmth.examcloud.tool.vo.user.User;
 import com.fasterxml.jackson.core.type.TypeReference;
 import org.apache.commons.collections4.CollectionUtils;
+import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
+import java.io.File;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -28,10 +31,12 @@ public class ExportStudentPhotoTask {
     @Autowired
     private CommonService commonService;
 
+    private static final String EXPORT_DIR = "D:/home/captures/photo/";
+
     public void start(User user, Long rootOrgId) {
         this.execute(user, rootOrgId);
     }
-    
+
     private void execute(User user, Long rootOrgId) {
         Map<String, String> headers = new HashMap<>();
         headers.put("key", user.getKey());
@@ -54,13 +59,33 @@ public class ExportStudentPhotoTask {
 
             for (StudentVO student : page.getList()) {
                 // 下载底照
-
+                this.downloadPhoto(student);
             }
 
             pageNo++;
             sum += page.getList().size();
             float rate = sum * 100f / page.getTotal();
-            log.info("rootOrgId:{} 已下载学生底照数:{} 进度:{}%", rootOrgId, sum, rate);
+            log.info("rootOrgId:{} 已下载学生底照数:{} 总数:{} 进度:{}%", rootOrgId, sum, page.getTotal(), rate);
+        }
+    }
+
+    private void downloadPhoto(StudentVO student) {
+        if (StringUtils.isEmpty(student.getPhotoPath())) {
+            return;
+        }
+
+        String studentCode = student.getStudentCodeList().get(0);
+
+        String filePath = EXPORT_DIR + "/" + student.getRootOrgId() + "/" + studentCode + ".jpg";
+        if (new File(filePath).exists()) {
+            log.info("Photo has download! filePath:{}", filePath);
+            return;
+        }
+
+        try {
+            FileHelper.saveImageToFile(student.getPhotoPath(), filePath);
+        } catch (Exception e) {
+            log.error(e.getMessage());
         }
     }
 

+ 11 - 2
src/main/java/cn/com/qmth/examcloud/tool/service/export_student_photo/vo/StudentVO.java

@@ -4,6 +4,7 @@ import lombok.Getter;
 import lombok.Setter;
 
 import java.io.Serializable;
+import java.util.List;
 
 @Setter
 @Getter
@@ -11,15 +12,23 @@ public class StudentVO implements Serializable {
 
     private static final long serialVersionUID = -4647126917276922968L;
 
-    private Long rootOrgId;
-
     private Long id;
 
+    /**
+     * 学校ID
+     */
+    private Long rootOrgId;
+
     /**
      * 学生姓名
      */
     private String name;
 
+    /**
+     * 学号列表
+     */
+    private List<String> studentCodeList;
+
     /**
      * 学号
      */

+ 55 - 1
src/main/java/cn/com/qmth/examcloud/tool/utils/FileHelper.java

@@ -1,6 +1,8 @@
 package cn.com.qmth.examcloud.tool.utils;
 
 import com.google.common.io.Files;
+import okhttp3.Request;
+import okhttp3.Response;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.LineIterator;
 import org.apache.commons.lang3.StringUtils;
@@ -102,7 +104,7 @@ public class FileHelper {
         }
 
         try (OutputStream outStream = new FileOutputStream(zipFile);
-             ZipOutputStream zipStream = new ZipOutputStream(outStream, StandardCharsets.UTF_8);) {
+                ZipOutputStream zipStream = new ZipOutputStream(outStream, StandardCharsets.UTF_8);) {
             if (sourceFile.isDirectory()) {
                 File[] subFiles = sourceFile.listFiles();
                 if (subFiles.length == 0) {
@@ -158,4 +160,56 @@ public class FileHelper {
         }
     }
 
+    public static byte[] download(String url) {
+        Request request;
+        try {
+            request = new Request.Builder().url(url).get().build();
+        } catch (IllegalArgumentException e) {
+            log.warn(e.getMessage());
+            return null;
+        }
+
+        try (Response response = HttpClientBuilder.getClient().newCall(request).execute();) {
+            if (response.isSuccessful()) {
+                return response.body().bytes();
+            }
+        } catch (Exception e) {
+            log.error(e.getMessage(), e);
+        }
+
+        return null;
+    }
+
+    public static void saveImageToFile(String imgUrl, String filePath) {
+        byte[] data = download(imgUrl);
+
+        if (data == null || data.length == 0) {
+            throw new RuntimeException("图片下载错误!" + imgUrl);
+        }
+
+        saveToFile(data, new File(filePath));
+    }
+
+    public static void saveToFile(byte[] bytes, File targetFile) {
+        if (bytes == null || targetFile == null) {
+            return;
+        }
+
+        if (!targetFile.exists()) {
+            try {
+                targetFile.getParentFile().mkdirs();
+                targetFile.createNewFile();
+            } catch (IOException e) {
+                log.error(e.getMessage(), e);
+            }
+        }
+
+        try (FileOutputStream fos = new FileOutputStream(targetFile);
+                BufferedOutputStream bos = new BufferedOutputStream(fos);) {
+            bos.write(bytes);
+        } catch (IOException e) {
+            log.error(e.getMessage(), e);
+        }
+    }
+
 }