❶ java如何獲取當前時間,java如何獲取ip地址
publicstaticvoidmain(String[]args){
try{
System.out.println("當前時間:"+newDate());
System.out.println("IP地址:"+InetAddress.getLocalHost());
}catch(UnknownHostExceptione){
e.printStackTrace();
}
}
❷ java如何獲取當前登錄ip
第一種:獲取本機的IP
Enumeration<NetworkInterface>
netInterfaces
=
null;
try
{
netInterfaces
=
NetworkInterface.getNetworkInterfaces();
while
(netInterfaces.hasMoreElements())
{
NetworkInterface
ni
=
netInterfaces.nextElement();
System.out.println("DisplayName:"
+
ni.getDisplayName());
System.out.println("Name:"
+
ni.getName());
Enumeration<InetAddress>
ips
=
ni.getInetAddresses();
while
(ips.hasMoreElements())
{
System.out.println("IP:"
+
ips.nextElement().getHostAddress());
ipTemp=
ni.getInetAddresses().nextElement().getHostAddress();
if(ipTemp!="127.0.0.1"
&&
!"127.0.0.1".equals(ipTemp))
{
ip=ipTemp;
}
}
}
}catch(Exception
ee)
{
ee.printStackTrace();
}
第二種:也是本機的:
InetAddress
addr
=
InetAddress.getLocalHost();
ip=addr.getHostAddress().toString();//獲得本機IP
❸ 如何用java獲取本地ip地址
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
/**
* @author Becolette
* @description TODO
* @date 2015-11-5 下午01:58:46
*/
public class IpAddress {
public static String find() {
List<String> ips = new ArrayList<String>();
// 返回所有網路介面的一個枚舉實例
Enumeration<?> allNetInterfaces = null;
try {
allNetInterfaces = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e) {
e.printStackTrace();
}
InetAddress ip = null;
while (allNetInterfaces.hasMoreElements()) {
NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
while (addresses.hasMoreElements()) {
// 獲得當前網路介面
ip = (InetAddress) addresses.nextElement();
if (ip != null && ip instanceof Inet4Address && ip.getHostAddress().indexOf(".") != -1) {
ips.add(ip.getHostAddress());
}
}
}
if (ips.size() == 1) {
return ips.get(0);
} else {
for (String ipa : ips) {
if (!"127.0.0.1".equals(ipa)) {
return ipa;
}
}
}
return MacAddress.find();
}
}
❹ 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
public void PingAll() throws Exception{
//首先得到本機的IP,得到網段
InetAddress host = InetAddress.getLocalHost();
String hostAddress = host.getHostAddress();
int k=0;
k=hostAddress.lastIndexOf(".");
String ss = hostAddress.substring(0,k+1);
for(int i=1;i <=255;i++){ //對所有區域網Ip
String iip=ss+i;
Ping(iip);
}
❻ JAVA怎麼獲取IP地址
java代碼獲取ip地址方法是
調用java.net包下面的的InetAddress類獲取。
❼ Java怎樣獲取當前機器外網IP
java獲取本機的外網ip示例:
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 獲取本機外網IP地址
* 思想是訪問網站http://checkip.dyndns.org/,得到返回的文本後解析出本機在外網的IP地址
* @author pieryon
*
*/
public class ExternalIpAddressFetcher {
// 外網IP提供者的網址
private String externalIpProviderUrl;
// 本機外網IP地址
private String myExternalIpAddress;
public ExternalIpAddressFetcher(String externalIpProviderUrl) {
this.externalIpProviderUrl = externalIpProviderUrl;
String returnedhtml = fetchExternalIpProviderHTML(externalIpProviderUrl);
parse(returnedhtml);
}
/**
* 從外網提供者處獲得包含本機外網地址的字元串
* 從http://checkip.dyndns.org返回的字元串如下
* <html><head><title>Current IP Check</title></head><body>Current IP Address: 123.147.226.222</body></html>
* @param externalIpProviderUrl
* @return
*/
private String fetchExternalIpProviderHTML(String externalIpProviderUrl) {
// 輸入流
InputStream in = null;
// 到外網提供者的Http連接
HttpURLConnection httpConn = null;
try {
// 打開連接
URL url = new URL(externalIpProviderUrl);
httpConn = (HttpURLConnection) url.openConnection();
// 連接設置
HttpURLConnection.setFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.setRequestProperty("User-Agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
// 獲取連接的輸入流
in = httpConn.getInputStream();
byte[] bytes=new byte[1024];// 此大小可根據實際情況調整
// 讀取到數組中
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead=in.read(bytes, offset, bytes.length-offset)) >= 0) {
offset += numRead;
}
// 將位元組轉化為為UTF-8的字元串
String receivedString=new String(bytes,"UTF-8");
// 返回
return receivedString;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
httpConn.disconnect();
} catch (Exception ex) {
ex.printStackTrace();
}
}
// 出現異常則返回空
return null;
}
/**
* 使用正則表達式解析返回的HTML文本,得到本機外網地址
* @param html
*/
private void parse(String html){
Pattern pattern=Pattern.compile("(\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})", Pattern.CASE_INSENSITIVE);
Matcher matcher=pattern.matcher(html);
while(matcher.find()){
myExternalIpAddress=matcher.group(0);
}
}
/**
* 得到本機外網地址,得不到則為空
* @return
*/
public String getMyExternalIpAddress() {
return myExternalIpAddress;
}
public static void main(String[] args){
ExternalIpAddressFetcher fetcher=new ExternalIpAddressFetcher("http://checkip.dyndns.org/");
System.out.println(fetcher.getMyExternalIpAddress());
}
}
❽ java如何獲取當前登錄ip
第一種:獲取本機的IP
Enumeration<NetworkInterface> netInterfaces = null;
try {
netInterfaces = NetworkInterface.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
NetworkInterface ni = netInterfaces.nextElement();
System.out.println("DisplayName:" + ni.getDisplayName());
System.out.println("Name:" + ni.getName());
Enumeration<InetAddress> ips = ni.getInetAddresses();
while (ips.hasMoreElements()) {
System.out.println("IP:" + ips.nextElement().getHostAddress());
ipTemp= ni.getInetAddresses().nextElement().getHostAddress();
if(ipTemp!="127.0.0.1" && !"127.0.0.1".equals(ipTemp))
{
ip=ipTemp;
}
}
}
}catch(Exception ee)
{
ee.printStackTrace();
}
第二種:也是本機的:
InetAddress addr = InetAddress.getLocalHost();
ip=addr.getHostAddress().toString();//獲得本機IP