南开小巷

导航

java web代码规范:

每个类前要有注释,类前的注释格式是:

/**

*类是干什么的

*@author  编写该类的作者

*/

类中的每个方法前也要有注释:

/**

*该方法是干什么的

*@param 该方法中传入的参数

*@return

*/

/**
* 目录服务类
* @author X
*
*/
@Component("project.docm.catalog.CatalogService")
@SuppressWarnings("all")
public class CatalogService {
@Autowired
PlatParamItemService paramItemService;

/**
* 获取根目录
* @return
*/
private String getRootPath(){
return paramItemService.getParam("FILE_FOLDER").getEvalue();
}


private String getIndexPath(){
return paramItemService.getParam("INDEX_FOLDER").getEvalue();
}
/**
* 查询目录
* @param path
* @return
*/
public List getCatalogs(String path) throws Exception{
File file = new File(path);
List result = new ArrayList();
if(file.isDirectory()){
//根目录
Map m = new HashMap();
m.put("id",UUID.randomUUID().toString());
m.put("name", file.getName());
m.put("len", file.list().length);
m.put("path", ZipUtils.gzip(file.getPath()));
m.put("size", file.length());
m.put("iconCls", "fa fa-laptop");
m.put("date", new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date(file.lastModified())));
m.put("children", eachCatalog(file));
result.add(m);
}else{
throw new Exception("不是一个目录");
}
return result;
}

public List getFiles(File catalog){
List result = new ArrayList();
if(catalog.isDirectory() && catalog.exists()){
File[] files = catalog.listFiles();
for(File file : files){
if(file.isFile()){
Map m = new HashMap();
m.put("id",UUID.randomUUID().toString());
m.put("name", file.getName());
m.put("path", ZipUtils.gzip(file.getPath()));
m.put("size", file.length());
m.put("iconCls", "fa fa-laptop");
m.put("date", new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date(file.lastModified())));
result.add(m);
}
}
}
return result;
}

public List eachCatalog(File path){
if(path.isDirectory()){
File[] files = path.listFiles();
List dirs = new ArrayList();
for(File file : files){
if(file.isDirectory()){
Map m = new HashMap();
m.put("id",UUID.randomUUID().toString());
m.put("path", ZipUtils.gzip(file.getPath()));
m.put("name", file.getName());
m.put("len", file.list().length);
m.put("size", file.length());
m.put("iconCls", "fa fa-folder");
m.put("date", new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date(file.lastModified())));
m.put("children", eachCatalog(file));
dirs.add(m);
}
}
return dirs;
}
return null;
}

//新增目录
@Transactional
public Object saveCatalogs(File file){
if(!file.exists()){
if(file.mkdir()){
Map m = new HashMap();
m.put("id",UUID.randomUUID().toString());
m.put("name", file.getName());
m.put("len", file.list().length);
m.put("path", ZipUtils.gzip(file.getPath()));
m.put("size", file.length());
m.put("iconCls", "fa fa-folder");
m.put("date", new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date(file.lastModified())));
m.put("children", eachCatalog(file));
return m;
}
}
return false;
}
//删除目录
public boolean deleteCatalogs(File file) {
if(file.exists() && file.isDirectory()){
if(file.delete()){
//删除索引
try {
IndexHelper.deleteIndex(file, getIndexPath(), getRootPath());
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
}
return false;
}
/**
* 删除文件
* @param file
* @return
*/
public boolean deleteFile(File file) {
if(file.exists()){
return file.delete();
}
return false;
}
}

posted on 2015-11-18 14:39  南开小巷  阅读(382)  评论(0编辑  收藏  举报