Java中如何得到某个远程机器的磁盘信息,包括有几个盘符,盘符已用空间及总空间,jdk1.6中可以的到本地磁盘空间信息,是否也能的到远程的呢?
import java.io.File;public class SpaceChecker 
{
private long totalspace;
private long freespace;
private long usedspace;
private String ip; 
private File[] roots;



public SpaceChecker()
{
totalspace=0;
freespace=0;
usedspace=0;
//ip="127.0.0.1";
roots = File.listRoots();
}

public SpaceChecker(String i)
{
//怎么得到IP为i的磁盘信息?
totalspace=0;
freespace=0;
usedspace=0;
ip=i;
File f=new File("\\\\192.168.126.119\\c$");
roots=f.listRoots();
}
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;
}
   
}

解决方案 »

  1.   

    我想这个除了exec来运行本机OS的命令之外,没有其他好办法了
      

  2.   

    写一个服务端和客户端程序,在要监控的机器上运行客户端,通过Socket通信,把客户端的磁盘信息告诉服务端。这种解决方案不知道是否符合你的要求。
      

  3.   

    谢谢大家了,后来我指定了某个IP的某个磁盘,可以访问了:
    package mainframe;
    import java.io.File;public class SpaceChecker 
    {
    private long totalspace;
    private long freespace;
    private long usedspace;
    private String ip; 
    private File[] roots;
    private String[] patharray;




    public SpaceChecker()
    {
    totalspace=0;
    freespace=0;
    usedspace=0;
    //ip="127.0.0.1";
    roots = File.listRoots();
    }

    public SpaceChecker(String i)
    {

    totalspace=0;
    freespace=0;
    usedspace=0;
    ip=i;
    File f=new File("\\\\192.168.126.119\\c$");
    roots=f.listRoots();
    }

    public SpaceChecker(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[])
    {

    String[] strarray={"192.168.126.119------C:","192.168.126.119------D:"};
    SpaceChecker sp=new SpaceChecker(strarray);
    System.out.println(sp.GetAllTotalSpaceByPath());
    }
       
    }