Procházet zdrojové kódy

easyExcel读取文件添加cleanUp

chenjun před 1 rokem
rodič
revize
2f797e0ac6

+ 10 - 6
zjugis-framework/zjugis-spring-boot-starter-excel/src/main/java/com/zjugis/framework/excel/core/util/ExcelUtils.java

@@ -2,10 +2,12 @@ package com.zjugis.framework.excel.core.util;
 
 import com.alibaba.excel.EasyExcel;
 import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
+import lombok.Cleanup;
 import org.springframework.web.multipart.MultipartFile;
 
 import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
+import java.io.InputStream;
 import java.net.URLEncoder;
 import java.util.List;
 
@@ -19,12 +21,12 @@ public class ExcelUtils {
     /**
      * 将列表以 Excel 响应给前端
      *
-     * @param response 响应
-     * @param filename 文件名
+     * @param response  响应
+     * @param filename  文件名
      * @param sheetName Excel sheet 名
-     * @param head Excel head 头
-     * @param data 数据列表哦
-     * @param <T> 泛型,保证 head 和 data 类型的一致性
+     * @param head      Excel head 头
+     * @param data      数据列表哦
+     * @param <T>       泛型,保证 head 和 data 类型的一致性
      * @throws IOException 写入失败的情况
      */
     public static <T> void write(HttpServletResponse response, String filename, String sheetName,
@@ -40,7 +42,9 @@ public class ExcelUtils {
     }
 
     public static <T> List<T> read(MultipartFile file, Class<T> head) throws IOException {
-       return EasyExcel.read(file.getInputStream(), head, null)
+        @Cleanup
+        InputStream inputStream = file.getInputStream();
+        return EasyExcel.read(inputStream, head, null)
                 .autoCloseStream(false)  // 不要自动关闭,交给 Servlet 自己处理
                 .doReadAllSync();
     }