0.前端是用的easyui的treegrid
<meta http-equiv="X-UA-Compatible" content="IE=9" />
<div class="easyui-layout" fit="true">
<div region="center" id="FileLog" style="padding:0px;border:0px">
<table id="test" title="" class="easyui-treegrid" style="width:401px;height:550px"
url="data\treegrid_data.json"
rownumbers="true"
idField="id" treeField="name">
<thead>
<tr>
<th field="name" width="160">名称</th>
<th field="size" width="100" align="right">大小</th>
<th field="date" width="110">修改日期</th>
<div style="margin-left:340px">
<a href="#" class="easyui-linkbutton" onclick="xiazai()" iconCls="icon-put">下 载</a>
</div>
</tr>
</thead>
</table>
</div>
</div>
第一句代码是为了兼容ie9 url是存放json字符串的文件位置,当时做这个功能的时候,用了这种方式,动态修改json。
1.文件对象 实体类
public class FileObject {
private String id;
private String name ;
private String size ;
private String date ;
private List<FileObject> children =new ArrayList<FileObject>();
}
2.后台方法(修改json文件)
//获取tomcat所在目录
String Dpath=System.getProperty("catalina.home")+File.separator+"webapps"+File.separator+"fpxtjsp"+File.separator+"file";
File rootFile = new File(Dpath);
File[] fs = rootFile.listFiles();
Arrays.sort(fs);
// 生成的json字符串
String FileString = formatString(rootFile);
// 将字符串写入到指定文件内
try {
// 存放json字符串的文件夹
String Parpath = System.getProperty("catalina.home") + File.separator + "webapps" + File.separator + "fpxtjsp" + File.separator + "data";
File parFile = new File(Parpath);
// 如果没有文件夹 则创建
if (!parFile.exists()) {
parFile.mkdir();
}
String dataPath = Parpath + File.separator + "treegrid_data.json";
File file = new File(dataPath);
// 如果没有该文件 则创建
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(FileString);
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
3.读取文件夹 转为json字符串
public static String formatString(File file) {
FileObject fo = new FileObject();
fo.setId("");
fo = getSubFile(file, fo);
return JSONObject.toJSONString(fo.getChildren());
}
//递归查找文件夹
public static FileObject getSubFile(File file, FileObject fo) {
String size = "";
long fileS = file.length();
// DecimalFormat df = new DecimalFormat("#.00");
if (fileS < 1024) {
size = fileS + "BT";
// size = df.format((double) fileS) + "BT";
} else if (fileS < 1048576) {
size = fileS / 1024 + "KB";
// size = df.format((double) fileS / 1024) + "KB";
} else if (fileS < 1073741824) {
size = fileS / 1048576 + "MB";
// size = df.format((double) fileS / 1048576) + "MB";
} else {
size = fileS / 1073741824 + "GB";
// size = df.format((double) fileS / 1073741824) +"GB";
}
fo.setSize(size);
fo.setName(file.getName());
Date date = new Date(file.lastModified());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm");
fo.setDate(sdf.format(date));
if (file.isDirectory()) {
File[] files = file.listFiles();
Arrays.sort(files, new CompratorByLastModified());
if (files.length >= 0) {
for (int i = 0; i < files.length; i++) {
FileObject subfo = new FileObject();
subfo.setId(fo.getId() + String.valueOf(i + 1));
fo.getChildren().add(getSubFile(files[i], subfo));
}
}
}
return fo;
}
//根据最后一次修改时间排序
static class CompratorByLastModified implements Comparator<File> {
public int compare(File f1, File f2) {
long diff = f1.lastModified() - f2.lastModified();
if (diff > 0)
return -1;
else if (diff == 0)
return 0;
else
return 1;
}
public boolean equals(Object obj) {
return true;
}
}