java主机名
A. java获取Unix主机名
System.out.println(System.getProperty("user.name")); //主机名 //获取当前可用专CPU数量
// Runtime.getRuntime().availableProcessors();
// //获取当前可用内属存数
// Runtime.getRuntime().totalMemory();
// //当前已用内存
// Runtime.getRuntime().freeMemory();
// //
// System.out.println(Runtime.getRuntime().maxMemory());
B. Java如何获取本地计算机的IP地址和主机名
可以使用 InetAddress.getLocalHost(),代码如下:
importjava.net.*;
publicclassApp{
publicstaticvoidmain(String[]args)throwsUnknownHostException{
InetAddresslocal=InetAddress.getLocalHost();
System.out.println("计算机名:"+local.getHostName());
System.out.println("IP:"+local.getHostAddress());
}
}
C. java 怎么根据IP地址获取主机名
//看看这个代码如何。
importjava.net.InetAddress;
importjava.net.UnknownHostException;
importjava.util.Properties;
importjava.util.Set;
{
publicstaticvoidmain(String[]args){
InetAddressnetAddress=getInetAddress();
System.out.println("hostip:"+getHostIp(netAddress));
System.out.println("hostname:"+getHostName(netAddress));
Propertiesproperties=System.getProperties();
Set<String>set=properties.stringPropertyNames();//获取java虚拟机和系统的信息。
for(Stringname:set){
System.out.println(name+":"+properties.getProperty(name));
}
}
(){
try{
returnInetAddress.getLocalHost();
}catch(UnknownHostExceptione){
System.out.println("unknownhost!");
}
returnnull;
}
publicstaticStringgetHostIp(InetAddressnetAddress){
if(null==netAddress){
returnnull;
}
Stringip=netAddress.getHostAddress();//gettheipaddress
returnip;
}
publicstaticStringgetHostName(InetAddressnetAddress){
if(null==netAddress){
returnnull;
}
Stringname=netAddress.getHostName();//getthehostaddress
returnname;
}
}
这个代码简单明了,就是调用现成的InetAddress类
D. JAVA WEB项目有怎么方法能获取访问自己网站的用户的IP和主机名
今天在搞(JavaScript)获取客户端IP的小程序,上网搜了下,好多在现在的系统和浏览器中的都无效,很无奈,在Chrome、FireFox中很少搞到直接利用ActiveX获取IP等的JS脚本。下面的代码是我在所有windowsNT5.0及以上的系统上都测试通过的,给出代码:
方法一(只针对IE且客户端的IE允许AcitiveX运行,通过平台:XP,SERVER03,2000):
获取客户端IP。
[html] view plain
<HTML>
<HEAD>
<TITLE>GetLocalIP</TITLE>
</HEAD>
<BODY>
获取IP:
<script language="JavaScript"> function GetLocalIPAddr(){ var oSetting = null; var ip = null; try{ oSetting = new ActiveXObject("rcbdyctl.Setting"); ip = oSetting.GetIPAddress; if (ip.length == 0){ return "没有连接到Internet"; } oSetting = null; }catch(e){ return ip; } return ip; } document.write(GetLocalIPAddr()+"<br/>") </script>
</BODY>
</HTML>
方法二(所有的平台及浏览器):
获取客户端处在网络中的IP,前提是客户得联网。
[html] view plain
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>JavaScript获取客户端IP[利用新浪接口]</title>
</head>
<body>
<script type="text/javascript" src="http://counter.sina.com.cn/ip/" charset="gb2312"></script> <!--获取接口数据,注意charset -->
<script type="text/javascript">
document.writeln("IP地址:"+ILData[0]+"<br />"); //输出接口数据中的IP地址
document.writeln("地址类型:"+ILData[1]+"<br />"); //输出接口数据中的IP地址的类型
document.writeln("地址类型:"+ILData[2]+"<br />"); //输出接口数据中的IP地址的省市
document.writeln("地址类型:"+ILData[3]+"<br />"); //输出接口数据中的IP地址的
document.writeln("地址类型:"+ILData[4]+"<br />"); //输出接口数据中的IP地址的运营商
</script>
</body>
</html>
方法三(只针对IE且客户端的IE允许AcitiveX运行):
调用VBS脚本,获取计算机名(有些人不知道什么是计算机名,简单解释就是它是这台机器的物理名称而不是你在用的那个用户名)和登录的用户名。
[html] view plain
<HTML>
<HEAD>
<TITLE>WMI Scripting HTML</TITLE>
</HEAD>
<BODY>
<script language=javascript>
var WshShell =new ActiveXObject("WScript.Shell");
document.write("计算机名 = "+ WshShell.ExpandEnvironmentStrings("%COMPUTERNAME%")+"<br/>");
document.write("登录用户名 = "+ WshShell.ExpandEnvironmentStrings("%USERNAME%")+"<br/>");
</script>
</BODY>
</HTML>
方法四(只针对IE且客户端的IE允许AcitiveX运行):
获取计算机名、登录的用户名和域名(如果你加入了域,显示你这台机器处在哪个域中)。
[html] view plain
<HTML>
<HEAD>
<TITLE>WMI Scripting HTML</TITLE>
</HEAD>
<BODY>
<script language=javascript>
var wshNetwork = new ActiveXObject("WScript.Network");
document.write("域名 = "+ wshNetwork.UserDomain+"<br/>");
document.write("计算机名 = "+ wshNetwork.ComputerName+"<br/>");
document.write("登录用户名 = "+ wshNetwork.UserName+"<br/>");
</script>
</BODY>
</HTML>
方法五(只针对IE且客户端的IE允许AcitiveX运行):
能够获取到局域网IP地址,本机的MAC,以及机器名(代码来源网络)。
[html] view plain
<html>
<head>
<title></title>
</head>
<body>
<object classid="CLSID:76A64158-CB41-11D1-8B02-00600806D9B6" id="locator" style="display:none;visibility:hidden"></object>
<object classid="CLSID:75718C9A-F029-11d1-A1AC-00C04FB6C223" id="foo" style="display:none;visibility:hidden"></object>
<form name="myForm">
<br/>MAC地址:<input type="text" name="macAddress">
<br/>IP地址:<input type="text" name="ipAddress">
<br/>主机名:<input type="text" name="hostName">
</form>
</body>
</html>
<script language="javascript">
var sMacAddr="";
var sIPAddr="";
var sDNSName="";
var service = locator.ConnectServer();
service.Security_.ImpersonationLevel=3;
service.InstancesOfAsync(foo, 'Win32_NetworkAdapterConfiguration');
</script>
<script FOR="foo" EVENT="OnObjectReady(objObject,objAsyncContext)" LANGUAGE="JScript">
if(objObject.IPEnabled != null && objObject.IPEnabled != "undefined" && objObject.IPEnabled == true){
if(objObject.IPEnabled && objObject.IPAddress(0) !=null && objObject.IPAddress(0) != "undefined")
sIPAddr = objObject.IPAddress(0);
if(objObject.MACAddress != null &&objObject.MACAddress != "undefined")
sMacAddr = objObject.MACAddress;
if(objObject.DNSHostName != null &&objObject.DNSHostName != "undefined")
sDNSName = objObject.DNSHostName;
}
</script>
<script FOR="foo" EVENT="OnCompleted(hResult,pErrorObject, pAsyncContext)" LANGUAGE="JScript">
myForm.macAddress.value=sMacAddr;
myForm.ipAddress.value=sIPAddr;
myForm.hostName.value=sDNSName;
</script>
E. 编写Java程序,获得指定端口的主机名、主机地址和本机地址。
不知道要得到那个主机名和 ,本机地址可以得到,request.getLocalhost();好像是这个,看看api就好了
F. 帮我用java写个能获取主机IP,主机名的窗体
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* @className: SystemTool
* @description: 与系统相关的一些常用工具方法. 目前实现的有:获取MAC地址、地址、主机名
* @author: 笑遍世界
* @createTime: 2010-11-13 下午08:03:44
*/
public class SystemTool {
/**
* 获取当前操作系统名称.
* return 操作系统名称 例如:windows xp,linux 等.
*/
public static String getOSName() {
return System.getProperty("os.name").toLowerCase();
}
/**
* 获取unix网卡的mac地址.
* 非windows的系统默认调用本方法获取.如果有特殊系统请继续扩充新的取mac地址方法.
* @return mac地址
*/
public static String getUnixMACAddress() {
String mac = null;
BufferedReader bufferedReader = null;
Process process = null;
try {
process = Runtime.getRuntime().exec("ifconfig eth0");// linux下的命令,一般取eth0作为本地主网卡 显示信息中包含有mac地址信息
bufferedReader = new BufferedReader(new InputStreamReader(process
.getInputStream()));
String line = null;
int index = -1;
while ((line = bufferedReader.readLine()) != null) {
index = line.toLowerCase().indexOf("hwaddr");// 寻找标示字符串[hwaddr]
if (index >= 0) {// 找到了
mac = line.substring(index +"hwaddr".length()+ 1).trim();// 取出mac地址并去除2边空格
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
bufferedReader = null;
process = null;
}
return mac;
}
/**
* 获取widnows网卡的mac地址.
* @return mac地址
*/
public static String getWindowsMACAddress() {
String mac = null;
BufferedReader bufferedReader = null;
Process process = null;
try {
process = Runtime.getRuntime().exec("ipconfig /all");// windows下的命令,显示信息中包含有mac地址信息
bufferedReader = new BufferedReader(new InputStreamReader(process
.getInputStream()));
String line = null;
int index = -1;
while ((line = bufferedReader.readLine()) != null) {
index = line.toLowerCase().indexOf("physical address");// 寻找标示字符串[physical address]
if (index >= 0) {// 找到了
index = line.indexOf(":");// 寻找":"的位置
if (index>=0) {
mac = line.substring(index + 1).trim();// 取出mac地址并去除2边空格
}
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
bufferedReader = null;
process = null;
}
return mac;
}
/**
* @return 本机主机名
*/
public static String getHostName() {
InetAddress ia = null;
try {
ia = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (ia == null ) {
return "some error..";
}
else
return ia.getHostName();
}
/**
* @return 本机IP 地址
*/
public static String getIPAddress() {
InetAddress ia = null;
try {
ia = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (ia == null ) {
return "some error..";
}
else
return ia.getHostAddress();
}
/**
* 测试用的main方法.
*
* @param argc
* 运行参数.
*/
public static void main(String[] argc) {
String os = getOSName();
System.out.println("OS Type:"+os);
if(os.startsWith("windows")){
//本地是windows
String mac = getWindowsMACAddress();
System.out.println("MAC Address:"+mac);
}else{
//本地是非windows系统 一般就是unix
String mac = getUnixMACAddress();
System.out.println(mac);
}
System.out.println("HostName:"+getHostName());
System.out.println("IPAddress:"+getIPAddress());
G. java中如何获得指定端口的主机名
用getPost()方法吧