From 3417cf014a65765e02696c1d121ce58b2b4a8aed Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期二, 08 四月 2025 15:55:36 +0800
Subject: [PATCH] 修改pom.xml

---
 src/main/java/com/se/simu/helper/WebHelper.java |  426 +++++++++++++++++++---------------------------------
 1 files changed, 159 insertions(+), 267 deletions(-)

diff --git a/src/main/java/com/se/simu/helper/WebHelper.java b/src/main/java/com/se/simu/helper/WebHelper.java
index 074817e..85e0781 100644
--- a/src/main/java/com/se/simu/helper/WebHelper.java
+++ b/src/main/java/com/se/simu/helper/WebHelper.java
@@ -1,54 +1,51 @@
 package com.se.simu.helper;
 
+import com.alibaba.fastjson.JSON;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.http.HttpStatus;
 import org.springframework.web.context.request.RequestContextHolder;
 import org.springframework.web.context.request.ServletRequestAttributes;
 
-import javax.servlet.ServletContext;
 import javax.servlet.ServletOutputStream;
-import javax.servlet.http.Cookie;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
-import javax.servlet.http.HttpSession;
-import java.io.BufferedReader;
-import java.io.FileInputStream;
-import java.io.InputStreamReader;
-import java.io.PrintWriter;
+import java.io.*;
 import java.net.InetAddress;
 import java.net.URLEncoder;
 import java.net.UnknownHostException;
 import java.sql.Timestamp;
+import java.text.SimpleDateFormat;
 import java.util.*;
 
-/**
- * Web甯姪绫�
- *
- * @author WWW
- * @date 2024-07-16
- */
 @Slf4j
