[英]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.