提交 | 用户 | 时间
|
e57a89
|
1 |
package com.jcdm.common.utils.file; |
懒 |
2 |
|
|
3 |
import java.io.File; |
|
4 |
import java.io.IOException; |
|
5 |
import java.nio.file.Paths; |
|
6 |
import java.util.Objects; |
|
7 |
import org.apache.commons.io.FilenameUtils; |
|
8 |
import org.springframework.web.multipart.MultipartFile; |
|
9 |
import com.jcdm.common.config.MesConfig; |
|
10 |
import com.jcdm.common.constant.Constants; |
|
11 |
import com.jcdm.common.exception.file.FileNameLengthLimitExceededException; |
|
12 |
import com.jcdm.common.exception.file.FileSizeLimitExceededException; |
|
13 |
import com.jcdm.common.exception.file.InvalidExtensionException; |
|
14 |
import com.jcdm.common.utils.DateUtils; |
|
15 |
import com.jcdm.common.utils.StringUtils; |
|
16 |
import com.jcdm.common.utils.uuid.Seq; |
|
17 |
|
|
18 |
/** |
|
19 |
* 文件上传工具类 |
|
20 |
* |
|
21 |
* @author jc |
|
22 |
*/ |
|
23 |
public class FileUploadUtils |
|
24 |
{ |
|
25 |
/** |
|
26 |
* 默认大小 50M |
|
27 |
*/ |
|
28 |
public static final long DEFAULT_MAX_SIZE = 50 * 1024 * 1024; |
|
29 |
|
|
30 |
/** |
|
31 |
* 默认的文件名最大长度 100 |
|
32 |
*/ |
|
33 |
public static final int DEFAULT_FILE_NAME_LENGTH = 100; |
|
34 |
|
|
35 |
/** |
|
36 |
* 默认上传的地址 |
|
37 |
*/ |
|
38 |
private static String defaultBaseDir = MesConfig.getProfile(); |
|
39 |
|
|
40 |
public static void setDefaultBaseDir(String defaultBaseDir) |
|
41 |
{ |
|
42 |
FileUploadUtils.defaultBaseDir = defaultBaseDir; |
|
43 |
} |
|
44 |
|
|
45 |
public static String getDefaultBaseDir() |
|
46 |
{ |
|
47 |
return defaultBaseDir; |
|
48 |
} |
|
49 |
|
|
50 |
/** |
|
51 |
* 以默认配置进行文件上传 |
|
52 |
* |
|
53 |
* @param file 上传的文件 |
|
54 |
* @return 文件名称 |
|
55 |
* @throws Exception |
|
56 |
*/ |
|
57 |
public static final String upload(MultipartFile file) throws IOException |
|
58 |
{ |
|
59 |
try |
|
60 |
{ |
|
61 |
return upload(getDefaultBaseDir(), file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION); |
|
62 |
} |
|
63 |
catch (Exception e) |
|
64 |
{ |
|
65 |
throw new IOException(e.getMessage(), e); |
|
66 |
} |
|
67 |
} |
|
68 |
|
|
69 |
/** |
|
70 |
* 根据文件路径上传 |
|
71 |
* |
|
72 |
* @param baseDir 相对应用的基目录 |
|
73 |
* @param file 上传的文件 |
|
74 |
* @return 文件名称 |
|
75 |
* @throws IOException |
|
76 |
*/ |
|
77 |
public static final String upload(String baseDir, MultipartFile file) throws IOException |
|
78 |
{ |
|
79 |
try |
|
80 |
{ |
|
81 |
return upload(baseDir, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION); |
|
82 |
} |
|
83 |
catch (Exception e) |
|
84 |
{ |
|
85 |
throw new IOException(e.getMessage(), e); |
|
86 |
} |
|
87 |
} |
|
88 |
|
|
89 |
/** |
|
90 |
* 文件上传 |
|
91 |
* |
|
92 |
* @param baseDir 相对应用的基目录 |
|
93 |
* @param file 上传的文件 |
|
94 |
* @param allowedExtension 上传文件类型 |
|
95 |
* @return 返回上传成功的文件名 |
|
96 |
* @throws FileSizeLimitExceededException 如果超出最大大小 |
|
97 |
* @throws FileNameLengthLimitExceededException 文件名太长 |
|
98 |
* @throws IOException 比如读写文件出错时 |
|
99 |
* @throws InvalidExtensionException 文件校验异常 |
|
100 |
*/ |
|
101 |
public static final String upload(String baseDir, MultipartFile file, String[] allowedExtension) |
|
102 |
throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException, |
|
103 |
InvalidExtensionException |
|
104 |
{ |
|
105 |
int fileNamelength = Objects.requireNonNull(file.getOriginalFilename()).length(); |
|
106 |
if (fileNamelength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH) |
|
107 |
{ |
|
108 |
throw new FileNameLengthLimitExceededException(FileUploadUtils.DEFAULT_FILE_NAME_LENGTH); |
|
109 |
} |
|
110 |
|
|
111 |
assertAllowed(file, allowedExtension); |
|
112 |
|
|
113 |
String fileName = extractFilename(file); |
|
114 |
|
|
115 |
String absPath = getAbsoluteFile(baseDir, fileName).getAbsolutePath(); |
|
116 |
file.transferTo(Paths.get(absPath)); |
|
117 |
return getPathFileName(baseDir, fileName); |
|
118 |
} |
|
119 |
|
|
120 |
/** |
|
121 |
* 编码文件名 |
|
122 |
*/ |
|
123 |
public static final String extractFilename(MultipartFile file) |
|
124 |
{ |
|
125 |
return StringUtils.format("{}/{}_{}.{}", DateUtils.datePath(), |
|
126 |
FilenameUtils.getBaseName(file.getOriginalFilename()), Seq.getId(Seq.uploadSeqType), getExtension(file)); |
|
127 |
} |
|
128 |
|
|
129 |
public static final File getAbsoluteFile(String uploadDir, String fileName) throws IOException |
|
130 |
{ |
|
131 |
File desc = new File(uploadDir + File.separator + fileName); |
|
132 |
|
|
133 |
if (!desc.exists()) |
|
134 |
{ |
|
135 |
if (!desc.getParentFile().exists()) |
|
136 |
{ |
|
137 |
desc.getParentFile().mkdirs(); |
|
138 |
} |
|
139 |
} |
|
140 |
return desc; |
|
141 |
} |
|
142 |
|
|
143 |
public static final String getPathFileName(String uploadDir, String fileName) throws IOException |
|
144 |
{ |
|
145 |
int dirLastIndex = MesConfig.getProfile().length() + 1; |
|
146 |
String currentDir = StringUtils.substring(uploadDir, dirLastIndex); |
|
147 |
return Constants.RESOURCE_PREFIX + "/" + currentDir + "/" + fileName; |
|
148 |
} |
|
149 |
|
|
150 |
/** |
|
151 |
* 文件大小校验 |
|
152 |
* |
|
153 |
* @param file 上传的文件 |
|
154 |
* @return |
|
155 |
* @throws FileSizeLimitExceededException 如果超出最大大小 |
|
156 |
* @throws InvalidExtensionException |
|
157 |
*/ |
|
158 |
public static final void assertAllowed(MultipartFile file, String[] allowedExtension) |
|
159 |
throws FileSizeLimitExceededException, InvalidExtensionException |
|
160 |
{ |
|
161 |
long size = file.getSize(); |
|
162 |
if (size > DEFAULT_MAX_SIZE) |
|
163 |
{ |
|
164 |
throw new FileSizeLimitExceededException(DEFAULT_MAX_SIZE / 1024 / 1024); |
|
165 |
} |
|
166 |
|
|
167 |
String fileName = file.getOriginalFilename(); |
|
168 |
String extension = getExtension(file); |
|
169 |
if (allowedExtension != null && !isAllowedExtension(extension, allowedExtension)) |
|
170 |
{ |
|
171 |
if (allowedExtension == MimeTypeUtils.IMAGE_EXTENSION) |
|
172 |
{ |
|
173 |
throw new InvalidExtensionException.InvalidImageExtensionException(allowedExtension, extension, |
|
174 |
fileName); |
|
175 |
} |
|
176 |
else if (allowedExtension == MimeTypeUtils.FLASH_EXTENSION) |
|
177 |
{ |
|
178 |
throw new InvalidExtensionException.InvalidFlashExtensionException(allowedExtension, extension, |
|
179 |
fileName); |
|
180 |
} |
|
181 |
else if (allowedExtension == MimeTypeUtils.MEDIA_EXTENSION) |
|
182 |
{ |
|
183 |
throw new InvalidExtensionException.InvalidMediaExtensionException(allowedExtension, extension, |
|
184 |
fileName); |
|
185 |
} |
|
186 |
else if (allowedExtension == MimeTypeUtils.VIDEO_EXTENSION) |
|
187 |
{ |
|
188 |
throw new InvalidExtensionException.InvalidVideoExtensionException(allowedExtension, extension, |
|
189 |
fileName); |
|
190 |
} |
|
191 |
else |
|
192 |
{ |
|
193 |
throw new InvalidExtensionException(allowedExtension, extension, fileName); |
|
194 |
} |
|
195 |
} |
|
196 |
} |
|
197 |
|
|
198 |
/** |
|
199 |
* 判断MIME类型是否是允许的MIME类型 |
|
200 |
* |
|
201 |
* @param extension |
|
202 |
* @param allowedExtension |
|
203 |
* @return |
|
204 |
*/ |
|
205 |
public static final boolean isAllowedExtension(String extension, String[] allowedExtension) |
|
206 |
{ |
|
207 |
for (String str : allowedExtension) |
|
208 |
{ |
|
209 |
if (str.equalsIgnoreCase(extension)) |
|
210 |
{ |
|
211 |
return true; |
|
212 |
} |
|
213 |
} |
|
214 |
return false; |
|
215 |
} |
|
216 |
|
|
217 |
/** |
|
218 |
* 获取文件名的后缀 |
|
219 |
* |
|
220 |
* @param file 表单文件 |
|
221 |
* @return 后缀名 |
|
222 |
*/ |
|
223 |
public static final String getExtension(MultipartFile file) |
|
224 |
{ |
|
225 |
String extension = FilenameUtils.getExtension(file.getOriginalFilename()); |
|
226 |
if (StringUtils.isEmpty(extension)) |
|
227 |
{ |
|
228 |
extension = MimeTypeUtils.getExtension(Objects.requireNonNull(file.getContentType())); |
|
229 |
} |
|
230 |
return extension; |
|
231 |
} |
|
232 |
} |