JAVA获取IP地址,第1张

public static void main(String[] args) { try { // 获取计算机名 String name = InetAddressgetLocalHost()getHostName(); // 获取IP地址 String ip = InetAddressgetLocalHost()getHostAddress(); Systemoutprintln("计算机名:"+name); Systemoutprintln("IP地址:"+ip); } catch (UnknownHostException e) { Systemoutprintln("异常:" + e); eprintStackTrace(); } }

是否可以解决您的问题?

使用JAVA后台代码取得WEBROOT物理路径,可以有如下两种方式:

1、使用JSP Servlet取得WEB根路径可以用requestgetContextPath(),相对路径requestgetSession()getServletContext()getRealPath("/"),它们可以使用我们很容易取得根路径。

2、如果使用了spring, 在WEB-INF/webxml中,创建一个webAppRootKey的param,指定一个值(默认为webapproot)作为键值,然后通过Listener,或者Filter,或者Servlet执行String webAppRootKey = getServletContext()getRealPath("/"); 并将webAppRootKey对应的webapproot分别作为Key,Value写到System Properties系统属性中。之后在程序中通过SystemgetProperty("webapproot")来获得WebRoot的物理路径。

具体示例代码如下:

webxml

<xml version="10" encoding="UTF-8">

<web-app version="24"

xmlns="http://javasuncom/xml/ns/j2ee"

xmlns:xsi="http://wwww3org/2001/XMLSchema-instance"

xsi:schemaLocation="http://javasuncom/xml/ns/j2ee

http://javasuncom/xml/ns/j2ee/web-app_2_4xsd">

<context-param>

<param-name>webAppRootKey</param-name>

<param-value>csc2root</param-value>

</context-param>

<listener>

<listener-class>testApplicationListener</listener-class>

</listener>

</web-app>

ApplicationListenerjava

package test;

import javaxservletServletContextEvent;

import orgspringframeworkwebcontextContextLoaderListener;

public class ApplicationListener extends ContextLoaderListener {

public void contextDestroyed(ServletContextEvent sce) {

// TODO Auto-generated method stub

}

public void contextInitialized(ServletContextEvent sce) {

// TODO Auto-generated method stub

String webAppRootKey = scegetServletContext()getRealPath("/");

SystemsetProperty("csc2root" , webAppRootKey);

String path =SystemgetProperty("csc2root");

Systemoutprintln("sssss:::"+path);

}

}

testjava

public class test {

public void remve(){

String path =SystemgetProperty("csc2root");

Systemoutprintln("result::::::::"+path);

}

}

indexjsp

<%@ page language="java" import="javautil" pageEncoding="UTF-8"%>

<%@ page import="javautil" %>

<%@ page import="testtest" %>

<%

String path = requestgetContextPath();

String basePath = requestgetScheme()+"://"+requestgetServerName()+":"+requestgetServerPort()+path+"/";

%>

<%

test t = new test();

tremve();

%>

<html>

</html>

部署程序发布 启动TOMCAT 运行indexjsp 就可以调用JAVA中全局设置的物理路径了(说明这里的JSP 只是调用了TESTJAVA 的remove方法,不做其他使用。原理解释,TOMCAT启动和读取WEBXML 监听方式加载SPRING ApplicationListener继承SPRING ContextLoaderListener加载SPRING顺便吧全局路径赋值给csc2root 描述,这样之后JAVA 代码中就可以使用SystemgetProperty("csc2root")调用全路路径了。

嗨 你好

据网上了解到:

在JSP里,获取客户端的IP地址的方法是:requestgetRemoteAddr(),这种方法在大部分情况下都是有效的。但是在通过了Apache,Squid等反向代理软件就不能获取到客户端的真实IP地址了。

如果使用了反向代理软件,将http://1921681110:2046/ 的URL反向代理为 http://wwwjavapeixuncomcn / 的URL时,用requestgetRemoteAddr()方法获取的IP地址是:127001 或 1921681110,而并不是客户端的真实IP。

经过代理以后,由于在客户端和服务之间增加了中间层,因此服务器无法直接拿到客户端的IP,服务器端应用也无法直接通过转发请求的地址返回给客户端。但是在转发请求的HTTP头信息中,增加了X-FORWARDED-FOR信息。用以跟踪原有的客户端IP地址和原来客户端请求的服务器地址。当我们访问http://wwwjavapeixuncomcn /indexjsp/ 时,其实并不是我们浏览器真正访问到了服务器上的indexjsp文件,而是先由代理服务器去访问http://1921681110:2046/indexjsp ,代理服务器再将访问到的结果返回给我们的浏览器,因为是代理服务器去访问indexjsp的,所以indexjsp中通过requestgetRemoteAddr()的方法获取的IP实际上是代理服务器的地址,并不是客户端的IP地址。

于是可得出获得客户端真实IP地址的方法一:

public String getRemortIP(HttpServletRequest request) { if (requestgetHeader("x-forwarded-for") == null) { return requestgetRemoteAddr(); } return requestgetHeader("x-forwarded-for"); }

可是当我访问http://www5a520cn /indexjsp/ 时,返回的IP地址始终是unknown,也并不是如上所示的127001 或 1921681110了,而我访问http://1921681110:2046/indexjsp 时,则能返回客户端的真实IP地址,写了个方法去验证。原因出在了Squid上。squidconf 的配制文件 forwarded_for 项默认是为on,如果 forwarded_for 设成了 off则:X-Forwarded-For: unknown

于是可得出获得客户端真实IP地址的方法二:

public String getIpAddr(HttpServletRequest request) { String ip = requestgetHeader("x-forwarded-for"); if(ip == null || iplength() == 0 || "unknown"equalsIgnoreCase(ip)) { ip = requestgetHeader("Proxy-Client-IP"); } if(ip == null || iplength() == 0 || "unknown"equalsIgnoreCase(ip)) { ip = requestgetHeader("WL-Proxy-Client-IP"); } if(ip == null || iplength() == 0 || "unknown"equalsIgnoreCase(ip)) { ip = requestgetRemoteAddr(); } return ip; }

可是,如果通过了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串Ip值,究竟哪个才是真正的用户端的真实IP呢?

答案是取X-Forwarded-For中第一个非unknown的有效IP字符串。

如:X-Forwarded-For:1921681110, 1921681120, 1921681130, 1921681100用户真实IP为: 1921681110

希望可以帮到你的忙

祝你学习愉快

   获得项目服务器的IP大概做法是在配置文件里面进行配置,可以使服务器已启动便执行,示例如下:

启动服务器的时候启动一个类,可以在webxml中配置,如下:

<servlet>

    <servlet-name></servlet-name>

    <servlet-class></servlet-class>

    <init-param>

      <param-name>basedir</param-name>

      <param-value></param-value>

    </init-param>

    <load-on-startup>1</load-on-startup>

  </servlet>

指明你需要启动的servlet即可

requestgetHeader("REDIRECT_URL");

或试试

那在servlet里边或者action调用requestgetRequestURL()就是了。

如果是获得容器内部的请求URI:requestgetRequestURI(),两个方法挺象的。

/

  获取访问者IP地址

  <p>在一般情况下使用RequestgetRemoteAddr()即可,但是经过nginx等反向代理软件后,这个方法会失效。</p>

  <p>本方法先从Header中获取X-Real-IP,如果不存在再从X-Forwarded-For获得第一个IP(用,分割)。</p>

  <p>如果还不存在则调用RequestgetRemoteAddr()。</p>

  @param request

  @return

 /

public static String getIp(HttpServletRequest request) {

String ip = requestgetHeader("X-Real-IP");

if (ValidateUtilisNotEmpty(ip) && !"unknown"equalsIgnoreCase(ip)) {

return ip;

}

ip = requestgetHeader("X-Forwarded-For");

if (ValidateUtilisNotEmpty(ip) && !"unknown"equalsIgnoreCase(ip)) {

int index = ipindexOf(",");

if (index != -1) {

return ipsubstring(0, index);

} else {

return ip;

}

} else {

return requestgetRemoteAddr();

}

}

DABAN RP主题是一个优秀的主题,极致后台体验,无插件,集成会员系统
网站模板库 » JAVA获取IP地址

0条评论

发表评论

提供最优质的资源集合

立即查看 了解详情