导航:首页 > 配服务器 > java得到服务器的ip地址

java得到服务器的ip地址

发布时间:2024-04-23 18:13:33

① 如何用java获取服务器的地址呢大虾指教下呗,小女子这里谢谢喽!

import java.net.*;
public class NetTool{
InetAddress myIPaddress=null;
InetAddress myServer=null;

public static void main( String args[]){

NetTool mytool;
mytool=new NetTool();

System.out.println("Your host IP is: " + mytool.getMyIP());
System.out.println("The Server IP is :" +mytool.getServerIP());

}

//取得LOCALHOST的IP地址
public InetAddress getMyIP() {
try { myIPaddress=InetAddress.getLocalHost();}
catch (UnknownHostException e) {}
return (myIPaddress);
}
//取得 www.abc.com 的IP地址
public InetAddress getServerIP(){
try {myServer=InetAddress.getByName("www.abc.com");}
catch (UnknownHostException e) {}
return (myServer);
}

}

② java怎么通过域名获取ip地址

importjava.net.InetAddress;
importjava.net.UnknownHostException;
publicclassTestInetAddress{
InetAddressmyIpAddress=null;
InetAddress[]myServer=null;
publicstaticvoidmain(Stringargs[]){
TestInetAddressaddress=newTestInetAddress();
System.out.println("YourhostIPis:"+address.getLocalhostIP());
Stringdomain=www.jb51.net;
System.out.println("Theserverdomainnameis:"+domain);
InetAddress[]array=address.getServerIP(domain);
intcount=0;
for(inti=1;i<array.length;i++){
System.out.println("ip"+i+""+address.getServerIP(domain)[i-1]);
count++;
}
System.out.println("IPaddresstotal:"+count);
}
/**
*获得localhost的IP地址
*@return
*/
(){
try{
myIpAddress=InetAddress.getLocalHost();
}catch(UnknownHostExceptione){
e.printStackTrace();
}
return(myIpAddress);
}
/**
*获得某域名的IP地址
*@paramdomain域名
*@return
*/
publicInetAddress[]getServerIP(Stringdomain){
try{
myServer=InetAddress.getAllByName(domain);
}catch(UnknownHostExceptione){
e.printStackTrace();
}
return(myServer);
}
}

③ java 调用接口获取服务器IP和端口号

package test5;
public interface A {
void setAll(int ID,String IP);
String getAll();
}
package test5;
public class B implements A{
String IP;
int ID;
public void setAll(int ID,String IP) {
this.ID=ID;
this.IP=IP;
}
public String getAll() {
return "ID为: "+ID+"\t\t"+"IP为: "+IP;
}
public static void main(String[] args) {
B b=new B();
b.setAll(1, "192.168.1.1");
System.out.println(b.getAll());
}
}

④ java如何获取当前时间,java如何获取ip地址

publicstaticvoidmain(String[]args){

try{

System.out.println("当前时间:"+newDate());

System.out.println("IP地址:"+InetAddress.getLocalHost());

}catch(UnknownHostExceptione){

e.printStackTrace();

}

}

⑤ 镐庢牱阃氲繃Java绋嫔簭 銮峰彇链链篒P鍦板潃..璇曚简鍑犵嶆柟娉曢兘涓嶈 銮峰彇镄勯兘鏄192.168.*.*杩欑.𨱍冲缑鍒扮绣缁泪P闾g嶏纴姹

鍦↗SP閲岋纴銮峰彇瀹㈡埛绔镄処P鍦板潃镄勬柟娉曟槸锛歳equest.getRemoteAddr锛堬级锛岃繖绉嶆柟娉曞湪澶ч儴鍒嗘儏鍐典笅閮芥槸链夋晥镄勚备絾鏄鍦ㄩ氲繃浜咥pache锛孲quid绛夊弽钖戜唬鐞呜蒋浠跺氨涓嶈兘銮峰彇鍒板㈡埛绔镄勭湡瀹滨P鍦板潃浜嗐
濡傛灉浣跨敤浜嗗弽钖戜唬鐞呜蒋浠讹纴鐢╮equest.getRemoteAddr锛堬级鏂规硶銮峰彇镄処P鍦板潃鏄锛127.0.0.1 鎴 192.168.1.110锛岃屽苟涓嶆槸瀹㈡埛绔镄勭湡瀹滨P銆

