Java中如何得到某个远程机器的磁盘信息,包括有几个盘符,盘符已用空间及总空间,jdk1.6中可以的到本地磁盘空间信息,是否也能的到远程的呢? 我正在做一个监控系统,监控机房的机器,这些机器用于存储试验数据,由于数据量很大,所以需要实时监控。当然,有权访问这些机器了。不知道是否还要考虑权限的问题,这些机器都是有防火墙的,我这种想法是不是有问题,我就想读一下系统的磁盘信息,然后显示一下,磁盘空间不足就报警。
比如,上传一段视频,要时时的显示磁盘空间占用率,磁盘空间不足就报警
比如,上传一段视频,要时时的显示磁盘空间占用率,磁盘空间不足就报警
解决方案 »
- 黑《Thinking in Java》的人这么多,平反一下
- jquery菜鸟问点验证提交问题
- enterprisedt的ftp包用来编写FTP上传下载时,中文问题?
- 请问如何获取JTable里的JCheckBox的值?
- 一个小程序,搞不定,请大家帮忙!!!
- 要学java了,不知从何开始,忘前辈们指点一下,不胜感激!
- ResultSet不能更新和滚动
- 110分:JAVA中Socket如何发送和接收字节数组?
- 买了《程序员大本营2001·Java》的大虾看过来!!!
- 关于不同类成员变量的调用问题
- 跪求高手一览:生成表格样式的图片其字段的内容不确定导致格式无法完美定位
- 谁有jmf比较全面的学习文档啊?
还要涉及到一些网络的命令,如,ping等等,我也就想到这些了.
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 + "%");
}
}
不如做一个webservices返磁盘阵列上的信息