「java获得host」Java获得resource目录
本篇文章给大家谈谈java获得host,以及Java获得resource目录对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
java如何获取机器名
java获取机器名,主要是使用InterAddress类,如下代码:
package com.qiu.lin.he;
import java.net.InetAddress;
public class Ceshi {
public static void main(String[] args) {
InetAddress addr = null;
String address = "";
try {
addr = InetAddress.getLocalHost();//新建一个InetAddress类
address = addr.getHostName().toString();// 获得本机名称
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(address);
}
}
结果如下:
Java如何获取本地计算机的IP地址和主机名
可以使用 InetAddress.getLocalHost(),代码如下:
import java.net.*;
public class App {
public static void main(String[] args) throws UnknownHostException {
InetAddress local = InetAddress.getLocalHost();
System.out.println("计算机名:" + local.getHostName());
System.out.println("IP:" + local.getHostAddress());
}
}
Java Web如何获取客户端的Hostname?
在Java web中获取hostname的方法:
使用request对象。用:
request.getHostName();
java 怎么根据IP地址获取主机名
//看看这个代码如何。
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Properties;
import java.util.Set;
public class TestSystemProperties {
public static void main(String [] args){
InetAddress netAddress = getInetAddress();
System.out.println("host ip:" + getHostIp(netAddress));
System.out.println("host name:" + getHostName(netAddress));
Properties properties = System.getProperties();
SetString set = properties.stringPropertyNames(); //获取java虚拟机和系统的信息。
for(String name : set){
System.out.println(name + ":" + properties.getProperty(name));
}
}
public static InetAddress getInetAddress(){
try{
return InetAddress.getLocalHost();
}catch(UnknownHostException e){
System.out.println("unknown host!");
}
return null;
}
public static String getHostIp(InetAddress netAddress){
if(null == netAddress){
return null;
}
String ip = netAddress.getHostAddress(); //get the ip address
return ip;
}
public static String getHostName(InetAddress netAddress){
if(null == netAddress){
return null;
}
String name = netAddress.getHostName(); //get the host address
return name;
}
}
这个代码简单明了,就是调用现成的InetAddress类
关于java获得host和Java获得resource目录的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。