yangyb 1 周之前
父節點
當前提交
6666ab22eb

+ 35 - 14
mall-service/src/main/java/com/txz/mall/service/impl/StoreOrderServiceImpl.java

@@ -42,6 +42,7 @@ import vo.*;
 import javax.servlet.http.HttpServletResponse;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
+import java.io.OutputStream;
 import java.math.BigDecimal;
 import java.net.URLEncoder;
 import java.util.*;
@@ -242,6 +243,7 @@ public class StoreOrderServiceImpl extends AbstractService<StoreOrder> implement
 
     @Override
     public void exportFile(StoreOrderDTO dto, HttpServletResponse response) {
+        OutputStream outputStream = null;
         try {
             response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
             response.setCharacterEncoding("utf-8");
@@ -255,18 +257,26 @@ public class StoreOrderServiceImpl extends AbstractService<StoreOrder> implement
                 BeanUtils.copyProperties(vo, po);
                 return po;
             }).collect(Collectors.toList());
-            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
             if (CollectionUtils.isEmpty(list)) {
                 list.add(new StoreProductPO());
             }
-            EasyExcel.write(outputStream, StoreProductPO.class).sheet("导出订单").doWrite(list);
+            EasyExcel.write(byteArrayOutputStream, StoreProductPO.class).sheet("导出订单").doWrite(list);
             // 将数据写入响应输出流
-            outputStream.writeTo(response.getOutputStream());
-            outputStream.close();
-            response.getOutputStream().flush();
+            outputStream = response.getOutputStream();
+            byteArrayOutputStream.writeTo(outputStream);
+            outputStream.flush();
         } catch (Exception e) {
             log.error("导出订单失败: ", e);
-            throw new ServiceException("模板下载失败, 请联系管理员: " + e.getMessage());
+        } finally {
+            // 正确关闭流资源
+            if (outputStream != null) {
+                try {
+                    outputStream.close();
+                } catch (IOException e) {
+                    log.error("关闭outputStream失败: ", e);
+                }
+            }
         }
     }
 
@@ -1339,26 +1349,37 @@ public class StoreOrderServiceImpl extends AbstractService<StoreOrder> implement
 
     @Override
     public void exportDelivery(HttpServletResponse response) {
+        OutputStream outputStream = null;
         try {
             response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
             response.setCharacterEncoding("utf-8");
-            String fileName = URLEncoder.encode("导出订单", "UTF-8").replaceAll("\\+", "%20");
+            String fileName = URLEncoder.encode("导出发货模版", "UTF-8").replaceAll("\\+", "%20");
             response.setHeader(
                     "Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
+
             // 使用字节数组输出流作为中间缓冲
-            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+
             // 创建一个空列表,但确保Excel文件包含表头
-            EasyExcel.write(outputStream, OrderDeliveryPO.class)
+            EasyExcel.write(byteArrayOutputStream, OrderDeliveryPO.class)
                     .sheet("导出发货模版")
                     .doWrite(new ArrayList<OrderDeliveryPO>());
 
             // 将数据写入响应输出流
-            outputStream.writeTo(response.getOutputStream());
-            outputStream.close();
-            response.getOutputStream().flush();
+            outputStream = response.getOutputStream();
+            byteArrayOutputStream.writeTo(outputStream);
+            outputStream.flush();
         } catch (Exception e) {
             log.error("导出配送模板失败: ", e);
-            throw new ServiceException("模板下载失败, 请联系管理员: " + e.getMessage());
+        } finally {
+            // 正确关闭流资源
+            if (outputStream != null) {
+                try {
+                    outputStream.close();
+                } catch (IOException e) {
+                    log.error("关闭outputStream失败: ", e);
+                }
+            }
         }
     }
 
@@ -1367,7 +1388,7 @@ public class StoreOrderServiceImpl extends AbstractService<StoreOrder> implement
         String name = file.getOriginalFilename();
         String suffix = FileUtil.extName(name);
         log.info("获取到的文件名为----->{},后缀为----->{},入参------->{}", name, suffix);
-        if ("xlsx".equals(suffix) && !"xls".equals(suffix)) {
+        if (!"xlsx".equals(suffix) && !"xls".equals(suffix)) {
             throw new ServiceException("请传入xlsx或xls文档格式文件");
         }
         try {

+ 6 - 0
mall-service/src/main/java/po/StoreProductPO.java

@@ -2,12 +2,18 @@ package po;
 
 import com.alibaba.excel.annotation.ExcelProperty;
 import io.swagger.annotations.ApiModelProperty;
+import lombok.AllArgsConstructor;
 import lombok.Data;
+import lombok.NoArgsConstructor;
+import lombok.ToString;
 
 import java.math.BigDecimal;
 import java.util.Date;
 
 @Data
+@AllArgsConstructor
+@NoArgsConstructor
+@ToString
 public class StoreProductPO {
 
     /**