FileUtil.java
1.77 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
package com.cjs.site.util.file;
import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;
/**
* 文件工具类
*
* @author tongyufu
*
*/
public class FileUtil {
public static final String ENCODING = "UTF-8";
/** 获取后缀名(包含.),如果没有后缀,返回"" @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 + "/";
}
}