RequestUtils工具类

版权声明: https://blog.csdn.net/qq_20544709/article/details/80897395
package com.cl.frame.spring.utils;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.cl.frame.utils.lang.StringUtils;
/**
 * 获取Request对象相关信息的工具类
 * @author lovem
 *
 */
public class RequestUtil {
    private final static String ServerPort = "80";
    /**
     * 获取request对象
     * @return request
     */
    public static HttpServletRequest getRequest() {
        try {
            return((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                    .getRequest();
        } catch (Exception e) {
            return null;
        }
    }
    /**
     * 项目的真实路径
     * @return String
     */
    public static String getPjoPath() {
        try {
            return // 项目的真实路径
                    StringUtils.replace(getRequest().getServletContext().getContextPath(), "/", "\\");
        } catch (Exception e) {
            return null;
        }
    }
    /**
     * 获取客户端请求的路径名,如:/object/delObject
     * @return String
     */
    public static String getServletPath() {
        try {
            return // 项目的真实路径
                    getRequest().getServletPath();
        } catch (Exception e) {
            return null;
        }
    }
    /**
     * 获取服务器地址,如:localhost
     * @return String
     */
    public static String getServerName() {
        try {
            return // 项目的真实路径
                    getRequest().getServerName();
        } catch (Exception e) {
            return null;
        }
    }
    /**
     * 获取服务器端口,如8080
     * @return String
     */
    public static String getServerPort() {
        try {
            return // 项目的真实路径
                    getRequest().getServerPort()+"";
        } catch (Exception e) {
            return ServerPort;
        }
    }
    /**
     * 获用户地址,如:127.0.0.1 
     * @return String
     */
    public static String getRemoteAddr() {
        try {
            String remoteAddr = getRequest().getHeader("X-Real-IP");
                if (StringUtils.isNotBlank(remoteAddr)) {
                    remoteAddr = getRequest().getHeader("X-Forwarded-For");
                } else if (StringUtils.isNotBlank(remoteAddr)) {
                    remoteAddr = getRequest().getHeader("Proxy-Client-IP");
                } else if (StringUtils.isNotBlank(remoteAddr)) {
                    remoteAddr = getRequest().getHeader("WL-Proxy-Client-IP");
                }
                return remoteAddr != null ? remoteAddr : getRequest().getRemoteAddr();
        } catch (Exception e) {
            return "";
        }
    }
    /**
     * 获取项目的访问路径,如: localhost:8080/clfbs
     * @return String
     */
    public static String getObjUrl() {
        return getServerName()+":"+getServerPort()+getServletPath();
    }
}

注意:web.xml配置

    <!-- 单点接口使用,全局request对象获取 -->
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>

猜你喜欢

转载自blog.csdn.net/qq_20544709/article/details/80897395