public boolean listRoots(String specialId, String specialUrl)
throws ToolException {
specialManagerDao.deleteSpecialStoreAndResourse(specialId);
Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
uploadDate = dateFormat.format(now);
File rootFile = new File(specialUrl);
if (!rootFile.exists()) { // 用户所给的地址不存在
return false;
} else {
if (rootFile.isFile()) {
String name = rootFile.getName();
String storePath = rootFile.getAbsolutePath();
String resourceSuf = name.substring(name.lastIndexOf('.') + 1);
totalFileNum++;
// 取文件的目录,添加到专辑资源目录结构中
String specialStoreId = specialManagerDao.insertspecialStore(
specialId, name, 1, "1", 1);
String resourcesize = String.valueOf(rootFile.length());
// 将文件信息添加到资源基本信息表中
specialManagerDao.insertSpecialResource(name, specialId,
storePath, resourceSuf, uploadDate, specialStoreId,resourcesize);
} else {
// 创建最根目录,添加到专辑资源目录中,
// grade=1;storeid=000000000001;upnode=0;orderdisplay=1;
String name = rootFile.getName();
String specialStoreId = specialManagerDao.insertspecialStore(
specialId, name, 1, "1", 1);
// 获取所有根目录下的所有文件夹和文件信息 int orderdisplay=0; specialUrl
listDirectory(rootFile, specialStoreId, 1, specialId);
}
specialManagerDao.updateSpecialAmountById(totalFileNum, specialId);
return true;
}
}
private void listDirectory(File dir, String upnode, int upnodeGrade,
String specialId) throws ToolException {
int thisGrade = upnodeGrade + 1;
if (!dir.exists()) {
System.out.println("文件名称不存在!");
} else {
File[] files = dir.listFiles();
if (files != null) {
int orderDisplay = 0;
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) { // 文件夹
orderDisplay++;
String name = files[i].getName();
// 创建本目录信息 orderdisplay=j++; grade = upgrade+1; upnode=
// upnode
String specialStoreId = specialManagerDao
.insertspecialStore(specialId, name, thisGrade,
upnode, orderDisplay);
listDirectory(files[i], specialStoreId, thisGrade,
specialId);
} else { // 文件
String name = files[i].getName();
String storePath = files[i].getAbsolutePath();
String resourceSuf = name.substring(name
.lastIndexOf('.') + 1);
String resourcesize = String.valueOf(files[i].length());
specialManagerDao.insertSpecialResource(name,
specialId, storePath, resourceSuf, uploadDate,
upnode,resourcesize);
totalFileNum++;
}
}
}
}
}
public boolean listVisualRoots(String specialId, String specialUrl)
throws ToolException {
specialManagerDao.deleteSpecialStoreAndResourse(specialId);
Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
uploadDate = dateFormat.format(now);
String rootPath = ServletActionContext.getServletContext().getRealPath(specialUrl);
File rootFile = new File(rootPath);
if (!rootFile.exists()) { // 用户所给的地址不存在
return false;
} else {
if (rootFile.isFile()) {
String name = rootFile.getName();
String storePath = specialUrl+"/"+name;
String resourceSuf = name.substring(name.lastIndexOf('.') + 1);
totalFileNum++;
// 取文件的目录,添加到专辑资源目录结构中
String specialStoreId = specialManagerDao.insertspecialStore(
specialId, name, 1, "1", 1);
String resourcesize = String.valueOf(rootFile.length());
// 将文件信息添加到资源基本信息表中
specialManagerDao.insertSpecialResource(name, specialId,
storePath, resourceSuf, uploadDate, specialStoreId,resourcesize);
} else {
// 创建最根目录,添加到专辑资源目录中,
// grade=1;storeid=000000000001;upnode=0;orderdisplay=1;
String name = rootFile.getName();
String specialStoreId = specialManagerDao.insertspecialStore(
specialId, name, 1, "1", 1);
// 获取所有根目录下的所有文件夹和文件信息 int orderdisplay=0; specialUrl
listVisualDirectory(rootFile, specialStoreId, 1, specialId,specialUrl);
}
specialManagerDao.updateSpecialAmountById(totalFileNum, specialId);
return true;
}
}
private void listVisualDirectory(File dir, String upnode, int upnodeGrade,
String specialId,String parentVisualPath) throws ToolException {
int thisGrade = upnodeGrade + 1;
if (!dir.exists()) {
System.out.println("文件名称不存在!");
} else {
File[] files = dir.listFiles();
if (files != null) {
int orderDisplay = 0;
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) { // 文件夹
orderDisplay++;
String name = files[i].getName();
String thisPath = parentVisualPath+"/"+name;
// 创建本目录信息 orderdisplay=j++; grade = upgrade+1; upnode=
// upnode
String specialStoreId = specialManagerDao
.insertspecialStore(specialId, name, thisGrade,
upnode, orderDisplay);
listVisualDirectory(files[i], specialStoreId, thisGrade,
specialId,thisPath);
} else { // 文件
String name = files[i].getName();
String storePath = parentVisualPath+"/"+name;
String resourceSuf = name.substring(name
.lastIndexOf('.') + 1);
String resourcesize = String.valueOf(files[i].length());
specialManagerDao.insertSpecialResource(name,
specialId, storePath, resourceSuf, uploadDate,
upnode,resourcesize);
totalFileNum++;
}
}
}
}
}