FileTplService.java
2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.thinkgem.jeesite.modules.cms.service;
import com.thinkgem.jeesite.modules.cms.entity.FileTpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.servlet.ServletContext;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* User: songlai
* Date: 13-8-27
* Time: 下午4:56
*/
@Service
@Transactional(readOnly = true)
public class FileTplService {
@Autowired
ServletContext context;
public List<String> getNameListByPrefix(String path) {
List<FileTpl> list = getListByPath(path, false);
List<String> result = new ArrayList<String>(list.size());
for (FileTpl tpl : list) {
result.add(tpl.getName());
}
return result;
}
public List<FileTpl> getListByPath(String path, boolean directory) {
File f = new File(context.getRealPath(path));
if (f.exists()) {
File[] files = f.listFiles();
if (files != null) {
List<FileTpl> list = new ArrayList<FileTpl>();
for (File file : files) {
if(file.isFile() || directory)
list.add(new FileTpl(file, context.getRealPath("")));
}
return list;
} else {
return new ArrayList<FileTpl>(0);
}
} else {
return new ArrayList<FileTpl>(0);
}
}
public List<FileTpl> getListForEdit(String path){
List<FileTpl> list = getListByPath(path, true);
List<FileTpl> result = new ArrayList<FileTpl>();
result.add(new FileTpl(new File(context.getRealPath(path)), context.getRealPath("")));
getAllDirectory(result, list);
return result;
}
private void getAllDirectory(List<FileTpl> result, List<FileTpl> list){
for (FileTpl tpl : list) {
result.add(tpl);
if(tpl.isDirectory()){
getAllDirectory(result, getListByPath(tpl.getName(), true));
}
}
}
public FileTpl getFileTpl(String name) {
File f = new File(context.getRealPath(name));
if (f.exists()) {
return new FileTpl(f, "");
} else {
return null;
}
}
}