|
@@ -9,7 +9,6 @@ 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;
|
|
@@ -141,10 +140,69 @@ public class CollectApi {
|
|
|
return list;
|
|
|
}
|
|
|
|
|
|
- File srcFile = new File("/Users/king/stmms-ms/static/thumbs/6/SX/1/1901040150.jpg"); //初始文件
|
|
|
+ File srcFile = new File("/Users/king/stmms-ms/static/images/6/SM/1/1901040150.jpg"); //初始文件
|
|
|
|
|
|
/**
|
|
|
- * 上传图片到服务器(图片加密)
|
|
|
+ * 上传原图(图片加密)
|
|
|
+ *
|
|
|
+ * @param workId
|
|
|
+ * @param subjectId
|
|
|
+ * @param fileName
|
|
|
+ * @param request
|
|
|
+ * @param response
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ @RequestMapping(value = "file/image/uploadsheet/{workId}/{subjectId}/{fileName}", method = {RequestMethod.GET})
|
|
|
+ public void imageUploadSheet(@PathVariable Long workId, @PathVariable Integer subjectId,
|
|
|
+ @PathVariable String fileName,
|
|
|
+ HttpServletRequest request, HttpServletResponse response) throws IOException {
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
+ LOGGER.info("准备生成原图:{}", start);
|
|
|
+ InputStream inputStream = null;
|
|
|
+ OutputStream outputStream = null;
|
|
|
+ try {
|
|
|
+// inputStream = request.getInputStream();
|
|
|
+ inputStream = new FileInputStream(srcFile);
|
|
|
+ Student student = studentRepo.findByWorkIdAndExamNumber(workId, fileName);
|
|
|
+ Subject subject = Subject.values()[subjectId - 1];
|
|
|
+
|
|
|
+ //保存原图
|
|
|
+ String sheetDir = systemConfig.getSheetDir() + File.separator + workId + File.separator + subject
|
|
|
+ + File.separator + student.getAreaCode();
|
|
|
+ File out = new File(sheetDir);
|
|
|
+ if (!out.exists()) {
|
|
|
+ out.mkdirs();
|
|
|
+ }
|
|
|
+
|
|
|
+ String imageMd5 = getImageRuleMd5(workId, subject.ordinal(), student.getAreaCode(), student.getExamNumber(), student.getId());
|
|
|
+ File sheetFile = new File(sheetDir + File.separator + imageMd5 + ".jpg");
|
|
|
+ LOGGER.info("sheet存放目录:{}", sheetFile.getPath());
|
|
|
+ outputStream = new FileOutputStream(sheetFile);
|
|
|
+ writeStream(inputStream, outputStream);
|
|
|
+ long end = System.currentTimeMillis();
|
|
|
+ LOGGER.info("生成原图耗时:{}", (end - start) / 1000 + "s");
|
|
|
+
|
|
|
+// String md5 = request.getHeader("md5");
|
|
|
+// in = new FileInputStream(imageFile);
|
|
|
+// String sheetMD5 = DigestUtils.md5Hex(in);
|
|
|
+// if (!md5.equalsIgnoreCase(sheetMD5)) {
|
|
|
+// throw new Exception("图片md5值不一致");
|
|
|
+// }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ if (Objects.nonNull(inputStream)) {
|
|
|
+ inputStream.close();
|
|
|
+ }
|
|
|
+ if (Objects.nonNull(outputStream)) {
|
|
|
+ outputStream.flush();
|
|
|
+ outputStream.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 上传裁切原图和缩略图到服务器(图片加密)
|
|
|
*
|
|
|
* @param workId
|
|
|
* @param subjectId
|
|
@@ -157,52 +215,73 @@ public class CollectApi {
|
|
|
public void imageUpload(@PathVariable Long workId, @PathVariable Integer subjectId,
|
|
|
@PathVariable String fileName,
|
|
|
HttpServletRequest request, HttpServletResponse response) throws IOException {
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
+ LOGGER.info("准备生成裁切原图和缩略图:{}", start);
|
|
|
InputStream inputStream = null;
|
|
|
OutputStream outputStream = null;
|
|
|
+ OutputStream outputStreamTemp = null;
|
|
|
try {
|
|
|
// inputStream = request.getInputStream();
|
|
|
-// String md5 = request.getHeader("md5");
|
|
|
- inputStream = PictureUtil.getInput(srcFile);
|
|
|
+ inputStream = new FileInputStream(srcFile);
|
|
|
Student student = studentRepo.findByWorkIdAndExamNumber(workId, fileName);
|
|
|
Subject subject = Subject.values()[subjectId - 1];
|
|
|
- //保存遮盖图+文件名加密
|
|
|
- String savePath = systemConfig.getImageDir() + File.separator + workId + File.separator + subject
|
|
|
+ //保存裁切原图+文件名加密
|
|
|
+ String imageDir = systemConfig.getImageDir() + File.separator + workId + File.separator + subject
|
|
|
+ File.separator + student.getAreaCode();
|
|
|
- File out = new File(savePath);
|
|
|
+ File out = new File(imageDir);
|
|
|
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 imageMd5 = getImageRuleMd5(workId, subject.ordinal(), student.getAreaCode(), student.getExamNumber(), student.getId());
|
|
|
+ File imageFile = new File(imageDir + File.separator + imageMd5 + ".jpg");
|
|
|
+ File imageFileTemp = new File(imageDir + File.separator + imageMd5 + "temp.jpg");
|
|
|
+ LOGGER.info("image存放目录:{}", imageFile.getPath());
|
|
|
+ outputStream = new FileOutputStream(imageFile);
|
|
|
+ outputStreamTemp = new FileOutputStream(imageFileTemp);
|
|
|
+ int index = 0;
|
|
|
+ byte[] bytes = new byte[1024];
|
|
|
+ byte[] bytesEnc = new byte[1024];
|
|
|
+ while ((index = inputStream.read(bytes)) != -1) {
|
|
|
+ //将字节数组的数据全部写入到输出流中
|
|
|
+ for (int i = 0; i < index; i++) {
|
|
|
+ //通过异或运算加密
|
|
|
+ bytesEnc[i] = (byte) (bytes[i] ^ SystemConstant.SECRET_KEY);
|
|
|
+ }
|
|
|
+ outputStream.write(bytesEnc, 0, index);
|
|
|
+ outputStreamTemp.write(bytes, 0, index);
|
|
|
}
|
|
|
|
|
|
- // 生成缩略图
|
|
|
+ //生成缩略图
|
|
|
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);
|
|
|
+
|
|
|
+ BufferedImage bufferedImage = ImageCompression.compress(imageFileTemp, compressionConfig);
|
|
|
+ File thumbFile = new File(thumbDir + File.separator + imageMd5 + ".jpg");
|
|
|
+// File thumbFileTemp = new File(thumbDir + File.separator + imageMd5 + "temp.jpg");
|
|
|
+ LOGGER.info("thumb存放目录:{}", thumbFile.getPath());
|
|
|
+ outputStream = new FileOutputStream(thumbFile);
|
|
|
+// outputStreamTemp = new FileOutputStream(thumbFileTemp);
|
|
|
+// ImageIO.write(bufferedImage, "jpg", outputStreamTemp);
|
|
|
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
|
|
|
+ ImageIO.write(bufferedImage, "gif", os);
|
|
|
+ inputStream = new ByteArrayInputStream(os.toByteArray());
|
|
|
+
|
|
|
+ //原图删除
|
|
|
+ imageFileTemp.delete();
|
|
|
+// inputStream = PictureUtil.getInput(thumbFileTemp);
|
|
|
+ //缩略图删除
|
|
|
+// thumbFileTemp.delete();
|
|
|
+ writeStream(inputStream, outputStream);
|
|
|
+ long end = System.currentTimeMillis();
|
|
|
+ LOGGER.info("生成原图和缩略图耗时:{}", (end - start) / 1000 + "s");
|
|
|
+
|
|
|
+// String md5 = request.getHeader("md5");
|
|
|
+// FileInputStream in = new FileInputStream(imageFile);
|
|
|
// String sliceMD5 = DigestUtils.md5Hex(in);
|
|
|
// if (!md5.equalsIgnoreCase(sliceMD5)) {
|
|
|
// throw new Exception("图片md5值不一致");
|
|
@@ -213,10 +292,14 @@ public class CollectApi {
|
|
|
if (Objects.nonNull(inputStream)) {
|
|
|
inputStream.close();
|
|
|
}
|
|
|
- if (Objects.nonNull(inputStream)) {
|
|
|
+ if (Objects.nonNull(outputStream)) {
|
|
|
outputStream.flush();
|
|
|
outputStream.close();
|
|
|
}
|
|
|
+ if (Objects.nonNull(outputStreamTemp)) {
|
|
|
+ outputStreamTemp.flush();
|
|
|
+ outputStreamTemp.close();
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -235,41 +318,45 @@ public class CollectApi {
|
|
|
public void imageDownload(@PathVariable Long workId, @PathVariable Integer subjectId,
|
|
|
@PathVariable Long studentId, @PathVariable Integer imageType,
|
|
|
HttpServletRequest request, HttpServletResponse response) throws IOException {
|
|
|
- //读取路径下面的文件
|
|
|
- InputStream in = null;
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
+ LOGGER.info("准备读取图片:{},imageType", start, imageType);
|
|
|
+ InputStream inputStream = null;
|
|
|
OutputStream outputStream = null;
|
|
|
try {
|
|
|
+ response.setContentType("image/jpg");
|
|
|
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");
|
|
|
+ String path = null;
|
|
|
+ switch (imageType) {
|
|
|
+ case 1:
|
|
|
+ //裁切原图
|
|
|
+ path = systemConfig.getImageDir() + File.separator + workId + File.separator + subject
|
|
|
+ + File.separator + student.getAreaCode();
|
|
|
+ break;
|
|
|
+ case 2:
|
|
|
+ //缩略图
|
|
|
+ path = systemConfig.getThumbDir() + File.separator + workId + File.separator + subject
|
|
|
+ + File.separator + student.getAreaCode();
|
|
|
+ break;
|
|
|
+ case 3:
|
|
|
+ //原图
|
|
|
+ path = systemConfig.getSheetDir() + File.separator + workId + File.separator + subject
|
|
|
+ + File.separator + student.getAreaCode();
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ //分数水印图
|
|
|
+ path = systemConfig.getSheetDir() + File.separator + workId + File.separator + subject
|
|
|
+ + File.separator + student.getAreaCode();
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ String md5 = getImageRuleMd5(workId, subject.ordinal(), student.getAreaCode(), student.getExamNumber(), student.getId());
|
|
|
+ File file = new File(path + File.separator + md5 + ".jpg");
|
|
|
//读取指定路径下面的文件
|
|
|
- in = new FileInputStream(file);
|
|
|
+ inputStream = new FileInputStream(file);
|
|
|
outputStream = new BufferedOutputStream(response.getOutputStream());
|
|
|
- int dataOfFile = 0;
|
|
|
- while ((dataOfFile = in.read()) > -1) {
|
|
|
- outputStream.write(dataOfFile ^ SystemConstant.SECRET_KEY);
|
|
|
- }
|
|
|
+ writeStream(inputStream, outputStream);
|
|
|
+ long end = System.currentTimeMillis();
|
|
|
+ LOGGER.info("读取图片耗时:{}", (end - start) / 1000 + "s");
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
} finally {
|
|
@@ -279,8 +366,8 @@ public class CollectApi {
|
|
|
//关流
|
|
|
outputStream.close();
|
|
|
}
|
|
|
- if (Objects.nonNull(in)) {
|
|
|
- in.close();
|
|
|
+ if (Objects.nonNull(inputStream)) {
|
|
|
+ inputStream.close();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -392,17 +479,40 @@ public class CollectApi {
|
|
|
* @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 {
|
|
|
+ public String getImageRuleMd5(Long workId, int subjectId, String areaCode, String examNumber, Long studentId) {
|
|
|
//暂不用Aes解密,因为没用到反解
|
|
|
- StringBuffer stringBuffer = new StringBuffer(String.valueOf(workId)).append(subjectId).append(areaCode).append(examNumber).append(studentId).append(imageType);
|
|
|
+ StringBuffer stringBuffer = new StringBuffer(String.valueOf(workId)).append(subjectId).append(areaCode).append(examNumber).append(studentId);
|
|
|
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());
|
|
|
+ if (rule.length() < 16) {
|
|
|
+ rule = String.format("%016d", Long.parseLong(rule));
|
|
|
+ LOGGER.info("rule补零后:{},length:{}", rule, rule.length());
|
|
|
+ }
|
|
|
return MD5Util.encoder(rule);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 流写入
|
|
|
+ *
|
|
|
+ * @param inputStream
|
|
|
+ * @param outputStream
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public OutputStream writeStream(InputStream inputStream, OutputStream outputStream) throws IOException {
|
|
|
+ int index = 0;
|
|
|
+ byte[] bytes = new byte[1024];
|
|
|
+ byte[] bytesEnc = new byte[1024];
|
|
|
+ while ((index = inputStream.read(bytes)) != -1) {
|
|
|
+ //将字节数组的数据全部写入到输出流中
|
|
|
+ for (int i = 0; i < index; i++) {
|
|
|
+ //通过异或运算加密
|
|
|
+ bytesEnc[i] = (byte) (bytes[i] ^ SystemConstant.SECRET_KEY);
|
|
|
+ }
|
|
|
+ outputStream.write(bytesEnc, 0, index);
|
|
|
+ }
|
|
|
+ return outputStream;
|
|
|
+ }
|
|
|
}
|