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()方法吧