+@SuppressWarnings("ALL")
 public class WebHelper {
+    public final static String POINT = ".";
+
     private final static String COMMA = ",";
 
     private final static String UNKNOWN = "unknown";
 
-    public final static String TOKEN_KEY = "token";
+    public static boolean isWin() {
+        String osName = System.getProperty("os.name");
 
-    public final static String TOKEN_COOKIE_KEY = "token";
+        return osName.startsWith("Windows");
+    }
 
-    public final static int COOKIE_MAX_AGE = 4 * 60 * 60;
+    public static int getCpuCores() {
+        return Runtime.getRuntime().availableProcessors();
+    }
 
-    /**
-     * 鑾峰彇GUID
-     */
+    public final static SimpleDateFormat YMDHMS = new SimpleDateFormat("yyyyMMddHHmmss");
+
+    public static boolean isEmpty(String str) {
+        return null == str || "".equals(str.trim());
+    }
+
     public static String getGuid() {
         return UUID.randomUUID().toString();
     }
 
-    /**
-     * 鑾峰彇涓绘満IP
-     * @return
-     */
     public static String getHostIp() {
         try {
             return InetAddress.getLocalHost().getHostAddress();
@@ -58,9 +55,6 @@
         return "127.0.0.1";
     }
 
-    /**
-     * 鑾峰彇鐢ㄦ埛IP
-     */
     public static String getIpAddress(HttpServletRequest request) {
         String ip = request.getHeader("X-Forwarded-For");
         if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
@@ -103,176 +97,44 @@
         return ip;
     }
 
-    /**
-     * 鑾峰彇褰撳墠鏃堕棿鐨凾imestamp
-     */
     public static Timestamp getCurrentTimestamp() {
         return new Timestamp(System.currentTimeMillis());
     }
 
-    /**
-     * 鑾峰彇褰撳墠鏃堕棿鎸囧畾鍒嗛挓鏁板悗鐨凾imestamp
-     */
-    public static Timestamp getTimestamp(int min) {
-        Calendar now = Calendar.getInstance();
-        now.add(Calendar.MINUTE, min);
-
-        return new Timestamp(now.getTimeInMillis());
-    }
-
-    /**
-     * 浠嶤ookie涓幏鍙杢oken
-     */
-    public static String getTokenFromCookie(HttpServletRequest request) {
-        Cookie[] cookies = request.getCookies();
-        if (cookies == null || cookies.length == 0) {
-            return null;
-        }
-
-        for (Cookie cookie : cookies) {
-            switch (cookie.getName()) {
-                case TOKEN_COOKIE_KEY:
-                    return cookie.getValue();
-                default:
-                    break;
-            }
-        }
-
-        return null;
-    }
-
-    /**
-     * 鍚慍ookie涓坊鍔爐oken
-     */
-    public static void saveToken2Cookie(String token, HttpServletRequest request, HttpServletResponse response) {
-        // 鍏堝垹闄�
-        deleteCookies(request, response);
-
-        // 鍐嶄繚瀛�
-        saveCookie(TOKEN_COOKIE_KEY, token, response);
-    }
-
-    /**
-     * 淇濆瓨Cookie
-     */
-    public static void saveCookie(String key, String value, HttpServletResponse response) {
-        Cookie cookie = new Cookie(key, value);
-        // 璁剧疆cookie澶辨晥鏃堕棿锛屽崟浣嶄负绉�
-        cookie.setMaxAge(COOKIE_MAX_AGE);
-        cookie.setHttpOnly(false);
-        cookie.setPath("/");
-        //cookie.setDomain("*")
-
-        response.setHeader("P3P", "CP=CAO PSA OUR");
-        response.addCookie(cookie);
-    }
-
-    /**
-     * 鍒犻櫎cookie涓殑鍊�
-     */
-    public static void deleteCookie(String cookieKey, HttpServletRequest request, HttpServletResponse response) {
-        Cookie[] cookies = request.getCookies();
-        if (cookies != null && cookies.length > 0) {
-            for (Cookie c : cookies) {
-                if (cookieKey.equalsIgnoreCase(c.getName())) {
-                    c.setMaxAge(0);
-                    c.setPath("/");
-                    response.addCookie(c);
-                }
-            }
-        }
-    }
-
-    /**
-     * 鍒犻櫎鎵�鏈塁ookie
-     */
-    public static void deleteCookies(HttpServletRequest request, HttpServletResponse response) {
-        Cookie[] cookies = request.getCookies();
-        if (cookies != null && cookies.length > 0) {
-            for (Cookie c : cookies) {
-                c.setMaxAge(0);
-                c.setPath("/");
-                response.addCookie(c);
-            }
-        }
-    }
-
-    /**
-     * 鏍规嵁閿幏鍙朇ookie鍊�
-     */
-    public static String getCookieByKey(String key, HttpServletRequest request) {
-        Cookie[] cookies = request.getCookies();
-        if (cookies == null || cookies.length == 0) {
-            return null;
-        }
-
-        for (Cookie c : cookies) {
-            if (key.equals(c.getName())) {
-                return c.getValue();
-            }
-        }
-
-        return null;
-    }
-
-    /**
-     * 鑾峰彇Token
-     */
-    public static String getToken(HttpServletRequest request) {
-        // 1.浠巙rl鍙傛暟涓紝鑾峰彇token
-        String token = request.getParameter(TOKEN_KEY);
-
-        // 2.涓虹┖锛屽垯浠巋eader閲岃幏鍙�
-        if (token == null) {
-            token = request.getHeader(TOKEN_KEY);
-        }
-
-        // 3.杩樹负绌猴紝鍒欎粠cookie閲岃幏鍙�
-        if (token == null) {
-            token = getTokenFromCookie(request);
-        }
-
-        return token;
-    }
-
-    /**
-     * 鑾峰彇Request
-     */
     public static HttpServletRequest getRequest() {
         ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
 
         return servletRequestAttributes.getRequest();
     }
 
-    /**
-     * 鑾峰彇Response
-     */
     public static HttpServletResponse getResponse() {
         ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
 
         return servletRequestAttributes.getResponse();
     }
 
-    /**
-     * 鑾峰彇Session
-     */
-    public static HttpSession getSession() {
-        return getRequest().getSession();
+    public static boolean writeJson2Page(HttpServletResponse res, HttpStatus status, Object obj) {
+        res.setStatus(status.value());
+
+        return writeStr2Page(res, JSON.toJSONString(obj));
     }
 
-    /**
-     * 鑾峰彇鐪熷疄璺緞
-     */
-    public static String getRealPath(String path) {
-        HttpServletRequest req = getRequest();
-        ServletContext ctx = req.getSession().getServletContext();
+    public static boolean writeJson2Page(HttpServletResponse res, HttpStatus status, String str) {
+        res.setStatus(status.value());
 
-        return ctx.getRealPath("/" + path);
+        Map<String, Object> map = new HashMap(2);
+        map.put("code", status.value() >= 400 ? -1 : 0);
+        map.put("msg", str);
+
+        return writeStr2Page(res, JSON.toJSONString(map));
     }
 
-    /**
-     * 杈撳嚭str鑷冲墠绔�
-     */
+    public static boolean writeStr2Page(HttpServletResponse res, HttpStatus status, String str) {
+        res.setStatus(status.value());
+
+        return writeStr2Page(res, str);
+    }
+
     public static boolean writeStr2Page(HttpServletResponse res, String str) {
         try {
             res.setContentType("application/json;charset=UTF-8");
@@ -292,133 +154,163 @@
         return false;
     }
 
-    /**
-     * 鑾峰彇闅忔満鏁存暟
-     */
+    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();
+    }
+
+    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) {
         return new Random().nextInt(max) % (max - min + 1) + min;
     }
 
-    /**
-     * 涓嬭浇鏂囦欢
-     */
     public static void download(String file, String fileName, HttpServletResponse res) throws Exception {
         download(file, fileName, false, res);
     }
 
-    /**
-     * 涓嬭浇鏂囦欢
-     *
-     * @param file     鏂囦欢
-     * @param fileName 鏂囦欢鍚�
-     * @param res      鍝嶅簲
-     * @throws Exception 寮傚父
-     */
     public static void download(String file, String fileName, boolean inline, HttpServletResponse res) throws Exception {
-        if (StringHelper.isEmpty(fileName)) {
-            fileName = StringHelper.YMDHMS2_FORMAT.format(new Date());
+        if (isEmpty(fileName)) {
+            fileName = YMDHMS.format(new Date());
         }
         fileName = URLEncoder.encode(fileName, "UTF-8").replace("+", "%20");
         String dispose = inline ? "inline" : "attachment";
 
-        // 璁剧疆鍝嶅簲澶翠腑鏂囦欢鐨勪笅杞芥柟寮忎负闄勪欢鏂瑰紡锛屼互鍙婅缃枃浠跺悕
         res.setHeader("Content-Disposition", dispose + "; filename*=UTF-8''" + fileName);
-        // 璁剧疆鍝嶅簲澶寸殑缂栫爜鏍煎紡涓� UTF-8
         res.setCharacterEncoding("UTF-8");
 
-        // 閫氳繃response瀵硅薄璁剧疆鍝嶅簲鏁版嵁鏍煎紡(濡傦細"text/plain; charset=utf-8")
-        String ext = FileHelper.getExtension(file);
-        String mime = FileHelper.getMime(ext);
+        String ext = getExtension(file);
+        String mime = getMime(ext);
         res.setContentType(mime);
 
-        // 閫氳繃response瀵硅薄锛岃幏鍙栧埌杈撳嚭娴�
+        writeFile(file, res);
+    }
+
+    private static void writeFile(String file, HttpServletResponse res) throws IOException {
         ServletOutputStream outputStream = res.getOutputStream();
-        // 瀹氫箟杈撳叆娴侊紝閫氳繃杈撳叆娴佽鍙栨枃浠跺唴瀹�
         FileInputStream fileInputStream = new FileInputStream(file);
 
         int len = 0;
         byte[] bytes = new byte[1024];
         while ((len = fileInputStream.read(bytes)) != -1) {
-            // 閫氳繃杈撳叆娴佽鍙栨枃浠舵暟鎹紝鐒跺悗閫氳繃涓婅堪鐨勮緭鍑烘祦鍐欏洖娴忚鍣�
             outputStream.write(bytes, 0, len);
             outputStream.flush();
         }
 
-        // 鍏抽棴璧勬簮
         fileInputStream.close();
         outputStream.close();
     }
 
-    /**
-     * 鎵ц鍛戒护
-     *
-     * @param cmd 鍛戒护
-     */
-    public static void exec(String cmd) {
-        try {
-            Runtime.getRuntime().exec(cmd);
-        } catch (Exception ex) {
-            log.error(ex.getMessage(), ex);
+    public static String getExtension(String fileName) {
+        if (isEmpty(fileName)) {
+            return "";
         }
+
+        int idx = fileName.lastIndexOf(POINT);
+        if (idx == -1) {
+            return "";
+        }
+
+        return fileName.substring(idx).toLowerCase();
     }
 
-    /**
-     * 鎵ц鍛戒护
-     *
-     * @param cmd 鍛戒护
-     */
-    public static String exec2(String cmd) {
-        try {
-            StringBuilder sb = new StringBuilder();
-            Process process = Runtime.getRuntime().exec(cmd);
-            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
-
-            String line;
-            while ((line = reader.readLine()) != null) {
-                sb.append(line).append("\n");
-            }
-            reader.close();
-
-            return sb.toString();
-        } catch (Exception ex) {
-            log.error(ex.getMessage(), ex);
-            return null;
+    public static String getMime(String ext) {
+        switch (ext) {
+            case ".tif":
+            case ".tiff":
+                return "image/tiff";
+            case ".img":
+                return "application/x-img";
+            case ".gif":
+                return "image/gif";
+            case ".jpg":
+            case ".jpeg":
+                return "image/jpeg";
+            case ".png":
+                return "image/png";
+            case ".mp3":
+                return "audio/mp3";
+            case ".mp4":
+                return "video/mpeg4";
+            case ".avi":
+                return "video/avi";
+            case ".mpg":
+            case ".mpeg":
+                return "video/mpg";
+            case ".wav":
+                return "audio/wav";
+            case ".wma":
+                return "audio/x-ms-wma";
+            case ".swf":
+                return "application/x-shockwave-flash";
+            case ".wmv":
+                return "video/x-ms-wmv";
+            case ".rm":
+                return "application/vnd.rn-realmedia";
+            case ".rmvb":
+                return "application/vnd.rn-realmedia-vbr";
+            case ".js":
+                return "application/x-javascript";
+            case ".css":
+                return "text/css";
+            case ".asp":
+                return "text/asp";
+            case ".mht":
+                return "message/rfc822";
+            case ".jsp":
+            case ".htm":
+            case ".html":
+            case ".xhtml":
+                return "text/html";
+            case ".xml":
+            case ".svg":
+                return "text/xml";
+            case ".txt":
+                return "text/plain";
+            case ".dbf":
+                return "application/x-dbf";
+            case ".mdb":
+                return "application/msaccess";
+            case ".pdf":
+                return "application/pdf";
+            case ".ppt":
+            case ".pptx":
+                return "application/x-ppt";
+            case ".doc":
+            case ".docx":
+                return "application/msword";
+            case ".xls":
+            case ".xlsx":
+                return "application/vnd.ms-excel";
+            case ".dgn":
+                return "application/x-dgn";
+            case ".dwg":
+                return "application/x-dwg";
+            case ".ext":
+                return "application/x-msdownload";
+            default:
+                return "application/octet-stream";
         }
-    }
-
-    /**
-     * 鑾峰彇璇锋眰鐨勫弬鏁板��
-     *
-     * @param req 璇锋眰
-     * @param key 鍙傛暟鍚�
-     * @return 鍙傛暟鍊�
-     */
-    public static String getReqParamVal(HttpServletRequest req, String key) {
-        Map<String, String[]> maps = req.getParameterMap();
-        for (Map.Entry<String, String[]> entry : maps.entrySet()) {
-            if (entry.getKey().equalsIgnoreCase(key)) {
-                return null == entry.getValue() || 0 == entry.getValue().length ? null : entry.getValue()[0];
-            }
-        }
-
-        return null;
-    }
-
-    /**
-     * 鑾峰彇璇锋眰鐨勫弬鏁板��
-     *
-     * @param req 璇锋眰
-     * @param key 鍙傛暟鍚�
-     * @return 鍙傛暟鍊�
-     */
-    public static String[] getReqParamVals(HttpServletRequest req, String key) {
-        Map<String, String[]> maps = req.getParameterMap();
-        for (Map.Entry<String, String[]> entry : maps.entrySet()) {
-            if (entry.getKey().equalsIgnoreCase(key)) {
-                return entry.getValue();
-            }
-        }
-
-        return null;
     }
 }

--
Gitblit v1.9.3