Java如何取得计算机硬件信息么

Python012

Java如何取得计算机硬件信息么,第1张

可以的

IMonitorService接口:  

public interface IMonitorService {  

public MonitorInfoBean getMonitorInfoBean() throws Exception  

}  

MonitorInfoBean类:  

/*采集系统存取数据JavaBean*/  

public class MonitorInfoBean implements Comparable<MonitorInfoBean> {  

/** 操作系统. */  

private String osName  

/** 总的物理内存. */  

private float totalMemorySize  

/** 已使用的物理内存. */  

private float usedMemory  

/** cpu使用率. */  

private double cpuRatio  

/** 主机IP地址 */  

private String mIpAddress  

/** 数据存储时间 */  

private String dDateTime  

/**内存使用率*/  

private float memoryRatio  

/**linux下Buffers内存*/  

private float buffersMemory   

/**linux下Cached内存*/  

private float cachedMemory  

public float getBuffersMemory() {  

   return buffersMemory  

}  

public float getCachedMemory() {  

   return cachedMemory  

}  

public String getDDateTime() {  

   return dDateTime  

}  

public void setDDateTime(String dateTime) {  

   dDateTime = dateTime  

}  

public String getMIpAddress() {  

   return mIpAddress  

}  

public void setMIpAddress(String ipAddress) {  

   mIpAddress = ipAddress  

}  

public String getOsName() {  

   return osName  

}  

public void setOsName(String osName) {  

   this.osName = osName  

}  

public float getTotalMemorySize() {  

   return totalMemorySize  

}  

public void setTotalMemorySize(float totalMemorySize) {  

   this.totalMemorySize = totalMemorySize  

}  

public float getUsedMemory() {  

   return usedMemory  

}  

public void setUsedMemory(long usedMemory) {  

   this.usedMemory = usedMemory  

}  

public double getCpuRatio() {  

   return cpuRatio  

}  

public void setCpuRatio(double cpuRatio) {  

   this.cpuRatio = cpuRatio  

}  

public int compareTo(MonitorInfoBean m) {  

   String stra = this.getDDateTime()  

   String strb = m.getDDateTime()  

   Timestamp a = Timestamp.valueOf(stra)  

   Timestamp b = Timestamp.valueOf(strb)  

   if (a.before(b)) {  

    return -1  

   } else if (a.after(b)) {  

    return 1  

   } else {  

    return 0  

   }  

}  

public float getMemoryRatio() {  

   return memoryRatio  

}  

public void setMemoryRatio(float memoryRatio) {  

   this.memoryRatio = memoryRatio  

}  

public void setUsedMemory(float usedMemory) {  

   this.usedMemory = usedMemory  

}  

public void setBuffersMemory(float buffersMemory) {  

   this.buffersMemory = buffersMemory  

}  

public void setCachedMemory(float cachedMemory) {  

   this.cachedMemory = cachedMemory  

}  

}  

import java.io.BufferedReader  

import java.io.File  

import java.io.FileInputStream  

import java.io.FileNotFoundException  

import java.io.IOException  

import java.io.InputStream  

import java.io.InputStreamReader  

import java.io.LineNumberReader  

import java.net.Inet6Address  

import java.net.InetAddress  

import java.net.NetworkInterface  

import java.net.SocketException  

import java.net.UnknownHostException  

import java.util.Enumeration  

import java.util.StringTokenizer  

import sun.management.ManagementFactory  

import com.sun.management.OperatingSystemMXBean

public class Main {

  public static void main(String[] args) {

    /* Total number of processors or cores available to the JVM */

    System.out.println("Available processors (cores): " + 

        Runtime.getRuntime().availableProcessors())

    /* Total amount of free memory available to the JVM */

    System.out.println("Free memory (bytes): " + 

        Runtime.getRuntime().freeMemory())

    /* This will return Long.MAX_VALUE if there is no preset limit */

    long maxMemory = Runtime.getRuntime().maxMemory()

    /* Maximum amount of memory the JVM will attempt to use */

    System.out.println("Maximum memory (bytes): " + 

        (maxMemory == Long.MAX_VALUE ? "no limit" : maxMemory))

    /* Total memory currently available to the JVM */

    System.out.println("Total memory available to JVM (bytes): " + 

        Runtime.getRuntime().totalMemory())

    /* Get a list of all filesystem roots on this system */

    File[] roots = File.listRoots()

    /* For each filesystem root, print some info */

    for (File root : roots) {

      System.out.println("File system root: " + root.getAbsolutePath())

      System.out.println("Total space (bytes): " + root.getTotalSpace())

      System.out.println("Free space (bytes): " + root.getFreeSpace())

      System.out.println("Usable space (bytes): " + root.getUsableSpace())

    }

  }}

不可以,这属于用户隐私信息,再说如果可以的话,那我们的电脑你不觉得很危险吗?随便一个网页都可以看到你的硬件信息,很危险的。我原来遇到过用户想这样搞,最后跟用户谈,不能实现,修改成cookie验证了,像韩家_四少发的代码,只可以在特定环境下使用,还需要考虑网络状态,而且nbtstat -a是windows 下面的命令,根本不可行。