Browse Source

美术阅卷10月新增需求-图片加密

wangliang 5 years ago
parent
commit
5d917f6782

+ 171 - 0
stmms-ms-collect/src/main/java/cn/com/qmth/stmms/ms/collect/api/CollectApi.java

@@ -7,6 +7,9 @@ import cn.com.qmth.stmms.ms.collect.dto.CollectSubjectDTO;
 import cn.com.qmth.stmms.ms.collect.dto.LoginDTO;
 import cn.com.qmth.stmms.ms.commons.config.ImageCompressionConfig;
 import cn.com.qmth.stmms.ms.commons.config.SystemConfig;
+import cn.com.qmth.stmms.ms.commons.constant.SystemConstant;
+import cn.com.qmth.stmms.ms.commons.utils.MD5Util;
+import cn.com.qmth.stmms.ms.commons.utils.PictureUtil;
 import cn.com.qmth.stmms.ms.commons.utils.image.ImageCompression;
 import cn.com.qmth.stmms.ms.core.domain.MarkSubject;
 import cn.com.qmth.stmms.ms.core.domain.Student;
@@ -19,6 +22,7 @@ import cn.com.qmth.stmms.ms.core.vo.Subject;
 import net.sf.json.JSONObject;
 import org.apache.commons.codec.digest.DigestUtils;
 import org.assertj.core.util.Strings;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.util.FileCopyUtils;
 import org.springframework.web.bind.annotation.*;
