浏览代码

close io streams

deason 5 年之前
父节点
当前提交
959358a061

+ 7 - 5
examcloud-core-questions-base/src/main/java/cn/com/qmth/examcloud/core/questions/base/excel/ExcelWriter.java

@@ -153,13 +153,9 @@ public class ExcelWriter extends ExcelUtils {
                                 // 是数字当作double处理
                                 cell.setCellValue(Double.parseDouble(textValue));
                             } else {
-                                XSSFRichTextString richString = new XSSFRichTextString(
-                                        textValue);
-
+                                XSSFRichTextString richString = new XSSFRichTextString(textValue);
                                 cell.setCellValue(richString);
                             }
-                        } else {
-                            textValue = "";
                         }
                     } catch (NoSuchFieldException e) {
                         log.error(e.getMessage(), e);
@@ -176,6 +172,12 @@ public class ExcelWriter extends ExcelUtils {
             workbook.write(out);
         } catch (IOException e) {
             log.error(e.getMessage(), e);
+        } finally {
+            try {
+                out.close();
+            } catch (IOException e) {
+                //ignore
+            }
         }
     }
 

+ 11 - 6
examcloud-core-questions-base/src/main/java/cn/com/qmth/examcloud/core/questions/base/excel/ExportUtils.java

@@ -5,6 +5,7 @@ import org.slf4j.LoggerFactory;
 
 import javax.servlet.ServletOutputStream;
 import javax.servlet.http.HttpServletResponse;
+import java.io.UnsupportedEncodingException;
 import java.net.URLEncoder;
 import java.util.Collection;
 
@@ -18,17 +19,21 @@ public class ExportUtils {
 
     public static void exportEXCEL(String fileName, Class<?> dataClass, Collection<?> dataset, HttpServletResponse response) {
         try {
-            response.setHeader("Content-Disposition", "inline; filename="
-                    + URLEncoder.encode(fileName, "UTF-8") + DEFALUT_EXT);
-            response.setContentType(DEFALUT_CONTENT_TYPE);
-            ServletOutputStream outputStream = response.getOutputStream();
+            fileName = URLEncoder.encode(fileName, "UTF-8");
+        } catch (UnsupportedEncodingException e) {
+            log.error(e.getMessage(), e);
+        }
+
+        response.setHeader("Content-Disposition", "inline; filename=" + fileName + DEFALUT_EXT);
+        response.setContentType(DEFALUT_CONTENT_TYPE);
+
+        try (ServletOutputStream outputStream = response.getOutputStream();) {
             ExcelWriter excelExporter = new ExcelWriter(dataClass);
             excelExporter.write("sheet1", dataset, outputStream);
             outputStream.flush();
-            outputStream.close();
         } catch (Exception e) {
             log.error(e.getMessage(), e);
         }
     }
 
-}
+}

+ 10 - 6
examcloud-core-questions-service/src/main/java/cn/com/qmth/examcloud/core/questions/service/export/ExportPaperAbstractService.java

@@ -1044,14 +1044,16 @@ public abstract class ExportPaperAbstractService {
                 objectiveQuestionStructureList.add(new ObjectiveQuestionStructure(paperExp, paperDetailExp, unit, paperType));
             }
         }
+
         ExcelWriter objectiveExcelExporter = new ExcelWriter(ObjectiveQuestionStructure.class);
