wangliang пре 3 година
родитељ
комит
e86f112659

+ 6 - 0
distributed-print-business/src/main/java/com/qmth/distributed/print/business/entity/TSyncExamStudentScore.java

@@ -108,6 +108,12 @@ public class TSyncExamStudentScore implements Serializable {
     @TableField(exist = false)
     private File trajectoryFile;
 
+    public void setErrorInfo(){
+        this.trajectoryFile = null;
+        this.trajectoryUrls = null;
+        this.path = null;
+    }
+
     public String getPath() {
         return path;
     }

+ 9 - 15
distributed-print-business/src/main/java/com/qmth/distributed/print/business/service/impl/TSyncExamStudentScoreServiceImpl.java

@@ -95,8 +95,6 @@ public class TSyncExamStudentScoreServiceImpl extends ServiceImpl<TSyncExamStude
     @Override
     public TSyncExamStudentScore createImageTrajectory(TSyncExamStudentScore tSyncExamStudentScore, ImageTrajectoryEnum imageTrajectoryEnum) {
         File fileSource = null, fileTarget = null;
-        String ossType = null;
-        JSONObject jsonObject = null;
         try {
             StringJoiner stringJoiner = new StringJoiner("");
             stringJoiner.add(SystemConstant.TEMP_FILES_DIR).add(File.separator);
@@ -130,7 +128,7 @@ public class TSyncExamStudentScoreServiceImpl extends ServiceImpl<TSyncExamStude
                 }
 
                 ImageTrajectoryUtil.createImage(fileSource, fileTarget, 10, 10);
-                jsonObject = new JSONObject();
+                JSONObject jsonObject = new JSONObject();
                 jsonObject.put(SystemConstant.UPLOAD_TYPE, UploadFileEnum.FILE);
                 if (oss) {
                     fileStoreUtil.ossUpload(dirTargetJpgName.toString(), fileTarget, DigestUtils.md5Hex(new FileInputStream(fileTarget)), UploadFileEnum.FILE.getFssType());
@@ -143,28 +141,24 @@ public class TSyncExamStudentScoreServiceImpl extends ServiceImpl<TSyncExamStude
                     tSyncExamStudentScore.setPath(stringJoiner.toString() + dirTargetJpgName.toString());
                 }
                 tSyncExamStudentScore.setTrajectoryUrls(jsonObject.toJSONString());
-                tSyncExamStudentScore.setTrajectoryFile(fileTarget);
             } else {
-                jsonObject = JSONObject.parseObject(tSyncExamStudentScore.getTrajectoryUrls());
-                ossType = (String) jsonObject.get(SystemConstant.TYPE);
+                JSONObject jsonObject = JSONObject.parseObject(tSyncExamStudentScore.getTrajectoryUrls());
+                String ossType = (String) jsonObject.get(SystemConstant.TYPE);
                 if (Objects.equals(ossType, SystemConstant.OSS)) {
                     tSyncExamStudentScore.setPath(teachcloudCommonService.filePreview(jsonObject.toJSONString()));
-                } else {
-                    tSyncExamStudentScore.setPath(stringJoiner.toString() + jsonObject.get(SystemConstant.PATH));
-                }
-            }
-            if (imageTrajectoryEnum == ImageTrajectoryEnum.DOWNLOAD && Objects.isNull(fileTarget)) {
-                ossType = (String) jsonObject.get(SystemConstant.TYPE);
-                if (Objects.equals(ossType, SystemConstant.OSS)) {
                     fileTarget = fileStoreUtil.ossDownload(jsonObject.get(SystemConstant.PATH).toString(), stringJoiner.toString() + jsonObject.get(SystemConstant.PATH).toString(), UploadFileEnum.FILE.getFssType());
                 } else {
+                    tSyncExamStudentScore.setPath(stringJoiner.toString() + jsonObject.get(SystemConstant.PATH));
                     fileTarget = new File(stringJoiner.toString() + jsonObject.get(SystemConstant.PATH).toString());
                 }
-                tSyncExamStudentScore.setTrajectoryFile(fileTarget);
             }
+            tSyncExamStudentScore.setTrajectoryFile(fileTarget);
         } catch (Exception e) {
             e.printStackTrace();
-            tSyncExamStudentScore.setTrajectoryUrls(null);
+            tSyncExamStudentScore.setErrorInfo();
+            if (Objects.nonNull(fileTarget)) {
+                fileTarget.delete();
+            }
         } finally {
             if (Objects.nonNull(fileSource)) {
                 fileSource.delete();

+ 25 - 10
distributed-print/src/main/java/com/qmth/distributed/print/api/TSyncExamStudentScoreController.java

@@ -27,16 +27,18 @@ import com.qmth.teachcloud.common.util.Result;
 import com.qmth.teachcloud.common.util.ResultUtil;
 import com.qmth.teachcloud.common.util.ServletUtil;
 import io.swagger.annotations.*;
+import org.apache.commons.io.IOUtils;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.validation.annotation.Validated;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestMethod;
-import org.springframework.web.bind.annotation.RequestParam;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 
 import javax.annotation.Resource;
+import javax.servlet.http.HttpServletResponse;
 import javax.validation.constraints.Max;
 import javax.validation.constraints.Min;
+import java.io.BufferedOutputStream;
+import java.io.FileInputStream;
+import java.io.OutputStream;
 import java.util.Map;
 import java.util.Objects;
 import java.util.Optional;
@@ -125,25 +127,38 @@ public class TSyncExamStudentScoreController {
         return Objects.nonNull(tbSyncTask) ? ResultUtil.ok(tbSyncTask.getId()) : ResultUtil.error("创建同步推送任务失败");
     }
 
+    @CrossOrigin(maxAge = 3600) //支持跨域
     @ApiOperation(value = "成绩动态轨迹图下载")
     @ApiResponses({@ApiResponse(code = 200, message = "常规信息", response = EditResult.class)})
     @RequestMapping(value = "/score/download", method = RequestMethod.POST)
     @Transactional
-    public Result download(@ApiParam(value = "学号", required = true) @RequestParam String studentCode) throws Exception {
+    public void download(@ApiParam(value = "学号", required = true) @RequestParam String studentCode) throws Exception {
         SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
         QueryWrapper<TSyncExamStudentScore> tSyncExamStudentScoreQueryWrapper = new QueryWrapper<>();
-        tSyncExamStudentScoreQueryWrapper.lambda().eq(TSyncExamStudentScore::getSchoolId, sysUser.getSchoolId())
-                .eq(TSyncExamStudentScore::getStudentCode, studentCode);
+        tSyncExamStudentScoreQueryWrapper.lambda().eq(TSyncExamStudentScore::getSchoolId, sysUser.getSchoolId()).eq(TSyncExamStudentScore::getStudentCode, studentCode);
         TSyncExamStudentScore tSyncExamStudentScore = tSyncExamStudentScoreService.getOne(tSyncExamStudentScoreQueryWrapper);
-        Optional.ofNullable(tSyncExamStudentScore).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("考生不存在"));
-        tSyncExamStudentScore = tSyncExamStudentScoreService.createImageTrajectory(tSyncExamStudentScore, ImageTrajectoryEnum.PREVIEW);
         boolean update = Objects.isNull(tSyncExamStudentScore.getTrajectoryUrls()) ? true : false;
         tSyncExamStudentScore = tSyncExamStudentScoreService.createImageTrajectory(tSyncExamStudentScore, ImageTrajectoryEnum.PREVIEW);
         Optional.ofNullable(tSyncExamStudentScore.getTrajectoryUrls()).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("资源未获取到,请稍候再试"));
         if (update || Objects.isNull(tSyncExamStudentScore.getTrajectoryUrls())) {
             tSyncExamStudentScoreService.updateById(tSyncExamStudentScore);
         }
-        return ResultUtil.ok(new EditResult(tSyncExamStudentScore.getPath()));
+        OutputStream outputStream = null;
+        try {
+            if (Objects.nonNull(tSyncExamStudentScore.getTrajectoryFile())) {
+                HttpServletResponse response = ServletUtil.getResponse();
+                response.setContentType("image/jpeg");
+                outputStream = new BufferedOutputStream(response.getOutputStream());
+                IOUtils.copy(new FileInputStream(tSyncExamStudentScore.getTrajectoryFile()), outputStream);
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            if (Objects.nonNull(outputStream)) {
+                outputStream.flush();
+                outputStream.close();
+            }
+        }
     }
 
     @ApiOperation(value = "成绩动态轨迹图一键下载")