Bladeren bron

优化文件下载工具方法

woody 11 maanden geleden
bovenliggende
commit
ad96bfa09b
1 gewijzigde bestanden met toevoegingen van 52 en 8 verwijderingen
  1. 52 8
      framework-base/src/main/java/com/chelvc/framework/base/util/HttpUtils.java

+ 52 - 8
framework-base/src/main/java/com/chelvc/framework/base/util/HttpUtils.java

@@ -30,6 +30,7 @@ import com.chelvc.framework.common.util.ObjectUtils;
 import com.chelvc.framework.common.util.StringUtils;
 import com.google.common.collect.Maps;
 import lombok.NonNull;
+import org.apache.poi.ss.usermodel.Workbook;
 import org.springframework.http.MediaType;
 
 /**
@@ -204,7 +205,39 @@ public final class HttpUtils {
     }
 
     /**
-     * 响应文件
+     * 初始化文件下载相应头
+     *
+     * @param response Http响应对象
+     * @param filename 文件名称
+     */
+    public static void initializeDownloadHeader(@NonNull HttpServletResponse response, @NonNull String filename) {
+        try {
+            filename = URLEncoder.encode(filename, "utf-8");
+        } catch (UnsupportedEncodingException e) {
+            throw new RuntimeException(e);
+        }
+        response.reset();
+        response.setHeader("Content-Disposition", "attachment;filename=" + filename);
+        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
+        response.addHeader("Access-Control-Allow-Origin", "*");
+        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
+    }
+
+    /**
+     * 初始化文件下载相应头
+     *
+     * @param response Http响应对象
+     * @param filename 文件名称
+     * @param length   文件大小
+     */
+    public static void initializeDownloadHeader(@NonNull HttpServletResponse response, @NonNull String filename,
+                                                long length) {
+        initializeDownloadHeader(response, filename);
+        response.addHeader("Content-Length", String.valueOf(length));
+    }
+
+    /**
+     * 将文件写入输出流
      *
      * @param response Http响应对象
      * @param file     文件对象
@@ -215,7 +248,7 @@ public final class HttpUtils {
     }
 
     /**
-     * 响应文件
+     * 将文件写入输出流
      *
      * @param response Http响应对象
      * @param file     文件对象
@@ -225,12 +258,7 @@ public final class HttpUtils {
     public static void write(@NonNull HttpServletResponse response, @NonNull File file, @NonNull String filename)
             throws IOException {
         // 设置响应头
-        response.reset();
-        response.addHeader("Content-Length", String.valueOf(file.length()));
-        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "utf-8"));
-        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
-        response.addHeader("Access-Control-Allow-Origin", "*");
-        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
+        initializeDownloadHeader(response, filename, file.length());
 
         // 将文件写入响应流
         int length;
@@ -243,6 +271,22 @@ public final class HttpUtils {
         }
     }
 
+    /**
+     * 将Excel工作薄写入输出流
+     *
+     * @param response Http响应对象
+     * @param workbook Excel工作薄实例
+     * @param filename 文件名称
+     * @throws IOException I/O异常
+     */
+    public static void write(@NonNull HttpServletResponse response, @NonNull Workbook workbook,
+                             @NonNull String filename) throws IOException {
+        initializeDownloadHeader(response, filename);
+        try (OutputStream output = response.getOutputStream()) {
+            workbook.write(output);
+        }
+    }
+
     /**
      * 对象URL编码
      *