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 | 126 +++++++++++++++-------------------------- 1 files changed, 46 insertions(+), 80 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..85e0781 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; @@ -18,13 +17,8 @@ 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 = "."; @@ -32,30 +26,26 @@ private final static String UNKNOWN = "unknown"; - /** - * 鏍煎紡鍖栧綋鍓嶇郴缁熸棩鏈� 4 - */ - public static final SimpleDateFormat YMDHMS = new SimpleDateFormat("yyyyMMddHHmmss"); + public static boolean isWin() { + String osName = System.getProperty("os.name"); - /** - * 瀛楃涓�,鏄惁涓虹┖null鍜岀┖鏍� - */ + return osName.startsWith("Windows"); + } + + public static int getCpuCores() { + return Runtime.getRuntime().availableProcessors(); + } + + public final static SimpleDateFormat YMDHMS = new SimpleDateFormat("yyyyMMddHHmmss"); + public static boolean isEmpty(String str) { return null == str || "".equals(str.trim()); } - /** - * 鑾峰彇GUID - */ public static String getGuid() { return UUID.randomUUID().toString(); } - /** - * 鑾峰彇涓绘満IP - * - * @return - */ public static String getHostIp() { try { return InetAddress.getLocalHost().getHostAddress(); @@ -65,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)) { @@ -110,43 +97,28 @@ return ip; } - /** - * 鑾峰彇褰撳墠鏃堕棿鐨凾imestamp - */ public static Timestamp getCurrentTimestamp() { return new Timestamp(System.currentTimeMillis()); } - /** - * 鑾峰彇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(); } - /** - * 杈撳嚭JSON鑷抽〉闈� - */ public static boolean writeJson2Page(HttpServletResponse res, HttpStatus status, Object obj) { res.setStatus(status.value()); return writeStr2Page(res, JSON.toJSONString(obj)); } - /** - * 杈撳嚭JSON鑷抽〉闈� - */ public static boolean writeJson2Page(HttpServletResponse res, HttpStatus status, String str) { res.setStatus(status.value()); @@ -157,18 +129,12 @@ 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); } - /** - * 杈撳嚭str鑷抽〉闈� - */ public static boolean writeStr2Page(HttpServletResponse res, String str) { try { res.setContentType("application/json;charset=UTF-8"); @@ -188,28 +154,42 @@ 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 (isEmpty(fileName)) { fileName = YMDHMS.format(new Date()); @@ -217,37 +197,31 @@ 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 = 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(); } - /** - * 鑾峰彇鏂囦欢鎵╁睍鍚� - */ public static String getExtension(String fileName) { if (isEmpty(fileName)) { return ""; @@ -261,12 +235,8 @@ return fileName.substring(idx).toLowerCase(); } - /** - * 鑾峰彇澶氱敤閫斾簰鑱旂綉閭欢鎵╁睍绫诲瀷 - */ public static String getMime(String ext) { switch (ext) { - // 鍥剧墖 case ".tif": case ".tiff": return "image/tiff"; @@ -279,7 +249,6 @@ return "image/jpeg"; case ".png": return "image/png"; - // 闊�/瑙嗛 case ".mp3": return "audio/mp3"; case ".mp4": @@ -301,7 +270,6 @@ return "application/vnd.rn-realmedia"; case ".rmvb": return "application/vnd.rn-realmedia-vbr"; - // 缃戦〉 case ".js": return "application/x-javascript"; case ".css": @@ -318,7 +286,6 @@ case ".xml": case ".svg": return "text/xml"; - // 鏂囦欢 case ".txt": return "text/plain"; case ".dbf": @@ -342,7 +309,6 @@ return "application/x-dwg"; case ".ext": return "application/x-msdownload"; - // 榛樿 default: return "application/octet-stream"; } -- Gitblit v1.9.3