From e5d40172f9bd9b0b967cb0e74b551f1a135dae6f Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期日, 22 九月 2024 16:06:53 +0800
Subject: [PATCH] 1

---
 src/main/java/com/se/simu/helper/WebHelper.java |   58 +++++++++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 51 insertions(+), 7 deletions(-)

diff --git a/src/main/java/com/se/simu/helper/WebHelper.java b/src/main/java/com/se/simu/helper/WebHelper.java
index 08db9f8..dd1d36a 100644
--- a/src/main/java/com/se/simu/helper/WebHelper.java
+++ b/src/main/java/com/se/simu/helper/WebHelper.java
@@ -1,6 +1,6 @@
 package com.se.simu.helper;
 
-import com.alibaba.fastjson2.JSON;
+import com.alibaba.fastjson.JSON;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.http.HttpStatus;
 import org.springframework.web.context.request.RequestContextHolder;
@@ -9,8 +9,7 @@
 import javax.servlet.ServletOutputStream;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
-import java.io.FileInputStream;
-import java.io.PrintWriter;
+import java.io.*;
 import java.net.InetAddress;
 import java.net.URLEncoder;
 import java.net.UnknownHostException;
@@ -32,10 +31,16 @@
 
     private final static String UNKNOWN = "unknown";
 
+    public static boolean isWin() {
+        String osName = System.getProperty("os.name");
+
+        return osName.startsWith("Windows");
+    }
+
     /**
-     * 鏍煎紡鍖栧綋鍓嶇郴缁熸棩鏈� 4
+     * 鏍煎紡鍖栨棩鏈�
      */
-    public static final SimpleDateFormat YMDHMS = new SimpleDateFormat("yyyyMMddHHmmss");
+    public final static SimpleDateFormat YMDHMS = new SimpleDateFormat("yyyyMMddHHmmss");
 
     /**
      * 瀛楃涓�,鏄惁涓虹┖null鍜岀┖鏍�
@@ -53,8 +58,6 @@
 
     /**
      * 鑾峰彇涓绘満IP
-     *
-     * @return
      */
     public static String getHostIp() {
         try {
@@ -189,6 +192,40 @@
     }
 
     /**
+     * 杈撳嚭瀛楄妭鏁扮粍
+     */
+    public static void writeBytes(byte[] bytes, HttpServletResponse res) throws IOException {
+        res.setContentType("application/octet-stream");
+
+        if (null == bytes) {
+            res.setStatus(HttpStatus.NOT_FOUND.value());
+            return;
+        }
+
+        OutputStream os = res.getOutputStream();
+        os.write(bytes, 0, bytes.length);
+        os.close();
+    }
+
+    /**
+     * 杈撳嚭Png鏂囦欢
+     */
+    public static void writePng(String filePath, HttpServletResponse res) throws IOException {
+        File file = new File(filePath);
+        if (!file.exists() || file.isDirectory()) {
+            res.setStatus(HttpStatus.NOT_FOUND.value());
+            return;
+        }
+
+        String fileName = URLEncoder.encode(filePath, "UTF-8").replace("+", "%20");
+        res.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + fileName);
+        res.setCharacterEncoding("UTF-8");
+        res.setContentType("image/png");
+
+        writeFile(filePath, res);
+    }
+
+    /**
      * 鑾峰彇闅忔満鏁存暟
      */
     public static int getRandomInt(int min, int max) {
@@ -227,6 +264,13 @@
         String mime = getMime(ext);
         res.setContentType(mime);
 
+        writeFile(file, res);
+    }
+
+    /**
+     * 鍐欐枃浠�
+     */
+    private static void writeFile(String file, HttpServletResponse res) throws IOException {
         // 閫氳繃response瀵硅薄锛岃幏鍙栧埌杈撳嚭娴�
         ServletOutputStream outputStream = res.getOutputStream();
         // 瀹氫箟杈撳叆娴侊紝閫氳繃杈撳叆娴佽鍙栨枃浠跺唴瀹�

--
Gitblit v1.9.3