瀏覽代碼

导出和上传路径格式化

wangliang 4 年之前
父節點
當前提交
17d77273b8

+ 4 - 4
themis-business/src/main/java/com/qmth/themis/business/service/impl/TBAttachmentServiceImpl.java

@@ -115,8 +115,8 @@ public class TBAttachmentServiceImpl extends ServiceImpl<TBAttachmentMapper, TBA
                 stringJoiner.add(uploadType).add(File.separator);
             }
             stringJoiner.add(String.valueOf(nowTime.getYear())).add(File.separator)
-                    .add(String.valueOf(nowTime.getMonthValue())).add(File.separator)
-                    .add(String.valueOf(nowTime.getDayOfMonth()));
+                    .add(String.format("%02d", nowTime.getMonthValue())).add(File.separator)
+                    .add(String.format("%02d", nowTime.getDayOfMonth()));
             if (oss) {//上传至oss
                 stringJoiner.add(File.separator).add(String.valueOf(UUID.randomUUID()).replaceAll("-", ""))
                         .add(tbAttachment.getType());
@@ -201,8 +201,8 @@ public class TBAttachmentServiceImpl extends ServiceImpl<TBAttachmentMapper, TBA
                 stringJoiner.add(uploadType);
             }
             stringJoiner.add(File.separator).add(String.valueOf(nowTime.getYear())).add(File.separator)
