
ip地址容易获取,request中的getRemoteAddress方法能得到。至于地区的IP,这个可以找找看,应该有对应的表格什么的!
>
//看看这个代码如何。
import javanetInetAddress;
import javanetUnknownHostException;
import javautilProperties;
import javautilSet;
public class TestSystemProperties {
public static void main(String [] args){
InetAddress netAddress = getInetAddress();
Systemoutprintln("host ip:" + getHostIp(netAddress));
Systemoutprintln("host name:" + getHostName(netAddress));
Properties properties = SystemgetProperties();
Set<String> set = propertiesstringPropertyNames(); //获取java虚拟机和系统的信息。
for(String name : set){
Systemoutprintln(name + ":" + propertiesgetProperty(name));
}
}
public static InetAddress getInetAddress(){
try{
return InetAddressgetLocalHost();
}catch(UnknownHostException e){
Systemoutprintln("unknown host!");
}
return null;
}
public static String getHostIp(InetAddress netAddress){
if(null == netAddress){
return null;
}
String ip = netAddressgetHostAddress(); //get the ip address
return ip;
}
public static String getHostName(InetAddress netAddress){
if(null == netAddress){
return null;
}
String name = netAddressgetHostName(); //get the host address
return name;
}
}
这个代码简单明了,就是调用现成的InetAddress类
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(); } }
是否可以解决您的问题?
方法如下:
方法一,使用CMD命令:
public static String getLocalIPForCMD(){
StringBuilder sb = new StringBuilder();
String command = "cmdexe /c ipconfig | findstr IPv4";
try {
Process p = RuntimegetRuntime()exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(pgetInputStream()));
String line = null;
while((line = brreadLine()) != null){
line = linesubstring(linelastIndexOf(":")+2,linelength());
sbappend(line);
}
brclose();
pdestroy();
} catch (IOException e) {
eprintStackTrace();
}
return sbtoString();
}
方法二,使用Java方法:
public static String getLocalIPForJava(){
StringBuilder sb = new StringBuilder();
try {
Enumeration<NetworkInterface> en = NetworkInterfacegetNetworkInterfaces();
while (enhasMoreElements()) {
NetworkInterface intf = (NetworkInterface) ennextElement();
Enumeration<InetAddress> enumIpAddr = intfgetInetAddresses();
while (enumIpAddrhasMoreElements()) {
InetAddress inetAddress = (InetAddress) enumIpAddrnextElement();
if (!inetAddressisLoopbackAddress() && !inetAddressisLinkLocalAddress()
&& inetAddressisSiteLocalAddress()) {
sbappend(inetAddressgetHostAddress()toString()+"\n");
}
}
}
} catch (SocketException e) { }
return sbtoString();
}
java获取外网ip地址方法:
public class Main {
public static void main(String[] args) throws SocketException {
Systemoutprintln(MaingetRealIp());
}
public static String getRealIp() throws SocketException {
String localip = null;// 本地IP,如果没有配置外网IP则返回它
String netip = null;// 外网IP
Enumeration<NetworkInterface> netInterfaces =
NetworkInterfacegetNetworkInterfaces();
InetAddress ip = null;
boolean finded = false;// 是否找到外网IP
while (netInterfaceshasMoreElements() && !finded) {
NetworkInterface ni = netInterfacesnextElement();
Enumeration<InetAddress> address = nigetInetAddresses();
while (addresshasMoreElements()) {
ip = addressnextElement();
if (!ipisSiteLocalAddress()
&& !ipisLoopbackAddress()
&& ipgetHostAddress()indexOf(":") == -1) {// 外网IP
netip = ipgetHostAddress();
finded = true;
break;
} else if (ipisSiteLocalAddress()
&& !ipisLoopbackAddress()
&& ipgetHostAddress()indexOf(":") == -1) {// 内网IP
localip = ipgetHostAddress();
}
}
}
if (netip != null && !""equals(netip)) {
return netip;
} else {
return localip;
}
}
}
import javanet;
public class Test6 {
public static void main(String[] args) {
// TODO Auto-generated method stub
InetAddress ia=null;
try {
ia=iagetLocalHost();
String localname=iagetHostName();
String localip=iagetHostAddress();
Systemoutprintln("本机名称是:"+ localname);
Systemoutprintln("本机的ip是 :"+localip);
} catch (Exception e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
}
}
以上就是关于想写一个javaweb程序点按钮获取点击者的ip地址并从数据库中进行IP对比 相同则显示这个IP地址所在地区的名全部的内容,包括:想写一个javaweb程序点按钮获取点击者的ip地址并从数据库中进行IP对比 相同则显示这个IP地址所在地区的名、怎么编写一个Java程序来获取ip地址、java 怎么根据IP地址获取主机名等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)