• Cookie相关工具方法


        /**
         * InputStream转化为byte[]数组
         * @param input
         * @return
         * @throws IOException
         */
        public static byte[] toByteArray(InputStream input) throws IOException {
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            byte[] buffer = new byte[4096];
            int n = 0;
            while (-1 != (n = input.read(buffer))) {
                output.write(buffer, 0, n);
            }
            return output.toByteArray();
        }
    
        /**
         * 设置Cookie
         * @param response
         * @param key
         * @param value
         */
        public static void setCookie(HttpServletResponse response, String key, String value){
            Cookie cookie = new Cookie(key, value);
            cookie.setPath("/");
            response.addCookie(cookie);
        }
    
        /**
         * 获取Cookie
         * @param cookies
         * @param key
         * @return
         */
        public static String getCookieByKey(Cookie[] cookies, String key) {
            return getCookieByKey(cookies, key, null);
        }
    
        /**
         * 获取Cookie
         * @param cookies
         * @param key
         * @param defaultValue
         * @return
         */
        public static String getCookieByKey(Cookie[] cookies, String key , String defaultValue) {
            if (cookies == null || key == null) {return defaultValue;}
            for (Cookie cookie : cookies) {
                if (key.equals(cookie.getName())) {
                    return cookie.getValue();
                }
            }
            return defaultValue;
        }
    
        /**
         * 从Cookie中获取token
         * @param request
         * @param tokenName
         * @return
         */
        public static String getTokenAdaptive(HttpServletRequest request, String tokenName) {
            String token;
            Cookie[] cookies = request.getCookies();
            token = CommonUtil.getCookieByKey(cookies, tokenName);
            if (Strings.isNullOrEmpty(token)){
                token = request.getHeader(tokenName);
            }
            return token;
        }
  • 相关阅读:
    安装MeeGo
    一、Android uevent架构
    android键盘映射(转)
    【转】密码学 :CSP的概念
    【原创翻译】链接DLL至可执行文件翻译自MSDN
    【转】c语言中的定义和声明
    Windows:32位程序运行在64位系统上注册表会重定向
    Wow64
    VS2008 ActiveX(ocx控件)的调试工具ActiveX Control Test Container安装说明
    小写bool和大写BOOL的区别
  • 原文地址:https://www.cnblogs.com/it-deepinmind/p/11805170.html
Copyright © 2020-2023  润新知