-                    .add(String.valueOf(nowTime.getMonthValue())).add(File.separator)
-                    .add(String.valueOf(nowTime.getDayOfMonth()));
+                    .add(String.format("%02d", nowTime.getMonthValue())).add(File.separator)
+                    .add(String.format("%02d", nowTime.getDayOfMonth()));
             if (oss) {//上传至oss
                 stringJoiner.add(File.separator).add(String.valueOf(UUID.randomUUID()).replaceAll("-", ""))
                         .add(tbAttachment.getType());

+ 14 - 13
themis-business/src/main/java/com/qmth/themis/business/templete/TaskExportCommon.java

@@ -17,10 +17,7 @@ import org.apache.poi.ss.usermodel.Row;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.FileWriter;
-import java.io.IOException;
+import java.io.*;
 import java.util.*;
 
 /**
@@ -110,15 +107,19 @@ public class TaskExportCommon {
     /**
      * oss上传
      *
-     * @param file
+     * @param inputStream
+     * @param path
      * @throws FileNotFoundException
      */
-    public void ossUpload(File file) throws IOException {
+    public String ossUpload(InputStream inputStream, String path) throws IOException {
+        StringJoiner stringJoiner = new StringJoiner("");
         boolean oss = (boolean) this.getOssEnv().get(SystemConstant.OSS);
         if (oss) {//上传至oss
-            this.ossUtil.upload(false, file.getPath().replaceAll("\\\\", "/"), file);
-            file.delete();
+            stringJoiner.add(path).add(File.separator).add(String.valueOf(UUID.randomUUID()).replaceAll("-", ""))
+                    .add(SystemConstant.EXCEL_PREFIX).toString();
+            this.ossUtil.upload(false, stringJoiner.toString(), inputStream);
         }
+        return stringJoiner.toString();
     }
 
     /**
@@ -140,10 +141,10 @@ public class TaskExportCommon {
      *
      * @param path
      * @param txtStr
-     * @param excelFile
+     * @param excelFilePath
      * @throws IOException
      */
-    public void writeExportResultTxt(String path, String txtStr, File excelFile) throws IOException {
+    public void writeExportResultTxt(String path, String txtStr, String excelFilePath) throws IOException {
         FileWriter fileWriter = null;
         try {
             File file = new File(path + File.separator + String.valueOf(UUID.randomUUID()).replaceAll("-", "")
@@ -163,12 +164,12 @@ public class TaskExportCommon {
             json.put("type", this.type);
             json.put(SystemConstant.UPLOAD_TYPE, UploadFileEnum.file.ordinal());
             this.tbTaskHistory.setReportFilePath(json.toJSONString());
-            if (Objects.nonNull(excelFile)) {
+            if (Objects.nonNull(excelFilePath)) {
                 JSONObject excelJson = new JSONObject();
                 if (Objects.equals(this.type, SystemConstant.OSS)) {
-                    excelJson.put("path", excelFile.getPath().replaceAll("\\\\", "/"));
+                    excelJson.put("path", excelFilePath);
                 } else {
-                    excelJson.put("path", excelFile.getPath());
+                    excelJson.put("path", excelFilePath);
                 }
                 excelJson.put("type", this.type);
                 excelJson.put(SystemConstant.UPLOAD_TYPE, UploadFileEnum.file.ordinal());

+ 9 - 16
themis-business/src/main/java/com/qmth/themis/business/templete/impl/TaskExamStudentExportTemplete.java

@@ -15,6 +15,7 @@ import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.time.LocalDateTime;
@@ -47,24 +48,20 @@ public class TaskExamStudentExportTemplete implements TaskExportTemplete {
         TaskExportCommon taskExportCommon = new TaskExportCommon(transMap);
         taskExportCommon.init();
         FileOutputStream fos = null;
-        File mkdir = null;
         File file = null;
-
+        StringJoiner stringJoiner = null;
+        String excelFilePath = null;
         boolean oss = (boolean) taskExportCommon.getOssEnv().get(SystemConstant.OSS);
         try {
-            StringJoiner stringJoiner = new StringJoiner("");
+            stringJoiner = new StringJoiner("");
             if (!oss) {
                 stringJoiner.add(SystemConstant.FILES_DIR).add(File.separator);
             }
             LocalDateTime nowTime = LocalDateTime.now();
             stringJoiner.add(String.valueOf(nowTime.getYear()))
-                    .add(File.separator).add(String.valueOf(nowTime.getMonthValue()))
-                    .add(File.separator).add(String.valueOf(nowTime.getDayOfMonth()));
-            mkdir = new File(stringJoiner.toString());
-            if (!mkdir.exists()) {
-                mkdir.mkdirs();
-            }
-            file = new File(mkdir.getPath() + "/" + uuid() + SystemConstant.EXCEL_PREFIX);
+                    .add(File.separator).add(String.format("%02d", nowTime.getMonthValue()))
+                    .add(File.separator).add(String.format("%02d", nowTime.getDayOfMonth()));
+            file = new File(SystemConstant.TEMP_FILES_DIR + "/" + uuid() + SystemConstant.EXCEL_PREFIX);
             file.createNewFile();
             fos = new FileOutputStream(file);
             Long examId = (Long) transMap.get("examId");
@@ -80,7 +77,7 @@ public class TaskExamStudentExportTemplete implements TaskExportTemplete {
             List<TEExamStudentDto> list = teExamStudentService.examStudentList(examId, activityId, identity, name, roomCode, courseCode, grade,
                     enable, classNo, hasPhoto);
             ExportUtils.makeExcel(TEExamStudentDto.class, list, fos);
-            taskExportCommon.ossUpload(file);
+            excelFilePath = taskExportCommon.ossUpload(new FileInputStream(file), stringJoiner.toString());
             taskExportCommon.getTbTaskHistory().setSummary("处理成功");
             long end = System.currentTimeMillis();
             log.info("导出考生数据结束,============耗时============:{}秒", (end - start) / 1000);
@@ -89,10 +86,6 @@ public class TaskExamStudentExportTemplete implements TaskExportTemplete {
             taskExportCommon.setException(true);
             taskExportCommon.getTbTaskHistory().setSummary("处理出错");
         } finally {
-            if (Objects.nonNull(fos)) {
-                fos.flush();
-                fos.close();
-            }
             if (oss && file != null) {
                 file.delete();
             }
@@ -100,7 +93,7 @@ public class TaskExamStudentExportTemplete implements TaskExportTemplete {
             taskExportCommon.getTbTaskHistory().setStatus(TaskStatusEnum.FINISH);
             taskExportCommon.getTbTaskHistory().setProgress(100.0d);
             //这里写入txt文件
-            taskExportCommon.writeExportResultTxt(mkdir.getPath(), "", file);
+            taskExportCommon.writeExportResultTxt(stringJoiner.toString(), "", excelFilePath);
         }
         return ResultUtil.ok(Collections.singletonMap(SystemConstant.SUCCESS, true));
     }

+ 21 - 21
themis-business/src/main/java/com/qmth/themis/business/templete/impl/TaskMarkResultSimpleExportTemplete.java

@@ -24,9 +24,7 @@ import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.IOException;
+import java.io.*;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Field;
 import java.time.LocalDateTime;
@@ -69,9 +67,11 @@ public class TaskMarkResultSimpleExportTemplete implements TaskExportTemplete {
         taskExportCommon.init();
         String timeFormat = taskExportCommon.getTimeFormat();
         taskExportCommon.setTxtList(new ArrayList());
-        FileOutputStream fos = null;
         SXSSFWorkbook wb = null;
-        File mkdir = null, file = null;
+        StringJoiner stringJoiner = null;
+        String excelFilePath = null;
+        InputStream inputStream = null;
+        ByteArrayOutputStream out = null;
         int y = 0, min = 0;
         try {
             taskExportCommon.getTxtList().add(DateUtil.format(new Date(), timeFormat) + "->开始准备处理导出的成绩简版数据");
@@ -98,18 +98,14 @@ public class TaskMarkResultSimpleExportTemplete implements TaskExportTemplete {
                 }
             }
             boolean oss = (boolean) taskExportCommon.getOssEnv().get(SystemConstant.OSS);
-            StringJoiner stringJoiner = new StringJoiner("");
+            stringJoiner = new StringJoiner("");
             if (!oss) {
                 stringJoiner.add(SystemConstant.FILES_DIR).add(File.separator);
             }
             LocalDateTime nowTime = LocalDateTime.now();
             stringJoiner.add(String.valueOf(nowTime.getYear()))
-                    .add(File.separator).add(String.valueOf(nowTime.getMonthValue()))
-                    .add(File.separator).add(String.valueOf(nowTime.getDayOfMonth()));
-            mkdir = new File(stringJoiner.toString());
-            if (!mkdir.exists()) {
-                mkdir.mkdirs();
-            }
+                    .add(File.separator).add(String.format("%02d", nowTime.getMonthValue()))
+                    .add(File.separator).add(String.format("%02d", nowTime.getDayOfMonth()));
             //创建excel
             wb = new SXSSFWorkbook();
             Sheet sheet = wb.createSheet("成绩统计简版");
@@ -164,11 +160,12 @@ public class TaskMarkResultSimpleExportTemplete implements TaskExportTemplete {
                     max = size;
                 }
             }
-            file = taskExportCommon.createExcel(mkdir.getPath());
-            //写入excel并上传附件
-            fos = new FileOutputStream(file);
-            wb.write(fos);
-            taskExportCommon.ossUpload(file);
+            //临时缓冲区
+            out = new ByteArrayOutputStream();
+            wb.write(out);
+            byte[] bookByteAry = out.toByteArray();
+            inputStream = new ByteArrayInputStream(bookByteAry);
+            excelFilePath = taskExportCommon.ossUpload(inputStream, stringJoiner.toString());
             long end = System.currentTimeMillis();
             log.info("导出成绩简版数据结束,============耗时============:{}秒", (end - start) / 1000);
         } catch (Exception e) {
@@ -178,12 +175,15 @@ public class TaskMarkResultSimpleExportTemplete implements TaskExportTemplete {
             if (Objects.nonNull(wb)) {
                 wb.dispose();
             }
-            if (Objects.nonNull(fos)) {
-                fos.flush();
-                fos.close();
+            if (Objects.nonNull(inputStream)) {
+                inputStream.close();
+            }
+            if (Objects.nonNull(out)) {
+                out.flush();
+                out.close();
             }
             //这里写入txt文件
-            taskExportCommon.writeExportResultTxt(mkdir.getPath(), taskExportCommon.getTxtList().toString(), file);
+            taskExportCommon.writeExportResultTxt(stringJoiner.toString(), taskExportCommon.getTxtList().toString(), excelFilePath);
         }
         return ResultUtil.ok(Collections.singletonMap(SystemConstant.SUCCESS, true));
     }

+ 21 - 21
themis-business/src/main/java/com/qmth/themis/business/templete/impl/TaskMarkResultStandardExportTemplete.java

@@ -30,9 +30,7 @@ import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.IOException;
+import java.io.*;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Field;
 import java.time.LocalDateTime;
@@ -81,9 +79,11 @@ public class TaskMarkResultStandardExportTemplete implements TaskExportTemplete
         taskExportCommon.init();
         String timeFormat = taskExportCommon.getTimeFormat();
         taskExportCommon.setTxtList(new ArrayList());
-        FileOutputStream fos = null;
         SXSSFWorkbook wb = null;
-        File mkdir = null, file = null;
+        StringJoiner stringJoiner = null;
+        String excelFilePath = null;
+        InputStream inputStream = null;
+        ByteArrayOutputStream out = null;
         int y = 0, min = 0;
         try {
             taskExportCommon.getTxtList().add(DateUtil.format(new Date(), timeFormat) + "->开始准备处理导出的成绩标准版数据");
@@ -184,18 +184,14 @@ public class TaskMarkResultStandardExportTemplete implements TaskExportTemplete
                 }
             }
             boolean oss = (boolean) taskExportCommon.getOssEnv().get(SystemConstant.OSS);
-            StringJoiner stringJoiner = new StringJoiner("");
+            stringJoiner = new StringJoiner("");
             if (!oss) {
                 stringJoiner.add(SystemConstant.FILES_DIR).add(File.separator);
             }
             LocalDateTime nowTime = LocalDateTime.now();
             stringJoiner.add(String.valueOf(nowTime.getYear()))
-                    .add(File.separator).add(String.valueOf(nowTime.getMonthValue()))
-                    .add(File.separator).add(String.valueOf(nowTime.getDayOfMonth()));
-            mkdir = new File(stringJoiner.toString());
-            if (!mkdir.exists()) {
-                mkdir.mkdirs();
-            }
+                    .add(File.separator).add(String.format("%02d", nowTime.getMonthValue()))
+                    .add(File.separator).add(String.format("%02d", nowTime.getDayOfMonth()));
             //创建excel
             wb = new SXSSFWorkbook();
             Sheet sheet = wb.createSheet("成绩统计标准版");
@@ -275,11 +271,12 @@ public class TaskMarkResultStandardExportTemplete implements TaskExportTemplete
                     max = size;
                 }
             }
-            file = taskExportCommon.createExcel(mkdir.getPath());
-            //写入excel并上传附件
-            fos = new FileOutputStream(file);
-            wb.write(fos);
-            taskExportCommon.ossUpload(file);
+            //临时缓冲区
+            out = new ByteArrayOutputStream();
+            wb.write(out);
+            byte[] bookByteAry = out.toByteArray();
+            inputStream = new ByteArrayInputStream(bookByteAry);
+            excelFilePath = taskExportCommon.ossUpload(inputStream, stringJoiner.toString());
             long end = System.currentTimeMillis();
             log.info("导出成绩标准版数据结束,============耗时============:{}秒", (end - start) / 1000);
         } catch (Exception e) {
@@ -289,12 +286,15 @@ public class TaskMarkResultStandardExportTemplete implements TaskExportTemplete
             if (Objects.nonNull(wb)) {
                 wb.dispose();
             }
-            if (Objects.nonNull(fos)) {
-                fos.flush();
-                fos.close();
+            if (Objects.nonNull(inputStream)) {
+                inputStream.close();
+            }
+            if (Objects.nonNull(out)) {
+                out.flush();
+                out.close();
             }
             //这里写入txt文件
-            taskExportCommon.writeExportResultTxt(mkdir.getPath(), taskExportCommon.getTxtList().toString(), file);
+            taskExportCommon.writeExportResultTxt(stringJoiner.toString(), taskExportCommon.getTxtList().toString(), excelFilePath);
         }
         return ResultUtil.ok(Collections.singletonMap(SystemConstant.SUCCESS, true));
     }

+ 21 - 21
themis-business/src/main/java/com/qmth/themis/business/templete/impl/TaskRoomCodeExportTemplete.java

@@ -17,9 +17,7 @@ import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.IOException;
+import java.io.*;
 import java.lang.reflect.Field;
 import java.time.LocalDateTime;
 import java.util.*;
@@ -55,9 +53,11 @@ public class TaskRoomCodeExportTemplete implements TaskExportTemplete {
         taskExportCommon.init();
         String timeFormat = taskExportCommon.getTimeFormat();
         taskExportCommon.setTxtList(new ArrayList());
-        FileOutputStream fos = null;
         SXSSFWorkbook wb = null;
-        File mkdir = null, file = null;
+        StringJoiner stringJoiner = null;
+        String excelFilePath = null;
+        InputStream inputStream = null;
+        ByteArrayOutputStream out = null;
         int y = 0, min = 0;
         try {
             taskExportCommon.getTxtList().add(DateUtil.format(new Date(), timeFormat) + "->开始准备处理导出的考场数据");
@@ -67,18 +67,14 @@ public class TaskRoomCodeExportTemplete implements TaskExportTemplete {
             List<RoomCodeExportDto> roomCodeExportDtoList = tbExamInvigilateUserService.examInvigilateUserExport(examId, roomCode, userId, taskExportCommon.getOrgId());
 
             boolean oss = (boolean) taskExportCommon.getOssEnv().get(SystemConstant.OSS);
-            StringJoiner stringJoiner = new StringJoiner("");
+            stringJoiner = new StringJoiner("");
             if (!oss) {
                 stringJoiner.add(SystemConstant.FILES_DIR).add(File.separator);
             }
             LocalDateTime nowTime = LocalDateTime.now();
             stringJoiner.add(String.valueOf(nowTime.getYear()))
-                    .add(File.separator).add(String.valueOf(nowTime.getMonthValue()))
-                    .add(File.separator).add(String.valueOf(nowTime.getDayOfMonth()));
-            mkdir = new File(stringJoiner.toString());
-            if (!mkdir.exists()) {
-                mkdir.mkdirs();
-            }
+                    .add(File.separator).add(String.format("%02d", nowTime.getMonthValue()))
+                    .add(File.separator).add(String.format("%02d", nowTime.getDayOfMonth()));
             //创建excel
             wb = new SXSSFWorkbook();
             Sheet sheet = wb.createSheet("考场导出");
@@ -127,11 +123,12 @@ public class TaskRoomCodeExportTemplete implements TaskExportTemplete {
                     max = size;
                 }
             }
-            file = taskExportCommon.createExcel(mkdir.getPath());
-            //写入excel并上传附件
-            fos = new FileOutputStream(file);
-            wb.write(fos);
-            taskExportCommon.ossUpload(file);
+            //临时缓冲区
+            out = new ByteArrayOutputStream();
+            wb.write(out);
+            byte[] bookByteAry = out.toByteArray();
+            inputStream = new ByteArrayInputStream(bookByteAry);
+            excelFilePath = taskExportCommon.ossUpload(inputStream, stringJoiner.toString());
             long end = System.currentTimeMillis();
             log.info("导出考场数据结束,============耗时============:{}秒", (end - start) / 1000);
         } catch (Exception e) {
@@ -141,12 +138,15 @@ public class TaskRoomCodeExportTemplete implements TaskExportTemplete {
             if (Objects.nonNull(wb)) {
                 wb.dispose();
             }
-            if (Objects.nonNull(fos)) {
-                fos.flush();
-                fos.close();
+            if (Objects.nonNull(inputStream)) {
+                inputStream.close();
+            }
+            if (Objects.nonNull(out)) {
+                out.flush();
+                out.close();
             }
             //这里写入txt文件
-            taskExportCommon.writeExportResultTxt(mkdir.getPath(), taskExportCommon.getTxtList().toString(), file);
+            taskExportCommon.writeExportResultTxt(stringJoiner.toString(), taskExportCommon.getTxtList().toString(), excelFilePath);
         }
         return ResultUtil.ok(Collections.singletonMap(SystemConstant.SUCCESS, true));
     }

+ 19 - 0
themis-business/src/main/java/com/qmth/themis/business/util/OssUtil.java

@@ -96,6 +96,7 @@ public class OssUtil {
      * @param isPublic
      * @param objectName
      * @param inputStream
+     * @param md5
      */
     public void upload(boolean isPublic, String objectName, InputStream inputStream, String md5) {
         log.info("oss  upload stream is come in");
@@ -110,6 +111,24 @@ public class OssUtil {
         log.info("objectName:{},requestid:{}", objectName, por.getRequestId());
     }
 
+    /**
+     * oss上传数据流
+     *
+     * @param isPublic
+     * @param objectName
+     * @param inputStream
+     */
+    public void upload(boolean isPublic, String objectName, InputStream inputStream) {
+        log.info("oss  upload stream is come in");
+        String bucket = isPublic ? aliYunOssPublicDomain.getPublicBucket() : aliYunOssPrivateDomain.getPrivateBucket();
+        OSS client = isPublic ? publicClient : privateClient;
+        ObjectMetadata meta = new ObjectMetadata();
+        // 上传内容到指定的存储空间(bucketName)并保存为指定的文件名称(objectName)。
+        PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, objectName, inputStream);
+        PutObjectResult por = client.putObject(putObjectRequest);
+        log.info("objectName:{},requestid:{}", objectName, por.getRequestId());
+    }
+
     /**
      * oss上传本地文件
      *