/**
*
*/
package com.duoguo.usb;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.filechooser.FileSystemView;
import org.apache.log4j.Logger;
import com.duoguo.Service.SocketServer;
import com.duoguo.Service.po.ReturnPo;
import com.duoguo.usb.po.UsbPo;
/**
* 磁盘检查 1、检测磁盘是本地磁盘还是可移动磁盘; 2、显示相应的磁盘名称,磁盘总空间以及可用的空间
*
* @author shyboy(897948924@qq.com)
*
*/
public class DiskTest {
private static Logger logger = Logger.getLogger(DiskTest.class);
private static final int methodId = 1;
private final static String localDiskName = "本地磁盘";
private final static String removableDiskName = "可移动磁盘";
private final static String enLocalDiskName = "Local Disk";
private final static String enRemovableDiskName = "Removable Disk";
/**
* 获取磁盘信息
*
* @return Map<String,ReturnPo>
*/
public static Map<String, ReturnPo> getDiskInfo() {
List<UsbPo> list = new ArrayList<UsbPo>();
ReturnPo returnPo = new ReturnPo();
Map<String, ReturnPo> map = new HashMap<String, ReturnPo>();
FileSystemView fileSystemView = FileSystemView.getFileSystemView();// 获取FileSystemView对象
double unit = 1024 * 1024 * 1024;
File[] roots = File.listRoots();
for (File file : roots) {
UsbPo usbPo = new UsbPo();
String pathName = file.getPath();// 获取磁盘的路径名称
usbPo.setName(pathName);
String diskName = fileSystemView.getSystemTypeDescription(file);// 获取磁盘的类型描述信息
if (diskName.startsWith(localDiskName)
|| diskName.startsWith(enLocalDiskName))// 当磁盘为本地磁盘时
{
usbPo.setFlag(0);
} else if (diskName.startsWith(removableDiskName)
|| diskName.startsWith(enRemovableDiskName))// 当磁盘为可移动磁盘时
{
usbPo.setFlag(1);
}
long usableSpace = file.getUsableSpace();// 获取磁盘的可用空间
usbPo.setUsableSize(usableSpace / unit);
long totalSpace = file.getTotalSpace();// 获取磁盘的总空间
usbPo.setTotalSize(totalSpace / unit);
System.out.println("磁盘名称为:" + diskName + pathName);
System.out.println("总空间为:" + usbPo.getTotalSize() + "GB");
System.out.println("可用空间为:" + usbPo.getUsableSize() + "GB");
list.add(usbPo);
}
returnPo.setMethodId(methodId);
returnPo.setExcept(null);
returnPo.setResultFlag(true);
returnPo.setResultInfo(list);
logger.info(returnPo.getResultInfo());
map.put(SocketServer.resultInfo, returnPo);
return map;
}
/**
* @param args
*/
public static void main(String[] args) {
DiskTest.getDiskInfo();
}
}