简体   繁体   English

如何使用Java计算远程计算机的磁盘空间?

[英]How can I calclulate the diskspace of a remote machine using java?

Can anyone suggest me how can I modify the below code which is currently calculating the free disk space(C:) of my local system (Windows OS) to calculate the free disk space (C:) of the remote machine (Windows OS) using java? 谁能建议我如何修改以下当前正在计算本地系统 (Windows OS) 的可用磁盘空间(C :)的 代码,以使用以下方法计算 远程计算机 (Windows OS) 的可用磁盘空间(C :) java吗?

import java.io.File;

public class DiskMemory
{
    public static void main(String[] args)
    {   
        File file = new File("C:");
        //total disk space in bytes.
        long totalSpace = file.getTotalSpace(); 
        //unallocated free disk space in bytes.
        long usableSpace = file.getUsableSpace(); 
        //unallocated free disk space available to current user.
        long freeSpace = file.getFreeSpace(); 



        System.out.println(" ==Total Memory Allocation == ");
        System.out.println("Total size : " + totalSpace + " bytes");
        System.out.println("Space free : " + usableSpace + " 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 : " + usableSpace /1024 /1024 + " mb");
        System.out.println("Space free : " + freeSpace /1024 /1024 + " mb");
    }
}

After looking at Apache's VFS, I found it overly complicated to get disk space usage, and it relies on jCIFS ( https://jcifs.samba.org/ ), which will do it with less hassel. 在查看了Apache的VFS之后,我发现获取磁盘空间使用情况过于复杂,它依赖于jCIFS( https://jcifs.samba.org/ ),这将减少麻烦。

SmbFile file = new SmbFile("smb://[user]:[pass]@[server]/[share]");
System.out.println(file.getDiskFreeSpace()); // Space free on share
System.out.println(file.length()); // Total space available on share

You could look into using commons VFS for accessing the remote file systems and determining the file size 您可以考虑使用Commons VFS来访问远程文件系统并确定文件大小

http://commons.apache.org/proper/commons-vfs/ http://commons.apache.org/proper/commons-vfs/

This would allow you to get a file representing the remote directory (FileObject in VFS lingo) and get the size much in the same way you are doing already. 这将允许您获取代表远程目录的文件(在VFS术语中为FileObject),并以与您已经执行的相同方式获得大小。 Have a look at the website for an example. 以网站为例。 You could use VFS to query your local disk space as well. 您也可以使用VFS查询本地磁盘空间。

Since you have access to all the machines whose disk space you want to calculate, you can write a simple client server program to solve the problem. 由于您可以访问要计算其磁盘空间的所有计算机,因此可以编写一个简单的客户端服务器程序来解决该问题。 The high level overview of the system will be the following : 该系统的高级概述如下:

Let C be your machine, let s1, s2, s3, ..., sN be the N machines whose disk space you want on C. 令C为您的机器,令s1,s2,s3,...,sN为您要在C上拥有其磁盘空间的N台机器。

On each the s machines, (s1, s2, etc), you need to run a server which is listening on some port (choose a port > 1024). 在每台s机器(s1,s2等)上,您需要运行一台正在某个端口上侦听的服务器(选择端口> 1024)。 On machine C, there should be a client which will connect to all the s machines to get the usage. 在机器C上,应该有一个客户端,它将连接到所有s机器以获取使用情况。

Look up client server programming in java, it's pretty straightforward. 在Java中查找客户端服务器编程,这非常简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何使用Java在远程计算机(LAN)中上传文件 - How can i upload file in remote machine(LAN) using java 如何使用Java在远程计算机上执行Linux命令? - How can I execute Linux commands on a remote machine using Java? 如何在远程计算机上将Java与R集成? - How can I integrate Java with R on a remote machine? 我们如何使用Java从本地计算机打开远程计算机上存在的文件? - How can we open a file present on remote machine from local machine using java? 如何与远程计算机上的对象通信? - How can I communicate with an object on a remote machine? 如何在Java中计算div服务器端中的文本行数 - How to calclulate the number of lines of text in a div server side in java 如何在远程Windows计算机上从Java运行批处理脚本 - How can I run a batch script from java on a remote windows machine 如何通过局域网访问我的java项目中的远程oracle数据库,我的机器上没有安装Oracle - How can i access Remote oracle Database in my java project via LAN, Oracle is not installed in my machine 使用 java runtim.exec 到 ssh 时,如何获得该远程机器的控制台 output? - When using java runtim.exec to ssh, how do I get console output of that remote machine? 如何使用Java在远程机器中打开路径并在该路径中写入文件 - How to open a path in remote machine and write a file in that path using java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM