FileUtil.java
3.69 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package com.cjs.cms.util.file;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.multipart.MultipartFile;
import com.cjs.cms.util.net.RestException;
/**
* 文件工具类
*
* @author tongyufu
*
*/
public class FileUtil {
public static final String ENCODING = "UTF-8";
private static Logger logger = LogManager.getLogger(FileUtil.class);
// 上传本地路径
@Value("${upload.article}")
public static String uploadPath;
// 上传服务器路径
@Value("${article.url}")
public static String imageUrl;
/**
* 读取src目录下的配置文件。
*
* @param source
* 配置文件名,不带"/"
* @return 文件内容
*/
public static String readSourceAsString(String source) {
try {
URI uri = FileUtil.class.getResource("/" + source).toURI();
return FileUtils.readFileToString(new File(uri), ENCODING);
} catch (Exception e) {
logger.error("读取文件出错", e);
throw new RestException("读取文件出错:" + e.getMessage());
}
}
/**
* 读取配置文件。该方法不会缓存已读文件。
*
* @param propertyFile
* 配置文件名,不带"/"
* @return 文件内容
*/
public static Properties readProperty(String propertyFile) {
try {
Properties props = new Properties();
props.load(FileUtil.class.getResourceAsStream("/" + propertyFile));
return props;
} catch (IOException e) {
logger.error("读取文件出错", e);
throw new RestException("读取文件出错:" + e.getMessage());
}
}
/** 获取后缀名(包含.),如果没有后缀,返回"" @author trunks */
public static String getSuffix(String filename) {
if (StringUtils.isBlank(filename) || filename.indexOf(".") == -1) {
return "";
}
return filename.substring(filename.lastIndexOf(".")).trim();
}
/**
* 移动文件到指定target
*
* @param source
* 被移动文件
* @param target
* 目标路径
* @throws IOException
* 如果在service中调用,请在catch中 throw ServiceException,确保事务回滚
* @author trunks
*/
public static void transferFile(MultipartFile source, String target) throws IOException {
File targetFile = new File(target);
targetFile.getParentFile().mkdirs();
source.transferTo(targetFile);
}
// 获得当前时间
public static String QueryArticleImgPrefix() {
String nowtime = new Date().getTime() + "";
return nowtime;
}
/**
* 创建月份目录(单月前面补0)
*
* @param parent 父路径
* @return 目录完整路径(以/结尾)
*/
public static String makeMonthDir(String parent) {
String dir = "";
Calendar calendar = Calendar.getInstance();
int month = calendar.get(Calendar.MONTH) + 1;
if (month < 10) {
dir += "0";
}
dir += month;
if (!parent.endsWith("/")) {
parent += "/";
}
dir = parent + dir;
File file = new File(dir);
if (!file.exists()) {
file.mkdirs();
}
return dir + "/";
}
}