|
@@ -11,6 +11,7 @@ import cn.com.qmth.stmms.ms.commons.config.ImageConfig;
|
|
|
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.CommandUtil;
|
|
|
+import cn.com.qmth.stmms.ms.commons.utils.FileUtil;
|
|
|
import cn.com.qmth.stmms.ms.commons.utils.MD5Util;
|
|
|
import cn.com.qmth.stmms.ms.commons.utils.image.ImageCompression;
|
|
|
import cn.com.qmth.stmms.ms.core.domain.MarkSubject;
|
|
@@ -32,6 +33,7 @@ import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.util.FileCopyUtils;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import javax.imageio.ImageIO;
|
|
@@ -83,11 +85,14 @@ public class CollectApi {
|
|
|
AliYunOssConfig aliYunOssConfig;
|
|
|
|
|
|
@RequestMapping("user/login")
|
|
|
- public LoginDTO login(@RequestParam String loginname, @RequestParam String password) {
|
|
|
+ public LoginDTO login(@RequestParam String loginname, @RequestParam String password) throws Exception {
|
|
|
LoginDTO loginDTO = null;
|
|
|
Work activeWork = null;
|
|
|
if (loginname.equals(loginConfig.uploadLoginConfig().getLoginName()) && password.equals(loginConfig.uploadLoginConfig().getPassword())) {
|
|
|
activeWork = workRepo.findByActiveTrue();
|
|
|
+ if (Objects.isNull(activeWork)) {
|
|
|
+ throw new Exception("没有work");
|
|
|
+ }
|
|
|
loginDTO = new LoginDTO();
|
|
|
loginDTO.setExamId(activeWork.getId());
|
|
|
loginDTO.setExamName(activeWork.getName());
|
|
@@ -106,6 +111,9 @@ public class CollectApi {
|
|
|
throw new RuntimeException("密码错误");
|
|
|
}
|
|
|
activeWork = workRepo.findByActiveTrue();
|
|
|
+ if (Objects.isNull(activeWork)) {
|
|
|
+ throw new Exception("没有work");
|
|
|
+ }
|
|
|
loginDTO = new LoginDTO();
|
|
|
loginDTO.setUserId(markUser.getId());
|
|
|
loginDTO.setExamId(activeWork.getId());
|
|
@@ -424,22 +432,64 @@ public class CollectApi {
|
|
|
*
|
|
|
* @param workId
|
|
|
* @param subjectId
|
|
|
- * @param fileName
|
|
|
+ * @param file
|
|
|
* @param request
|
|
|
* @param response
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- @RequestMapping(value = "file/ms-slice/{workId}/{subjectId}/{fileName}", method = {RequestMethod.PUT, RequestMethod.POST})
|
|
|
+ @RequestMapping(value = "file/ms-slice/{workId}/{subjectId}", method = {RequestMethod.PUT, RequestMethod.POST})
|
|
|
public void upload(@PathVariable Long workId, @PathVariable Integer subjectId,
|
|
|
- @PathVariable String fileName,
|
|
|
+ @RequestParam MultipartFile file,
|
|
|
HttpServletRequest request, HttpServletResponse response) throws Exception {
|
|
|
- InputStream inputStream = request.getInputStream();
|
|
|
- Student student = studentRepo.findByWorkIdAndExamNumberAndTest(workId, fileName, String.valueOf(TrialEnum.DEFAULT.getId()));
|
|
|
+ if (Objects.isNull(file)) {
|
|
|
+ throw new Exception("file文件不能为空");
|
|
|
+ }
|
|
|
+ int temp = file.getOriginalFilename().indexOf(".");
|
|
|
+ String examNumber = file.getOriginalFilename().substring(0, temp);
|
|
|
+ String format = file.getOriginalFilename().substring(temp, file.getOriginalFilename().length());
|
|
|
+ if (Objects.nonNull(format) && !(format.equalsIgnoreCase(".jpg") || format.equalsIgnoreCase(".jpeg") || format.equalsIgnoreCase(".png"))) {
|
|
|
+ throw new Exception("file文件格式只能为jpg,jpeg,png");
|
|
|
+ }
|
|
|
+ InputStream inputStream = file.getInputStream();
|
|
|
+ Student student = studentRepo.findByWorkIdAndExamNumberAndTest(workId, examNumber, String.valueOf(TrialEnum.DEFAULT.getId()));
|
|
|
+ if (Objects.isNull(student)) {
|
|
|
+ throw new Exception("准考证号与学生不匹配");
|
|
|
+ }
|
|
|
Subject subject = Subject.values()[subjectId - 1];
|
|
|
- //保存遮盖图
|
|
|
+
|
|
|
+ //保存原图
|
|
|
+ String saveSheetPath = systemConfig.getSheetDir() + File.separator + workId + File.separator + subject
|
|
|
+ + File.separator + student.getAreaCode();
|
|
|
String savePath = systemConfig.getImageDir() + File.separator + workId + File.separator + subject
|
|
|
+ File.separator + student.getAreaCode();
|
|
|
- File outFile = saveImage(student, inputStream, savePath);
|
|
|
+ File imageOut = new File(savePath);
|
|
|
+ if (!imageOut.exists()) {
|
|
|
+ imageOut.mkdirs();
|
|
|
+ }
|
|
|
+ File sheetOut = new File(saveSheetPath);
|
|
|
+ if (!sheetOut.exists()) {
|
|
|
+ sheetOut.mkdirs();
|
|
|
+ }
|
|
|
+ File outSheetFile = new File(saveSheetPath + File.separator + student.getExamNumber() + ".jpg");
|
|
|
+ File outImageFile = new File(savePath + File.separator + student.getExamNumber() + ".jpg");
|
|
|
+ OutputStream outputSheetStream = new FileOutputStream(outSheetFile);
|
|
|
+ OutputStream outputImageStream = new FileOutputStream(outImageFile);
|
|
|
+ int index = 0;
|
|
|
+ byte[] bytes = new byte[1024 * 8];
|
|
|
+ while ((index = inputStream.read(bytes)) != -1) {
|
|
|
+ outputSheetStream.write(bytes, 0, index);
|
|
|
+ outputImageStream.write(bytes, 0, index);
|
|
|
+ }
|
|
|
+ inputStream.close();
|
|
|
+ outputSheetStream.flush();
|
|
|
+ outputSheetStream.close();
|
|
|
+ outputImageStream.flush();
|
|
|
+ outputImageStream.close();
|
|
|
+
|
|
|
+ //保存遮盖图
|
|
|
+// String savePath = systemConfig.getImageDir() + File.separator + workId + File.separator + subject
|
|
|
+// + File.separator + student.getAreaCode();
|
|
|
+// File outFile = saveImage(student, inputStream, savePath);
|
|
|
|
|
|
// 生成缩略图
|
|
|
String thumbDir = systemConfig.getThumbDir() + File.separator + workId + File.separator + subject
|
|
@@ -448,12 +498,15 @@ public class CollectApi {
|
|
|
if (!thumb.exists()) {
|
|
|
thumb.mkdirs();
|
|
|
}
|
|
|
- BufferedImage bufferedImage = ImageCompression.compress(outFile, compressionConfig);
|
|
|
+ BufferedImage bufferedImage = ImageCompression.compress(outImageFile, compressionConfig);
|
|
|
String thumbFileName = thumbDir + File.separator + student.getExamNumber() + ".jpg";
|
|
|
ImageIO.write(bufferedImage, "jpg", new File(thumbFileName));
|
|
|
|
|
|
String md5 = request.getHeader("md5");
|
|
|
- FileInputStream in = new FileInputStream(outFile);
|
|
|
+ if (Objects.isNull(md5)) {
|
|
|
+ throw new Exception("request的md5为空");
|
|
|
+ }
|
|
|
+ FileInputStream in = new FileInputStream(outImageFile);
|
|
|
String sliceMD5 = DigestUtils.md5Hex(in);
|
|
|
if (!md5.equalsIgnoreCase(sliceMD5)) {
|
|
|
throw new Exception("图片md5值不一致");
|
|
@@ -478,24 +531,39 @@ public class CollectApi {
|
|
|
*
|
|
|
* @param workId
|
|
|
* @param subjectId
|
|
|
- * @param fileName
|
|
|
+ * @param file
|
|
|
* @param request
|
|
|
* @param response
|
|
|
* @throws IOException
|
|
|
*/
|
|
|
@RequestMapping(value = "file/ms-sheet/{workId}/{subjectId}/{fileName}", method = {RequestMethod.PUT, RequestMethod.POST})
|
|
|
public void uploadsheet(@PathVariable Long workId, @PathVariable Integer subjectId,
|
|
|
- @PathVariable String fileName,
|
|
|
+ @RequestParam MultipartFile file,
|
|
|
HttpServletRequest request, HttpServletResponse response) throws Exception {
|
|
|
- Student student = studentRepo.findByWorkIdAndExamNumberAndTest(workId, fileName, String.valueOf(TrialEnum.DEFAULT.getId()));
|
|
|
+ if (Objects.isNull(file)) {
|
|
|
+ throw new Exception("file文件不能为空");
|
|
|
+ }
|
|
|
+ int temp = file.getOriginalFilename().indexOf(".");
|
|
|
+ String examNumber = file.getOriginalFilename().substring(0, temp);
|
|
|
+ String format = file.getOriginalFilename().substring(temp, file.getOriginalFilename().length());
|
|
|
+ if (Objects.nonNull(format) && !(format.equalsIgnoreCase(".jpg") || format.equalsIgnoreCase(".jpeg") || format.equalsIgnoreCase(".png"))) {
|
|
|
+ throw new Exception("file文件格式只能为jpg,jpeg,png");
|
|
|
+ }
|
|
|
+ Student student = studentRepo.findByWorkIdAndExamNumberAndTest(workId, examNumber, String.valueOf(TrialEnum.DEFAULT.getId()));
|
|
|
+ if (Objects.isNull(student)) {
|
|
|
+ throw new Exception("准考证号与学生不匹配");
|
|
|
+ }
|
|
|
Subject subject = Subject.values()[subjectId - 1];
|
|
|
- InputStream inputStream = request.getInputStream();
|
|
|
+ InputStream inputStream = file.getInputStream();
|
|
|
//保存原图图
|
|
|
String savePath = systemConfig.getSheetDir() + File.separator + workId + File.separator + subject
|
|
|
+ File.separator + student.getAreaCode();
|
|
|
File outFile = saveImage(student, inputStream, savePath);
|
|
|
|
|
|
String md5 = request.getHeader("md5");
|
|
|
+ if (Objects.isNull(md5)) {
|
|
|
+ throw new Exception("request的md5为空");
|
|
|
+ }
|
|
|
FileInputStream in = new FileInputStream(outFile);
|
|
|
String sheetMD5 = DigestUtils.md5Hex(in);
|
|
|
if (!md5.equalsIgnoreCase(sheetMD5)) {
|
|
@@ -531,25 +599,25 @@ public class CollectApi {
|
|
|
if (Objects.nonNull(imageFile) && imageFile.exists()) {
|
|
|
String imageResult = CommandUtil.run(new StringJoiner("").add(aliYunOssConfig.getUtil()).add(" ").add(systemConfig.getImageDir()).add(" ").add(aliYunOssConfig.getImageDir()).toString());
|
|
|
LOGGER.info("ossUpload imageResult:{}", imageResult);
|
|
|
- deleteFile(imageFile);
|
|
|
+ FileUtil.deleteFile(imageFile);
|
|
|
}
|
|
|
File thumbFile = new File(systemConfig.getThumbDir() + File.separator + workId);
|
|
|
if (Objects.nonNull(thumbFile) && thumbFile.exists()) {
|
|
|
String thumbResult = CommandUtil.run(new StringJoiner("").add(aliYunOssConfig.getUtil()).add(" ").add(systemConfig.getThumbDir()).add(" ").add(aliYunOssConfig.getThumbDir()).toString());
|
|
|
LOGGER.info("ossUpload thumbResult:{}", thumbResult);
|
|
|
- deleteFile(thumbFile);
|
|
|
+ FileUtil.deleteFile(thumbFile);
|
|
|
}
|
|
|
File sheetFile = new File(systemConfig.getSheetDir() + File.separator + workId);
|
|
|
if (Objects.nonNull(sheetFile) && sheetFile.exists()) {
|
|
|
String sheetResult = CommandUtil.run(new StringJoiner("").add(aliYunOssConfig.getUtil()).add(" ").add(systemConfig.getSheetDir()).add(" ").add(aliYunOssConfig.getSheetDir()).toString());
|
|
|
LOGGER.info("ossUpload sheetResult:{}", sheetResult);
|
|
|
- deleteFile(sheetFile);
|
|
|
+ FileUtil.deleteFile(sheetFile);
|
|
|
}
|
|
|
File watermarkFile = new File(systemConfig.getWatermark() + File.separator + workId);
|
|
|
if (Objects.nonNull(watermarkFile) && watermarkFile.exists()) {
|
|
|
String watermarkResult = CommandUtil.run(new StringJoiner("").add(aliYunOssConfig.getUtil()).add(" ").add(systemConfig.getWatermark()).add(" ").add(aliYunOssConfig.getWatermark()).toString());
|
|
|
LOGGER.info("ossUpload watermarkResult:{}", watermarkResult);
|
|
|
- deleteFile(watermarkFile);
|
|
|
+ FileUtil.deleteFile(watermarkFile);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
@@ -557,26 +625,4 @@ public class CollectApi {
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
-
|
|
|
- /**
|
|
|
- * 删除文件夹
|
|
|
- *
|
|
|
- * @param file
|
|
|
- */
|
|
|
- public void deleteFile(File file) throws Exception {
|
|
|
- //取得这个目录下的所有子文件对象
|
|
|
- File[] files = file.listFiles();
|
|
|
- //遍历该目录下的文件对象
|
|
|
- for (File f : files) {
|
|
|
- //打印文件名
|
|
|
- //判断子目录是否存在子目录,如果是文件则删除
|
|
|
- if (f.isDirectory()) {
|
|
|
- deleteFile(f);
|
|
|
- } else {
|
|
|
- f.delete();
|
|
|
- }
|
|
|
- }
|
|
|
- //删除空文件夹 for循环已经把上一层节点的目录清空。
|
|
|
- file.delete();
|
|
|
- }
|
|
|
}
|