-        try {
-            File file = new File(TEMP_FILE_EXP + objectiveFilename);
-            FileOutputStream out = new FileOutputStream(file);
+        File file = new File(TEMP_FILE_EXP + objectiveFilename);
+        try (FileOutputStream out = new FileOutputStream(file);) {
             objectiveExcelExporter.write(objectiveFilename, objectiveQuestionStructureList, out);
+
             String objectiveFilePath = sysProperty.getUploadPath() + extractConfig.getOrgId() + "/" + objectiveFilename;
             UpYun upyun = new UpYun(sysProperty.getBucketName(), sysProperty.getUserName(), sysProperty.getPassword());
             upyun.writeFile(objectiveFilePath, file, true);
+
             ExamFile examFile = new ExamFile(paperExp.getId(), extractConfig, objectiveFilePath, ExamFileType.PAPER_STRUCTURE_OBJECTIVE, EXCEL_SUFFIX);
             examFile.setGroupCode(examPaper.getGroupCode());
             examFile.setFileName(extractConfig.getCourseName() + "_" + extractConfig.getCourseCode() + "_" + ExamFileType.PAPER_STRUCTURE_OBJECTIVE + "_" + examPaper.getGroupCode() + EXCEL_SUFFIX);
@@ -1082,14 +1084,16 @@ public abstract class ExportPaperAbstractService {
                 subjectiveQuestionStructureList.add(new SubjectiveQuestionStructure(paperExp, paperDetailExp, unit, paperType));
             }
         }
+
         ExcelWriter subjectiveExcelExporter = new ExcelWriter(SubjectiveQuestionStructure.class);
-        try {
-            File file = new File(TEMP_FILE_EXP + subjectiveFileName);
-            FileOutputStream out = new FileOutputStream(file);
+        File file = new File(TEMP_FILE_EXP + subjectiveFileName);
+        try (FileOutputStream out = new FileOutputStream(file);) {
             subjectiveExcelExporter.write(subjectiveFileName, subjectiveQuestionStructureList, out);
+
             String subjectiveFilePath = sysProperty.getUploadPath() + extractConfig.getOrgId() + "/" + subjectiveFileName;
             UpYun upyun = new UpYun(sysProperty.getBucketName(), sysProperty.getUserName(), sysProperty.getPassword());
             upyun.writeFile(subjectiveFilePath, file, true);
+
             ExamFile examFile = new ExamFile(paperExp.getId(), extractConfig, subjectiveFilePath, ExamFileType.PAPER_STRUCTURE_SUBJECTIVE, EXCEL_SUFFIX);
             examFile.setGroupCode(examPaper.getGroupCode());
             examFile.setFileName(extractConfig.getCourseName() + "_" + extractConfig.getCourseCode() + "_" + ExamFileType.PAPER_STRUCTURE_SUBJECTIVE + "_" + examPaper.getGroupCode() + EXCEL_SUFFIX);

+ 11 - 6
examcloud-core-questions-service/src/main/java/cn/com/qmth/examcloud/core/questions/service/impl/ExtractConfigFileServiceImpl.java

@@ -531,18 +531,23 @@ public class ExtractConfigFileServiceImpl implements ExtractConfigFileService {
             index++;
         }
 
+
         ExcelWriter objectiveExcelExporter = new ExcelWriter(ObjectiveQuestionStructure.class);
         String keguanFileName = examName + "_客观题.xlsx";
-        FileOutputStream out1 = getFileOutputStream(downloadDir, keguanFileName);
-        objectiveExcelExporter.write(keguanFileName, objectiveQuestionStructureList, out1);
+        try (FileOutputStream out1 = getFileOutputStream(downloadDir, keguanFileName);) {
+            objectiveExcelExporter.write(keguanFileName, objectiveQuestionStructureList, out1);
+        } catch (IOException e) {
+            logger.error(e.getMessage(), e);
+        }
 
         ExcelWriter subjectiveExcelExporter = new ExcelWriter(SubjectiveQuestionStructure.class);
         String zhuguanFileName = examName + "_主观题.xlsx";
-        FileOutputStream out2 = getFileOutputStream(downloadDir, zhuguanFileName);
-        subjectiveExcelExporter.write(zhuguanFileName, subjectiveQuestionStructureList, out2);
+        try (FileOutputStream out2 = getFileOutputStream(downloadDir, zhuguanFileName);) {
+            subjectiveExcelExporter.write(zhuguanFileName, subjectiveQuestionStructureList, out2);
+        } catch (IOException e) {
+            logger.error(e.getMessage(), e);
+        }
 
-        out1.close();
-        out2.close();
         logger.info("批量生成试卷结构完成");
     }