문제

Can someone suggest how can I calculate the total and free disk-space of a window remote machine with a given ip say (1.10.11.14) and window login credentials say (username: test password : test) from my local machine using java codes. The below java code is very much capable and working to calculate the total and free disk space of my local machine but the query arises for remote machine.

How can I change the below code to calculate the disk space for C: drive in my remote machine(1.10.11.14). Also how can I import free and used disk storage values to an xls file.

import java.io.File;

public class DiskSpacecalaculate
{
    public static void main(String[] args)
    {   
        File file = new File("c:");
        long usableSpace = file.getUsableSpace(); ///unallocated / free disk space in bytes.
        long freeSpace = file.getFreeSpace(); //unallocated / free disk space in bytes.

        System.out.println(" === Partition Detail ===");


        System.out.println("Total size : " + totalSpace + " bytes");
        System.out.println("Space free : " + freeSpace + " bytes");

        System.out.println(" === mega bytes ===");
        System.out.println("Total size : " + totalSpace /1024 /1024 + " mb");
        System.out.println("Space free : " + freeSpace /1024 /1024 + " mb");
    }
}
도움이 되었습니까?

해결책

To calculate the disk size on your remote machine, you need a connection to your remote machine (obviously). The difficult question is how to establish this connection. As for any problem, there are some different solutions:

Mount the drive as a remote folder

If you can (I don't know if you can or want) mount the remote folder as a shared folder on your Windows machine. If you do so, you can just use our code as it is by changing the path of the file to the mounted folder. But this solution could be unpracticle and is not a "Java" solution.

Use an agent on your remote machine

Windows lacks of a solution like Linux's SSH to connect to a machine. So there is no easy way to connect to Windows machine. There are some tools like IBM's STAF (see here) which address this problem. You install STAF on the remote machine and on your local machine and can connect trough it. But it is a pretty ugly solution and is super insecure. But for a test system maybe enough.

Write your own service

There is no way to connect to a Windows machine (correct me if I am wrong) and tools like STAF are just bad workarounds. As you also concern about performance, I suggest you just deploy your tool to your Windows machine and provide a JSON-API on top of it. So you can take your browser, go to the machine and see how much space is left.

I know that's not what you hopped to hear, but you will have a bad time when you try to connect to a Windows machine...

다른 팁

** Update ** - Sorry, misread from remote machine, automatically jumped to the remote share conclusion... You can however share the C drive on each of the machines and only give a special account access to the share. Otherwise, the other post has the various suggestions you need to accomlish this.

You can use the java.io.File like your doing and define the path. I don't know if there's any limitations (I'm running this from a Windows host), but it works.

import java.io.File;

public class RemoteShareSpace {
   public static void main(String[] args) {

  try{
     // Works either way...

     //File f = new File("\\\\myServer\\ShareDir1\\ShareDir2\\ShareDir3");
     File f = new File("//myServer/ShareDir1/ShareDir2/ShareDir3");

     long totalSpace  = f.getTotalSpace();
     long usableSpace = f.getUsableSpace(); // unallocated / free disk space in bytes.
     long freeSpace   = f.getFreeSpace();   // unallocated / free disk space in bytes.

     boolean pathExists = f.exists();

     // if path exists
     if(pathExists)
     {

         System.out.println(" === Partition Detail ===");
         System.out.println("Total Space: " + totalSpace);
         System.out.println("Total size : " + totalSpace + " bytes");
         System.out.println("Space free : " + freeSpace + " bytes");

         System.out.println(" === mega bytes ===");
         System.out.println("Total size : " + totalSpace /1024 /1024 + " mb");
         System.out.println("Space free : " + freeSpace /1024 /1024 + " mb");
     }
  }catch(Exception e){
     e.printStackTrace();
 }
}
}

Output:

C:\Source>java RemoteShareSpace
=== Partition Detail ===
Total Space: 64315076608
Total size : 64315076608 bytes
Space free : 22259978240 bytes
=== mega bytes ===
Total size : 61335 mb
Space free : 21228 mb

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top