缁忚繃浠g悊浠ュ悗锛岀敱浜庡湪瀹㈡埛绔鍜屾湇锷′箣闂村炲姞浜嗕腑闂村眰锛屽洜姝ゆ湇锷″櫒镞犳硶鐩存帴𨰾垮埌瀹㈡埛绔镄処P锛屾湇锷″櫒绔搴旂敤涔熸棤娉旷洿鎺ラ氲繃杞鍙戣锋眰镄勫湴鍧杩斿洖缁椤㈡埛绔銆备絾鏄鍦ㄨ浆鍙戣锋眰镄凥TTP澶翠俊鎭涓锛屽炲姞浜哫锛岶ORWARDED锛岶OR淇℃伅銆傜敤浠ヨ窡韪铡熸湁镄勫㈡埛绔疘P鍦板潃鍜屽师𨱒ュ㈡埛绔璇锋眰镄勬湇锷″櫒鍦板潃銆傚綋鎴戜滑璁块梾 镞讹纴鍏跺疄骞朵笉鏄鎴戜滑娴忚埚櫒鐪熸h块梾鍒颁简链嶅姟鍣ㄤ笂镄刬ndex.jsp鏂囦欢锛岃屾槸鍏堢敱浠g悊链嶅姟鍣ㄥ幓璁块梾 锛屼唬鐞嗘湇锷″櫒鍐嶅皢璁块梾鍒扮殑缁撴灉杩斿洖缁欐垜浠镄勬祻瑙埚櫒锛屽洜涓烘槸浠g悊链嶅姟鍣ㄥ幓璁块梾index.jsp镄勶纴镓浠index.jsp涓阃氲繃request.getRemoteAddr锛堬级镄勬柟娉曡幏鍙栫殑IP瀹为檯涓婃槸浠g悊链嶅姟鍣ㄧ殑鍦板潃锛屽苟涓嶆槸瀹㈡埛绔镄処P鍦板潃銆

浜庢槸鍙寰楀嚭銮峰缑瀹㈡埛绔鐪熷疄IP鍦板潃镄勬柟娉曚竴锛

public String getRemortIP(HttpServletRequest request) {
if (request.getHeader("x-forwarded-for") == null) {
return request.getRemoteAddr();
}
return request.getHeader("x-forwarded-for");
}

鍙鏄褰撴垜璁块梾 镞讹纴杩斿洖镄処P鍦板潃濮嬬粓鏄痷nknown锛屼篃骞朵笉鏄濡备笂镓绀虹殑127.0.0.1 鎴 192.168.1.110浜嗭纴钥屾垜璁块梾 锛2046/index.jsp 镞讹纴鍒栾兘杩斿洖瀹㈡埛绔镄勭湡瀹滨P鍦板潃锛屽啓浜嗕釜鏂规硶铡婚獙璇併傚师锲犲嚭鍦ㄤ简Squid涓娿俿quid.conf 镄勯厤鍒舵枃浠 forwarded_for 椤归粯璁ゆ槸涓簅n锛屽傛灉 forwarded_for 璁炬垚浜 off 鍒欙细X-Forwarded-For锛 unknown

浜庢槸鍙寰楀嚭銮峰缑瀹㈡埛绔鐪熷疄IP鍦板潃镄勬柟娉曚簩锛

public String getIpAddr(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.getRemoteAddr();
}
return ip;
}

鍙鏄锛屽傛灉阃氲繃浜嗗氱骇鍙嶅悜浠g悊镄勮瘽锛孹-Forwarded-For镄勫煎苟涓嶆涓涓锛岃屾槸涓涓睮p鍊硷纴绌剁珶鍝涓镓嶆槸鐪熸g殑鐢ㄦ埛绔镄勭湡瀹滨P锻锛

绛旀堟槸鍙朮-Forwarded-For涓绗涓涓闱潆nknown镄勬湁鏁圛P瀛楃︿覆銆

濡傦细X-Forwarded-For锛192.168.1.110锛 192.168.1.120锛 192.168.1.130锛 192.168.1.100鐢ㄦ埛鐪熷疄IP涓猴细 192.168.1.110

⑥ JAVA获取IP地址

public static void main(String[] args) { try { // 获取计算机名 String name = InetAddress.getLocalHost().getHostName(); // 获取IP地址 String ip = InetAddress.getLocalHost().getHostAddress(); System.out.println("计算机名:"+name); System.out.println("IP地址:"+ip); } catch (UnknownHostException e) { System.out.println("异常:" + e); e.printStackTrace(); } }
是否可以解决您的问题?

阅读全文

与java得到服务器的ip地址相关的资料

热点内容
华为思科的配置命令 浏览:867
linux设置光盘启动 浏览:357
程序员俱乐部注册 浏览:17
洗洁精鸡蛋盘解压视频 浏览:291
企业内网加密app 浏览:49
腾讯云服务器怎么设置本地网络 浏览:623
日常程序员 浏览:513
哪个pe有bit加密功能 浏览:108
rust服务器是用什么ip 浏览:692
java随机字符串生成 浏览:551
数码宝贝编程人物 浏览:390
php多用户建站系统 浏览:624
加密防盗地上栓价位 浏览:671
比泽尔压缩机下载 浏览:420
深圳压缩机制造公司 浏览:882
如何给u盘单独文件加密码 浏览:284
恒温机故障源码 浏览:418
如何打开管家婆服务器支持者 浏览:266
安卓手机自带浏览器有什么用 浏览:532
老板咨询阿里云还是独立服务器 浏览:814