www.stfm.net > jAvA 获取ip

jAvA 获取ip

import java.net.*;public class Test6 {public static void main(String[] args) {// TODO Auto-generated method stubInetAddress ia=null;try {ia=ia.getLocalHost();String localname=ia.getHostName();String localip=ia.getHostAddress();...

为适应不同的网络情况,提供这个类:看下面的代码吧,Copy过去就能用。 import javax.servlet.http.HttpServletRequest; public class HttpUtil { public static String getIpAddr(HttpServletRequest request) { String ip = request.getHeader...

《java》中提取本地IP的方法如下: private static void getIpAddressByNetworkInterface() { try { Enumeration nets = NetworkInterface.getNetworkInterfaces(); NetworkInterface net; InetAddress inetAddress; while (nets.hasMoreElement...

public static void main(String[] args) { try { // 获取计算机名 String name = InetAddress.getLocalHost().getHostName(); // 获取IP地址 String ip = InetAddress.getLocalHost().getHostAddress(); System.out.println("计算机名:"+name)...

新建一个ServerSocket对象然后用accept()方法接受请求连接的Socket对象在调用获得的Socket对象的getInetAddress()方法获取InetAddress对象在调用InetAddress对象的getHostAddress方法来获取IP地址。

这代码的问题只会出现在本机访问服务,获取ip时才会出现。 而且如果你本机通过localhost、127.0.0.1和本机真实地址访问时获取到的ip也有区别,有兴趣可以试试

server = new SocketServer(3389); Socket s = server.accept(); s.getInetAddress().getHostAddress()

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 =...

request.getRemoteAddr()是获得客户端的ip地址。 request.getRemoteHost()是获得客户端的主机名。

public String getIP(String name){ InetAddress address = null; try { address = InetAddress.getByName(name); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("获取失败"...

网站地图

All rights reserved Powered by www.stfm.net

copyright ©right 2010-2021。
www.stfm.net内容来自网络,如有侵犯请联系客服。zhit325@qq.com