1、tomcat是一款免费的开源Web服务器,如果部署在本地,那么对应的那么为localhost,对应地址为127.0.0.1。
例子:可以通过http://localhost:8080/项目root值访问,也可以通过http://127.0.0.1/项目root值访问。
如果部署在服务器(linux)系统类,则需要通过服务器的Ip地址进行访问。
2、下面说说怎么获取Ip地址:
获取本地的Ip地址:
public static void main(String[] args) {
try {
InetAddress address = InetAddress.getLocalHost();//获取的是本地的IP地址 //PC-20140317PXKX/192.168.0.121
String hostAddress = address.getHostAddress());//192.168.0.121
InetAddress address1 = InetAddress.getByName("www.wodexiangce.cn");//获取的是该网站的ip地址,比如我们所有的请求都通过nginx的,所以这里获取到的其实是nginx服务器的IP地
String hostAddress1 = address1.getHostAddress());//124.237.121.122
InetAddress[] addresses = InetAddress.getAllByName("www.baidu.com");//根据主机名返回其可能的所有InetAddress对象
for(InetAddress addr:addresses){
System.out.println(addr);//www.baidu.com/14.215.177.38
//www.baidu.com/14.215.177.37
}
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
获取服务器的Ip地址
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
public class IpUtil {
public IpUtil() {
}
public static String getIpAddress() {
try {
Enumeration netInterfaces = NetworkInterface.getNetworkInterfaces();
InetAddress ip = null;
while(true) {
NetworkInterface ni;
do {
if (!netInterfaces.hasMoreElements()) {
if (ip != null) {
return ip.getHostAddress();
}
ip = InetAddress.getLocalHost();
if (ip == null) {
throw new RuntimeException("can not get ip address");
}
return ip.getHostAddress();
}
ni = (NetworkInterface)netInterfaces.nextElement();
} while(ni.getName().contains("docker"));
Enumeration nii = ni.getInetAddresses();
while(nii.hasMoreElements()) {
InetAddress inetAdd = (InetAddress)nii.nextElement();
if (!inetAdd.isLoopbackAddress()) {
if (inetAdd.isSiteLocalAddress()) {
return inetAdd.getHostAddress();
}
if (inetAdd == null) {
ip = inetAdd;
}
}
}
}
} catch (UnknownHostException | SocketException var5) {
throw new RuntimeException(var5);
}
}
/**
* 获取真实ip地址
* @param request
* @return
*/
public static String getIpAddress(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
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.getHeader("HTTP_CLIENT_IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
public static long ipToLong(String strIp) {
long[] ip = new long[4];
int position1 = strIp.indexOf(".");
int position2 = strIp.indexOf(".", position1 + 1);
int position3 = strIp.indexOf(".", position2 + 1);
ip[0] = Long.parseLong(strIp.substring(0, position1));
ip[1] = Long.parseLong(strIp.substring(position1 + 1, position2));
ip[2] = Long.parseLong(strIp.substring(position2 + 1, position3));
ip[3] = Long.parseLong(strIp.substring(position3 + 1));
return (ip[0] << 24) + (ip[1] << 16) + (ip[2] << 8) + ip[3];
}
public static String longToIP(long longIp) {
StringBuffer sb = new StringBuffer("");
sb.append(String.valueOf(longIp >>> 24));
sb.append(".");
sb.append(String.valueOf((longIp & 16777215L) >>> 16));
sb.append(".");
sb.append(String.valueOf((longIp & 65535L) >>> 8));
sb.append(".");
sb.append(String.valueOf(longIp & 255L));
return sb.toString();
}
public static void main(String[] args) {
System.out.println( IpUtil.getIpAddress());
}
}