• http请求requestUtils


    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    import javax.servlet.http.HttpServletRequest;
    
    import org.apache.commons.lang.StringUtils;
    
    /**
     * <p> 
     *  <h3>Web Request 工具类</h3>
     *  <span> 该类使用 abstaract 修饰,防止用户创建对象。其方法一般为静态,如添加非静态方法,修改者需提供具体子类。<span>
     *     <pre></pre>
     * </p>*/
    public abstract class RequestUtils {
        
        
        /**
         * <p> 解析客户端的 ip 地址  </p>
         */
        public static String getIpAddr(HttpServletRequest request) {
            String ip = request.getHeader("x-forwarded-for");
            if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
            }
    
            if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }
    
            if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
            }
    
            return ip;
        }
        
        /**
         * <p>获取 request body 中的内容</p>
         * <span>该方法会抛出 IOException ,需调用者处理或者抛到更上层</span>*/
        public static String getRequestBody(HttpServletRequest request) throws IOException {
            
            StringBuilder sb = new StringBuilder() ;
            BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
            String line;
            
            //避免程序异常退出不能关闭
            try {
                while((line = br.readLine()) != null) {
                    sb.append(line);
                }
            }finally { 
                br.close();
            }
            
            return sb.toString();
        }
    
    }
  • 相关阅读:
    查看网络接口
    httpd sshd firewalld 很多服务后面的d是什么意思
    CentOS7入门
    1005:取余,循环,找规律
    1006 Tick and Tick
    cv.Mat 与 .txt 文件数据的读写操作
    禁止别人用QQ号搜索到你,同时告诉你如何破解
    LaTex初学
    github上对一些名词的理解(之如fork)
    CCF Z字形扫描
  • 原文地址:https://www.cnblogs.com/zyf-yxm/p/10572344.html
Copyright © 2020-2023  润新知