Java中如何得到某个远程机器的磁盘信息,包括有几个盘符,盘符已用空间及总空间,jdk1.6中可以的到本地磁盘空间信息,是否也能的到远程的呢? 我正在做一个监控系统,监控机房的机器,这些机器用于存储试验数据,由于数据量很大,所以需要实时监控。当然,有权访问这些机器了。不知道是否还要考虑权限的问题,这些机器都是有防火墙的,我这种想法是不是有问题,我就想读一下系统的磁盘信息,然后显示一下,磁盘空间不足就报警。
比如,上传一段视频,要时时的显示磁盘空间占用率,磁盘空间不足就报警

解决方案 »

  1.   

    读取本地的可以了,那读取网络中的应该也行.具体也没做过,我想,应该从路径,IP,端口这几个方面考虑.
    还要涉及到一些网络的命令,如,ping等等,我也就想到这些了.
      

  2.   

    写一客户端程序吧,然后用socket,不然我感觉有点难,若真的实现了,给我一ip我就能得到你机器的数据。这不好吧。
      

  3.   

    package cn.business.manager.itemSource;import java.io.File;
    import java.math.BigDecimal;public class test {
    private long totalspace;
    private long freespace;
    private long usedspace;
    private String ip;
    private File[] roots;
    private String[] patharray; public test() {
    totalspace = 0;
    freespace = 0;
    usedspace = 0;
    // ip="127.0.0.1";
    roots = File.listRoots();
    } public test(String i) { totalspace = 0;
    freespace = 0;
    usedspace = 0;
    ip = i;
    File f = new File("\\\\192.168.10.3\\C$");
    roots = f.listRoots();
    } public test(String[] apatharray) {
    totalspace = 0;
    freespace = 0;
    usedspace = 0;
    patharray = apatharray; // File f=new File("\\\\192.168.126.119\\c$");
    // roots=f.listRoots();
    } public long GetAllTotalSpaceByPath() {
    try {
    for (int i = 0; i < patharray.length; i++) {
    String[] ipstr = patharray[i].split("------");
    String ph = "\\\\"
    + ipstr[0]
    + "\\"
    + ((ipstr[1].replace(":", "")).replace("/", ""))
    .replace("\\", "") + "$"; File f = new File(ph);
    totalspace += f.getTotalSpace();
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    return totalspace;
    } public long GetTotalSpaceByPath(String str) {
    File f = null;
    try {
    String[] ipstr = str.split("------");
    String ph = "\\\\"
    + ipstr[0]
    + "\\"
    + ((ipstr[1].replace(":", "")).replace("/", "")).replace(
    "\\", "") + "$"; f = new File(ph); } catch (Exception e) {
    e.printStackTrace();
    } return f.getTotalSpace();
    } public long GetFreeSpaceByPath(String str) {
    File f = null;
    try {
    String[] ipstr = str.split("------");
    String ph = "\\\\"
    + ipstr[0]
    + "\\"
    + ((ipstr[1].replace(":", "")).replace("/", "")).replace(
    "\\", "") + "$"; f = new File(ph); } catch (Exception e) {
    e.printStackTrace();
    } return f.getFreeSpace();
    } public long GetAllFreeSpaceByPath() {
    try {
    for (int i = 0; i < patharray.length; i++) {
    String[] ipstr = patharray[i].split("------");
    String ph = "\\\\"
    + ipstr[0]
    + "\\"
    + ((ipstr[1].replace(":", "")).replace("/", ""))
    .replace("\\", "") + "$"; File f = new File(ph);
    freespace += f.getFreeSpace();
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    return freespace;
    } public long GetAllTotalSpace() { for (File _file : roots) {
    totalspace += _file.getTotalSpace(); }
    return totalspace;
    } public long GetAllFreeSpace() { for (File _file : roots) {
    freespace += _file.getFreeSpace();
    }
    return freespace;
    } public String[] GetDiskName() {
    String[] perdiskname; perdiskname = new String[roots.length];
    for (int i = 0; i < roots.length; i++) {
    perdiskname[i] = roots[i].getAbsolutePath(); }
    return perdiskname;
    } public long[] GetTotalSpace() {
    long[] pertotalspace; pertotalspace = new long[roots.length];
    for (int i = 0; i < roots.length; i++) {
    pertotalspace[i] = roots[i].getTotalSpace();
    }
    return pertotalspace;
    } public long[] GetFreeSpace() { long[] perfreespace; perfreespace = new long[roots.length];
    for (int i = 0; i < roots.length; i++) {
    perfreespace[i] = roots[i].getFreeSpace();
    }
    return perfreespace;
    } public int GetNum() {
    return roots.length;
    } public static void main(String args[]) {
    test sp = new test();
    float totalSapce = (float) sp.GetTotalSpaceByPath("192.168.10.53------d:")/ (1024 * 1024 * 1024);
    System.out.println(totalSapce);
    float freeSpace = (float) sp.GetFreeSpaceByPath("192.168.10.53------d:")/ (1024 * 1024 * 1024);
    System.out.println(freeSpace);
    float usedSpace = totalSapce - freeSpace;
    float freeUse = (freeSpace / totalSapce) * 100;
    BigDecimal b1 = new BigDecimal(totalSapce);
    BigDecimal b2 = new BigDecimal(freeSpace);
    BigDecimal b3 = new BigDecimal(usedSpace);
    BigDecimal b4 = new BigDecimal(freeUse);
    float total = b1.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
    float used = b3.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
    float free = b2.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
    float useFree = b4.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
    System.out.println("总容量:" + total);
    System.out.println("已用容量:" + used);
    System.out.println("剩余容量:" + free);
    System.out.println("剩余百分比:" + useFree + "%");
    //String[] strarray = { "192.168.10.53------d:" };
    //test sp = new test(strarray);
    // float totalSapce = (float) sp.GetAllTotalSpaceByPath()
    // / (1024 * 1024 * 1024);
    // float freeSpace = (float) sp.GetAllFreeSpaceByPath()
    // / (1024 * 1024 * 1024);
    // float usedSpace = totalSapce - freeSpace;
    // float freeUse = (freeSpace / totalSapce) * 100;
    // BigDecimal b1 = new BigDecimal(totalSapce);
    // BigDecimal b2 = new BigDecimal(freeSpace);
    // BigDecimal b3 = new BigDecimal(usedSpace);
    // BigDecimal b4 = new BigDecimal(freeUse);
    // float total = b1.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
    // float used = b3.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
    // float free = b2.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
    // float useFree = b4.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
    // System.out.println("总容量:" + total);
    // System.out.println("已用容量:" + used);
    // System.out.println("剩余容量:" + free);
    // System.out.println("剩余百分比:" + useFree + "%");
    }
    }
      

  4.   

    不过这样要用到远程计算机上的用户名和密码
    不如做一个webservices返磁盘阵列上的信息
      

  5.   

    采用SNMP 读取远程主机信息进行监控
      

  6.   

    写个jsp程序不就行了吗,很简单啊。