FileUtils

First Post:

Last Update:

Word Count:
674

Read Time:
3 min

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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
public final class FileUtils {
private static final String PREFIX_PATH = StrUtil.format(
"{userDir}{separator}docker{separator}static-file{separator}",
MapUtil.builder("userDir", System.getProperties().get("user.dir").toString())
.put("separator", File.separator)
.build()
);

// private static String PREFIX_PATH = "/docker/static-file/";

static {
log.info("初始化文件存放路径: {}", PREFIX_PATH);
}

/**
* 保存文件
*
* @param file
* @return
*/
public static FileOperation save(MultipartFile file) {
String originalFilename = file.getOriginalFilename();

String extName = FileUtil.extName(originalFilename);
String uuid = UUID.randomUUID().toString().replace("-", "");

// File newFile = new File(PREFIX_PATH + uuid + "." + extName);
File newFile = new File(PREFIX_PATH + originalFilename);

if (newFile.exists()) {
// 存在重名文件, 则删除
newFile.deleteOnExit();
log.info("删除重名文件: " + newFile.getAbsolutePath());
}

if (!newFile.getParentFile().exists()) {
// 递归创建文件夹
log.info("创建上级目录{}", newFile.getParentFile().getAbsolutePath());
newFile.getParentFile().mkdirs();
}

try {
file.transferTo(newFile);
} catch (IOException e) {
log.warn("创建" + newFile.getAbsolutePath() + "文件失败: " + e.getMessage(), e);
return FileOperation.error(newFile, e, FileOperation.SAVE);
}
return FileOperation.ok(newFile, FileOperation.SAVE);
}


/**
* 更新文件
*
* @param path
* @param isAbsolute
* @param file
* @return
*/
public static FileOperation update(String path, boolean isAbsolute, MultipartFile file) {
File oldFile = new File(isAbsolute ? path : PREFIX_PATH + path);

if (oldFile.exists()) {
log.info("删除旧文件{}", oldFile.getAbsolutePath());
}

if (!oldFile.getParentFile().exists()) {
log.info("创建上级目录{}", oldFile.getParentFile().getAbsolutePath());
oldFile.mkdirs();
}

try {
file.transferTo(oldFile);
} catch (IOException e) {
log.warn("更新文件" + oldFile.getAbsolutePath() + "文件失败: " + e.getMessage(), e);
return FileOperation.error(oldFile, e, FileOperation.UPDATE);
}

return FileOperation.ok(oldFile, FileOperation.UPDATE);
}

/**
* 删除文件
*
* @param path
* @param isAbsolute
* @return
*/
public static FileOperation delete(String path, boolean isAbsolute) {
File file = new File(isAbsolute ? path : PREFIX_PATH + path);

if (!file.exists()) {
FileOperation error = FileOperation.error(file, new FileNotFoundException(file.getAbsolutePath()), FileOperation.DELETE);
log.warn("文件{}已被删除", error.getRelativePath());
return error;
}

file.deleteOnExit();

return FileOperation.ok(file, FileOperation.DELETE);
}

public static class FileOperation {

public static final String SAVE = "save";
public static final String UPDATE = "update";
public static final String DELETE = "delete";

private boolean ok;

private String operation;

private File file;

private String relativePath;

private Throwable cause;

public FileOperation(boolean ok, File file, Throwable cause, String operation) {
this.ok = ok;
this.file = file;
if (file.getAbsolutePath().indexOf(PREFIX_PATH) == 0) {
this.relativePath = file.getAbsolutePath().substring(PREFIX_PATH.length());
}
this.cause = cause;
this.operation = operation;
}

public boolean ok() {
return ok;
}

public String getRelativePath() {
return relativePath;
}

public String getAbsolutePath() {
return file.getAbsolutePath();
}

public Throwable getCause() {
return cause;
}

public static FileOperation ok(File file, String operation) {
return new FileOperation(true, file, null, operation);
}

public static FileOperation error(File file, Throwable cause, String operation) {
return new FileOperation(false, file, cause, operation);
}
}
}

WebConfig

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@Component
public class WebConfig implements WebMvcConfigurer {

//yml中的路径
@Value(value = "${file.path}")
private String filePath;

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
String PREFIX_PATH = StrUtil.format(
"{userDir}{separator}src{separator}main{separator}resources{separator}static{separator}file{separator}",
MapUtil.builder("userDir", System.getProperties().get("user.dir").toString())
.put("separator", File.separator)
.build()
);

log.info("资源映射位置: [{}]", filePath);
registry.addResourceHandler("/file/**")
.setCacheControl(CacheControl.noCache())
.addResourceLocations("file:" + filePath)
.addResourceLocations("file:" + PREFIX_PATH);
}
}