① 如何用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(); } }
是否可以解決您的問題?