@@ -38,6 +42,7 @@ import java.util.function.Consumer;
 @RestController
 @RequestMapping("api")
 public class CollectApi {
+    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(CollectApi.class);
 
     @Autowired
     private LoginConfig loginConfig;
@@ -136,6 +141,150 @@ public class CollectApi {
         return list;
     }
 
+    File srcFile = new File("/Users/king/stmms-ms/static/thumbs/6/SX/1/1901040150.jpg"); //初始文件
+
+    /**
+     * 上传图片到服务器(图片加密)
+     *
+     * @param workId
+     * @param subjectId
+     * @param fileName
+     * @param request
+     * @param response
+     * @throws IOException
+     */
+    @RequestMapping(value = "file/image/upload/{workId}/{subjectId}/{fileName}", method = {RequestMethod.GET})
+    public void imageUpload(@PathVariable Long workId, @PathVariable Integer subjectId,
+                            @PathVariable String fileName,
+                            HttpServletRequest request, HttpServletResponse response) throws IOException {
+        InputStream inputStream = null;
+        OutputStream outputStream = null;
+        try {
+//            inputStream = request.getInputStream();
+//            String md5 = request.getHeader("md5");
+            inputStream = PictureUtil.getInput(srcFile);
+            Student student = studentRepo.findByWorkIdAndExamNumber(workId, fileName);
+            Subject subject = Subject.values()[subjectId - 1];
+            //保存遮盖图+文件名加密
+            String savePath = systemConfig.getImageDir() + File.separator + workId + File.separator + subject
+                    + File.separator + student.getAreaCode();
+            File out = new File(savePath);
+            if (!out.exists()) {
+                out.mkdirs();
+            }
+
+            String imageMd5 = getImageRuleMd5(workId, subject.ordinal(), student.getAreaCode(), student.getExamNumber(), student.getId(), SystemConstant.IMAGE);
+//            暂不用Aes加密,因为没用到反解
+//            StringBuffer stringBuffer = new StringBuffer(String.valueOf(workId)).append(subject.ordinal()).append(student.getAreaCode()).append(student.getExamNumber()).append(student.getId()).append(image);
+//            String rule = stringBuffer.toString();
+//            LOGGER.info("rule:{},length:{}", rule, rule.length());
+//            rule = String.format("%016d", Long.parseLong(rule));
+//            LOGGER.info("rule补零后:{},length:{}", rule, rule.length());
+//            String aesStr = AesUtil.encoder(student.getExamNumber(), stringBuffer.toString(), rule);
+//            LOGGER.info("aesStr:{}", aesStr);
+//            File outFile = new File(savePath + File.separator + aesStr + ".jpg");
+            File outFile = new File(savePath + File.separator + imageMd5 + ".jpg");
+            LOGGER.info("image存放目录:{}", outFile.getPath());
+            outputStream = new FileOutputStream(outFile);
+            int dataOfFile = 0;
+            while ((dataOfFile = inputStream.read()) > -1) {
+                outputStream.write(dataOfFile ^ SystemConstant.SECRET_KEY);
+            }
+
+            // 生成缩略图
+            String thumbDir = systemConfig.getThumbDir() + File.separator + workId + File.separator + subject
+                    + File.separator + student.getAreaCode();
+            File thumb = new File(thumbDir);
+            if (!thumb.exists()) {
+                thumb.mkdirs();
+            }
+            BufferedImage bufferedImage = ImageCompression.compress(outFile, compressionConfig);
+//            String thumbMd5 = getImageRuleMd5(workId, subject.ordinal(), student.getAreaCode(), student.getExamNumber(), student.getId(), SystemConstant.THUMB);
+            File thumbFile = new File(savePath + File.separator + imageMd5 + ".jpg");
+            ImageIO.write(bufferedImage, "jpg", thumbFile);
+//            FileInputStream in = new FileInputStream(outFile);
+//            String sliceMD5 = DigestUtils.md5Hex(in);
+//            if (!md5.equalsIgnoreCase(sliceMD5)) {
+//                throw new Exception("图片md5值不一致");
+//            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            if (Objects.nonNull(inputStream)) {
+                inputStream.close();
+            }
+            if (Objects.nonNull(inputStream)) {
+                outputStream.flush();
+                outputStream.close();
+            }
+        }
+    }
+
+    /**
+     * 从服务器上下载图片(图片解密)
+     *
+     * @param workId
+     * @param subjectId
+     * @param studentId
+     * @param imageType
+     * @param request
+     * @param response
+     * @throws IOException
+     */
+    @RequestMapping(value = "file/image/download/{workId}/{subjectId}/{studentId}/{imageType}", method = {RequestMethod.GET})
+    public void imageDownload(@PathVariable Long workId, @PathVariable Integer subjectId,
+                              @PathVariable Long studentId, @PathVariable Integer imageType,
+                              HttpServletRequest request, HttpServletResponse response) throws IOException {
+        //读取路径下面的文件
+        InputStream in = null;
+        OutputStream outputStream = null;
+        try {
+            Student student = studentRepo.findOne(studentId);
+            Subject subject = Subject.values()[subjectId - 1];
+            String savePath = systemConfig.getImageDir() + File.separator + workId + File.separator + subject
+                    + File.separator + student.getAreaCode();
+//            暂不用Aes解密,因为没用到反解
+//            StringBuffer stringBuffer = new StringBuffer(String.valueOf(workId)).append(subject.ordinal()).append(student.getAreaCode()).append(student.getExamNumber()).append(student.getId()).append(image);
+//            String savePath = systemConfig.getImageDir() + File.separator + workId + File.separator + subject
+//                    + File.separator + student.getAreaCode();
+//            String rule = stringBuffer.toString();
+//            LOGGER.info("rule:{},length:{}", rule, rule.length());
+//            rule = String.format("%016d", Long.parseLong(rule));
+//            LOGGER.info("rule补零后:{},length:{}", rule, rule.length());
+//            String aesStr = AesUtil.encoder(student.getExamNumber(), stringBuffer.toString(), rule);
+//            String examNumber = AesUtil.decoder(aesStr, stringBuffer.toString(), rule);
+//            LOGGER.info("解密后的文件名称:{}", examNumber);
+            String md5 = getImageRuleMd5(workId, subject.ordinal(), student.getAreaCode(), student.getExamNumber(), student.getId(), imageType);
+//            String ext = file.getName().substring(file.getName().indexOf(".") + 1);
+            //判断图片格式,设置相应的输出文件格式
+//            if (ext.equalsIgnoreCase("jpg")) {
+            response.setContentType("image/jpg");
+//            } else if (ext.equalsIgnoreCase("png")) {
+//                response.setContentType("image/png");
+//            }
+            File file = new File(savePath + File.separator + md5 + ".jpg");
+            //读取指定路径下面的文件
+            in = new FileInputStream(file);
+            outputStream = new BufferedOutputStream(response.getOutputStream());
+            int dataOfFile = 0;
+            while ((dataOfFile = in.read()) > -1) {
+                outputStream.write(dataOfFile ^ SystemConstant.SECRET_KEY);
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            //强制将缓存区的数据进行输出
+            if (Objects.nonNull(outputStream)) {
+                outputStream.flush();
+                //关流
+                outputStream.close();
+            }
+            if (Objects.nonNull(in)) {
+                in.close();
+            }
+        }
+    }
+
     /**
      * 上传裁切原图并压缩
      *
@@ -234,4 +383,26 @@ public class CollectApi {
         markSubjectRepo.save(markSubject);
         return true;
     }
+
+    /**
+     * 生成图片md5规则
+     *
+     * @param workId
+     * @param subjectId
+     * @param areaCode
+     * @param examNumber
+     * @param studentId
+     * @param imageType
+     * @return
+     * @throws Exception
+     */
+    public String getImageRuleMd5(Long workId, int subjectId, String areaCode, String examNumber, Long studentId, int imageType) throws Exception {
+        //暂不用Aes解密,因为没用到反解
+        StringBuffer stringBuffer = new StringBuffer(String.valueOf(workId)).append(subjectId).append(areaCode).append(examNumber).append(studentId).append(imageType);
+        String rule = stringBuffer.toString();
+        LOGGER.info("rule:{},length:{}", rule, rule.length());
+        rule = String.format("%025d", Long.parseLong(rule));
+        LOGGER.info("rule补零后:{},length:{}", rule, rule.length());
+        return MD5Util.encoder(rule);
+    }
 }