From 138b959cc11dc9a73b0c766030b99ba1180d8650 Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期一, 11 十一月 2024 14:32:48 +0800 Subject: [PATCH] 11 --- src/main/java/com/se/simu/helper/WebHelper.java | 87 ------------------------------------------- 1 files changed, 1 insertions(+), 86 deletions(-) diff --git a/src/main/java/com/se/simu/helper/WebHelper.java b/src/main/java/com/se/simu/helper/WebHelper.java index e386684..85e0781 100644 --- a/src/main/java/com/se/simu/helper/WebHelper.java +++ b/src/main/java/com/se/simu/helper/WebHelper.java @@ -17,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 = "."; @@ -37,35 +32,20 @@ return osName.startsWith("Windows"); } - /** - * 鑾峰彇CPU鏍稿績鏁� - */ public static int getCpuCores() { return Runtime.getRuntime().availableProcessors(); } - /** - * 鏍煎紡鍖栨棩鏈� - */ public final static SimpleDateFormat YMDHMS = new SimpleDateFormat("yyyyMMddHHmmss"); - /** - * 瀛楃涓�,鏄惁涓虹┖null鍜岀┖鏍� - */ public static boolean isEmpty(String str) { return null == str || "".equals(str.trim()); } - /** - * 鑾峰彇GUID - */ public static String getGuid() { return UUID.randomUUID().toString(); } - /** - * 鑾峰彇涓绘満IP - */ public static String getHostIp() { try { return InetAddress.getLocalHost().getHostAddress(); @@ -75,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)) { @@ -120,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()); @@ -167,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"); @@ -198,9 +154,6 @@ return false; } - /** - * 杈撳嚭瀛楄妭鏁扮粍 - */ public static void writeBytes(byte[] bytes, HttpServletResponse res) throws IOException { res.setContentType("application/octet-stream"); @@ -214,9 +167,6 @@ os.close(); } - /** - * 杈撳嚭Png鏂囦欢 - */ public static void writePng(String filePath, HttpServletResponse res) throws IOException { File file = new File(filePath); if (!file.exists() || file.isDirectory()) { @@ -232,28 +182,14 @@ 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()); @@ -261,12 +197,9 @@ 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); @@ -274,31 +207,21 @@ writeFile(file, res); } - /** - * 鍐欐枃浠� - */ private static void writeFile(String file, HttpServletResponse res) throws IOException { - // 閫氳繃response瀵硅薄锛岃幏鍙栧埌杈撳嚭娴� 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 ""; @@ -312,12 +235,8 @@ return fileName.substring(idx).toLowerCase(); } - /** - * 鑾峰彇澶氱敤閫斾簰鑱旂綉閭欢鎵╁睍绫诲瀷 - */ public static String getMime(String ext) { switch (ext) { - // 鍥剧墖 case ".tif": case ".tiff": return "image/tiff"; @@ -330,7 +249,6 @@ return "image/jpeg"; case ".png": return "image/png"; - // 闊�/瑙嗛 case ".mp3": return "audio/mp3"; case ".mp4": @@ -352,7 +270,6 @@ return "application/vnd.rn-realmedia"; case ".rmvb": return "application/vnd.rn-realmedia-vbr"; - // 缃戦〉 case ".js": return "application/x-javascript"; case ".css": @@ -369,7 +286,6 @@ case ".xml": case ".svg": return "text/xml"; - // 鏂囦欢 case ".txt": return "text/plain"; case ".dbf": @@ -393,7 +309,6 @@ return "application/x-dwg"; case ".ext": return "application/x-msdownload"; - // 榛樿 default: return "application/octet-stream"; } -- Gitblit v1.9.3