¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.lf.server.helper; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.lf.server.entity.all.HttpStatus; |
| | | import com.lf.server.entity.all.ResponseMsg; |
| | | import com.lf.server.entity.all.SettingData; |
| | | import com.lf.server.entity.all.StaticData; |
| | | 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.FileInputStream; |
| | | import java.io.IOException; |
| | | import java.io.PrintWriter; |
| | | import java.net.URLEncoder; |
| | | import java.sql.Timestamp; |
| | | import java.util.Calendar; |
| | | import java.util.Random; |
| | | import java.util.UUID; |
| | | |
| | | /** |
| | | * Web帮å©ç±» |
| | | * @author WWW |
| | | */ |
| | | public class WebHelper { |
| | | private final static String UNKNOWN = "unknown"; |
| | | |
| | | private final static String COMMA = ","; |
| | | |
| | | /** |
| | | * è·å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)) { |
| | | ip = request.getHeader("Proxy-Client-IP"); |
| | | } |
| | | if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) { |
| | | ip = request.getHeader("WL-Proxy-Client-IP"); |
| | | } |
| | | if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) { |
| | | ip = request.getHeader("HTTP_X_FORWARDED_FOR"); |
| | | } |
| | | if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) { |
| | | ip = request.getHeader("HTTP_X_FORWARDED"); |
| | | } |
| | | if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) { |
| | | ip = request.getHeader("HTTP_X_CLUSTER_CLIENT_IP"); |
| | | } |
| | | if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) { |
| | | ip = request.getHeader("HTTP_CLIENT_IP"); |
| | | } |
| | | if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) { |
| | | ip = request.getHeader("HTTP_FORWARDED_FOR"); |
| | | } |
| | | if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) { |
| | | ip = request.getHeader("HTTP_FORWARDED"); |
| | | } |
| | | if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) { |
| | | ip = request.getHeader("HTTP_VIA"); |
| | | } |
| | | if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) { |
| | | ip = request.getHeader("REMOTE_ADDR"); |
| | | } |
| | | if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) { |
| | | ip = request.getRemoteAddr(); |
| | | } |
| | | if (ip.contains(COMMA)) { |
| | | return ip.split(",")[0]; |
| | | } |
| | | |
| | | return ip; |
| | | } |
| | | |
| | | /** |
| | | * è·åå½åæ¶é´çTimestamp |
| | | */ |
| | | public static Timestamp getCurrentTimestamp() { |
| | | return new Timestamp(System.currentTimeMillis()); |
| | | } |
| | | |
| | | /** |
| | | * è·åå½åæ¶é´æå®åéæ°åçTimestamp |
| | | */ |
| | | public static Timestamp getTimestamp(int min) { |
| | | Calendar now = Calendar.getInstance(); |
| | | now.add(Calendar.MINUTE, min); |
| | | |
| | | return new Timestamp(now.getTimeInMillis()); |
| | | } |
| | | |
| | | /** |
| | | * ä»Cookieä¸è·åtoken |
| | | */ |
| | | 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 StaticData.TOKEN_COOKIE_KEY: |
| | | return cookie.getValue(); |
| | | default: |
| | | break; |
| | | } |
| | | } |
| | | |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * åCookie䏿·»å token |
| | | */ |
| | | 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("/"); |
| | | //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); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å 餿æCookie |
| | | */ |
| | | 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); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * æ ¹æ®é®è·åCookieå¼ |
| | | */ |
| | | 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.ä»urlåæ°ä¸ï¼è·åtoken |
| | | String token = request.getParameter(StaticData.TOKEN_KEY); |
| | | |
| | | // 2.为空ï¼åä»headeréè·å |
| | | if (token == null) { |
| | | token = request.getHeader(StaticData.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 String getRealPath(String path) { |
| | | HttpServletRequest req = getRequest(); |
| | | ServletContext ctx = req.getSession().getServletContext(); |
| | | |
| | | return ctx.getRealPath("/" + path); |
| | | } |
| | | |
| | | /** |
| | | * è¾åºjsonæ°æ®å°å端 |
| | | */ |
| | | public static boolean write2Page(HttpServletResponse response, String jsonPack) throws IOException { |
| | | response.setContentType("application/json;charset=UTF-8"); |
| | | response.setHeader("Cache-Control", "no-cache"); |
| | | response.setHeader("Pragma", "No-cache"); |
| | | response.setDateHeader("Expires", 0); |
| | | |
| | | PrintWriter out = response.getWriter(); |
| | | out.print(jsonPack); |
| | | |
| | | out.flush(); |
| | | out.close(); |
| | | |
| | | return false; |
| | | } |
| | | |
| | | /** |
| | | * è·åé误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) { |
| | | try { |
| | | WebHelper.write2Page(res, WebHelper.getErrJson(status, info)); |
| | | } catch (Exception e) { |
| | | // |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * è·åéæºæ´æ° |
| | | */ |
| | | public static int getRandomInt(int min, int max) { |
| | | return new Random().nextInt(max) % (max - min + 1) + min; |
| | | } |
| | | |
| | | /** |
| | | * ä¸è½½æä»¶ |
| | | * |
| | | * @param file æä»¶ |
| | | * @param fileName æä»¶å |
| | | * @param res ååº |
| | | * @throws Exception å¼å¸¸ |
| | | */ |
| | | public static void download(String file, String fileName, HttpServletResponse res) throws Exception { |
| | | if (StringHelper.isNull(fileName)) { |
| | | fileName = URLEncoder.encode(FileHelper.getFileName(file), "UTF-8"); |
| | | } |
| | | |
| | | // 设置ååºå¤´ä¸æä»¶çä¸è½½æ¹å¼ä¸ºéä»¶æ¹å¼ï¼ä»¥å设置æä»¶å |
| | | res.setHeader("Content-Disposition", "attachment; filename=" + 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(); |
| | | } |
| | | |
| | | // å
³éèµæº |
| | | outputStream.close(); |
| | | fileInputStream.close(); |
| | | } |
| | | } |