首页 > 其他分享 >webUtils

webUtils

时间:2024-09-01 19:27:25浏览次数:12  
标签:webUtils request equalsIgnoreCase length ip null response

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class WebUtils {
/**
* 将字符串渲染到客户端
*
* @param response 渲染对象
* @param string 待渲染的字符串
*/
public static void renderString(HttpServletResponse response, String string) {
try {
response.setStatus(200);
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
response.getWriter().print(string);
} catch (IOException e) {
e.printStackTrace();
}
}

/**
 * 获取IP地址
 *
 * @param request
 * @return
 */
public static String getIpAddr(HttpServletRequest request) {
    String ip = request.getHeader("x-forwarded-for");
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getHeader("X-Real-IP");
    }
    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.getRemoteAddr();
    }
    if ("0:0:0:0:0:0:0:1".equals(ip)) {
        ip = "127.0.0.1";
    }
    if (ip.split(",").length > 1) {
        ip = ip.split(",")[0];
    }
    return ip;
}

}

标签:webUtils,request,equalsIgnoreCase,length,ip,null,response
From: https://www.cnblogs.com/luo9tian/p/18391608

相关文章

  • WebUtils工具类解析
    WebUtils根据名称,知道是Spring当中一个工具类,主要用于Web应用程序,供各种框架使用。其中有些方法还是挺有用的,比如可以获取Session中的会话属性,获取Cookies,设置Session中的会......