From a088987e7ab7005db1bb1da61dfc0cf420e02d78 Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期三, 13 十一月 2024 17:11:28 +0800 Subject: [PATCH] 1 --- src/main/java/com/moon/server/helper/WebHelper.java | 111 ------------------------------------------------------- 1 files changed, 1 insertions(+), 110 deletions(-) diff --git a/src/main/java/com/moon/server/helper/WebHelper.java b/src/main/java/com/moon/server/helper/WebHelper.java index 6f0fb3f..95ac2d1 100644 --- a/src/main/java/com/moon/server/helper/WebHelper.java +++ b/src/main/java/com/moon/server/helper/WebHelper.java @@ -22,10 +22,7 @@ import java.sql.Timestamp; import java.util.*; -/** - * Web甯姪绫� - * @author WWW - */ +@SuppressWarnings("ALL") public class WebHelper { private final static String UNKNOWN = "unknown"; @@ -33,25 +30,16 @@ private final static Log log = LogFactory.getLog(WebHelper.class); - /** - * 淇濈暀灏忔暟浣� - */ public static double round(double val, double size) { double power = Math.pow(10.0, size); return Math.round(val * power) / power; } - /** - * 鑾峰彇GUID - */ public static String getGuid() { return UUID.randomUUID().toString(); } - /** - * 鑾峰彇鐢ㄦ埛ip - */ public static String getIpAddress(HttpServletRequest request) { String ip = request.getHeader("X-Forwarded-For"); if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) { @@ -94,16 +82,10 @@ 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); @@ -111,9 +93,6 @@ return new Timestamp(now.getTimeInMillis()); } - /** - * 浠嶤ookie涓幏鍙杢oken - */ public static String getTokenFromCookie(HttpServletRequest request) { Cookie[] cookies = request.getCookies(); if (cookies == null || cookies.length == 0) { @@ -132,23 +111,14 @@ return null; } - /** - * 鍚慍ookie涓坊鍔爐oken - */ public static void saveToken2Cookie(String token, HttpServletRequest request, HttpServletResponse response) { - // 鍏堝垹闄� deleteCookies(request, response); - // 鍐嶄繚瀛� saveCookie(StaticData.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(SettingData.COOKIE_MAX_AGE); cookie.setHttpOnly(false); cookie.setPath("/"); @@ -158,9 +128,6 @@ response.addCookie(cookie); } - /** - * 鍒犻櫎cookie涓殑鍊� - */ public static void deleteCookie(String cookieKey, HttpServletRequest request, HttpServletResponse response) { Cookie[] cookies = request.getCookies(); if (cookies != null && cookies.length > 0) { @@ -174,9 +141,6 @@ } } - /** - * 鍒犻櫎鎵�鏈塁ookie - */ public static void deleteCookies(HttpServletRequest request, HttpServletResponse response) { Cookie[] cookies = request.getCookies(); if (cookies != null && cookies.length > 0) { @@ -188,9 +152,6 @@ } } - /** - * 鏍规嵁閿幏鍙朇ookie鍊� - */ public static String getCookieByKey(String key, HttpServletRequest request) { Cookie[] cookies = request.getCookies(); if (cookies == null || cookies.length == 0) { @@ -206,19 +167,13 @@ return null; } - /** - * 鑾峰彇Token - */ public static String getToken(HttpServletRequest request) { - // 1.浠巙rl鍙傛暟涓紝鑾峰彇token String token = request.getParameter(StaticData.TOKEN_KEY); - // 2.涓虹┖锛屽垯浠巋eader閲岃幏鍙� if (token == null) { token = request.getHeader(StaticData.TOKEN_KEY); } - // 3.杩樹负绌猴紝鍒欎粠cookie閲岃幏鍙� if (token == null) { token = getTokenFromCookie(request); } @@ -226,34 +181,22 @@ 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 String getRealPath(String path) { HttpServletRequest req = getRequest(); ServletContext ctx = req.getSession().getServletContext(); @@ -261,9 +204,6 @@ return ctx.getRealPath("/" + path); } - /** - * 杈撳嚭str鑷冲墠绔� - */ public static boolean writeStr2Page(HttpServletResponse res, String str) { try { res.setContentType("application/json;charset=UTF-8"); @@ -283,50 +223,27 @@ return false; } - /** - * 杈撳嚭json鑷冲墠绔� - */ public static void writeJson2Page(HttpServletResponse res, String str) { String json = JSON.toJSONString(new ResponseMsg<>(HttpStatus.ERROR, str)); writeStr2Page(res, json); } - /** - * 鑾峰彇閿欒JSON - */ public static String getErrJson(HttpStatus status, String msg) { return JSON.toJSONString(new ResponseMsg<String>(status, msg)); } - /** - * 鍐欏搷搴斾俊鎭� - */ public static void writeInfo(HttpStatus status, String info, HttpServletResponse res) { WebHelper.writeStr2Page(res, WebHelper.getErrJson(status, info)); } - /** - * 鑾峰彇闅忔満鏁存暟 - */ 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()); @@ -334,39 +251,27 @@ 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); res.setContentType(mime); - // 閫氳繃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(); } - /** - * 鎵ц鍛戒护 - * - * @param cmd 鍛戒护 - */ public static void exec(String cmd) { try { Process process = Runtime.getRuntime().exec(cmd); @@ -376,13 +281,6 @@ } } - /** - * 鑾峰彇璇锋眰鐨勫弬鏁板�� - * - * @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()) { @@ -394,13 +292,6 @@ 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()) { -- Gitblit v1.9.3