package com.xuanzi.utils;
import jakarta.servlet.http.HttpSession;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MultipartFileUtil {
private String serverUploadPath;
private boolean serverUploadPathExists;
private String originalFileName;
private String originalFileSuffix;
private long originalFileSize;
private double maxRequire = 10485760;
private double minRequire = 10485.76;
private String uploadFileName;
private String serverFinalFileUploadPath;
private boolean isUploadFile;
private String errorMessage;
private boolean multipartFileIsEmpty(MultipartFile multipartFile) {
if (!multipartFile.isEmpty()) {
this.originalFileSize = multipartFile.getSize();
this.originalFileName = multipartFile.getOriginalFilename();
this.originalFileSuffix = FilenameUtils.getExtension(this.originalFileName);
System.out.println("单文件上传:源文件非空校验通过");
return isUploadFile = true;
} else {
System.out.println("单文件上传:源文件非空校验异常");
return isUploadFile = false;
}
}
private boolean multipartFilesIsEmpty(MultipartFile[] multipartFiles) {
if (multipartFiles != null) {
System.out.println("多文件上传:源文件非空校验通过");
return isUploadFile = true;
} else {
System.out.println("多文件上传:源文件非空校验异常");
return isUploadFile = false;
}
}
private boolean serverUploadPathExists(String serverUploadPath) {
this.serverUploadPath = serverUploadPath;
File uploadPath = new File(this.serverUploadPath);
if (uploadPath.exists()) {
this.serverUploadPathExists = true;
System.out.println("服务器上传路径校验通过");
return isUploadFile = true;
} else {
this.serverUploadPathExists = uploadPath.mkdirs();
if (!this.serverUploadPathExists) {
System.out.println("服务器上传路径校验异常");
return isUploadFile = false;
} else {
System.out.println("服务器上传路径校验通过");
return isUploadFile = false;
}
}
}
private boolean originalFileSizeRequire(double minRequire, double maxRequire) {
this.minRequire = minRequire;
this.maxRequire = maxRequire;
if ((this.originalFileSize >= this.minRequire) && (this.originalFileSize <= this.maxRequire)) {
System.out.println("上传文件大小校验通过");
return isUploadFile = true;
} else {
System.out.println("上传文件大小校验异常:上传文件字节:" + this.originalFileSize + ";限制最小字节:" + this.minRequire + ";限制最大字节:" + this.maxRequire);
return isUploadFile = false;
}
}
private boolean suffixRequires(List<String> suffixRequires) {
for (String suffixRequire : suffixRequires) {
if (this.originalFileSuffix.equalsIgnoreCase(suffixRequire)) {
System.out.println("上传文件后缀校验通过");
return isUploadFile = true;
}
}
System.out.println("上传文件后缀校验异常:上传文件后缀:" + this.originalFileSuffix + ";限制文件后缀:" + suffixRequires);
return isUploadFile = false;
}
private MultipartFileUtil UploadFileError(String errorMessage) {
this.errorMessage = errorMessage;
System.out.println("文件上传失败:" + this.isUploadFile);
System.out.println("========================");
return this;
}
public MultipartFileUtil upLoadMultipartFile(MultipartFile multipartFile, String serverUploadPath, List<String> suffixRequires, double minRequire, double maxRequire) throws IOException {
if (multipartFileIsEmpty(multipartFile)) {
if (serverUploadPathExists(serverUploadPath)) {
if (originalFileSizeRequire(minRequire, maxRequire)) {
if (suffixRequires(suffixRequires)) {
this.uploadFileName = System.currentTimeMillis() + "-" + this.originalFileName;
System.out.println("处理后上传文件名称:" + this.uploadFileName);
this.serverFinalFileUploadPath = this.serverUploadPath + this.uploadFileName;
System.out.println("服务器最终文件上传文件路径:" + this.serverFinalFileUploadPath);
multipartFile.transferTo(new File(this.serverFinalFileUploadPath));
System.out.println("文件上传成功:" + isUploadFile);
System.out.println("========================");
return this;
} else {
return UploadFileError("上传文件后缀校验异常:上传文件后缀:" + this.originalFileSuffix + ";限制文件后缀:" + suffixRequires);
}
} else {
return UploadFileError("上传文件大小校验异常:上传文件字节:" + this.originalFileSize + ";限制最小字节:" + this.minRequire + ";限制最大字节:" + this.maxRequire);
}
} else {
return UploadFileError("服务器上传路径校验异常");
}
} else {
return UploadFileError("单文件上传:源文件非空校验异常");
}
}
public MultipartFileUtil upLoadMultipartFileNoSize(MultipartFile multipartFile, String serverUploadPath, List<String> suffixRequires) throws IOException {
return upLoadMultipartFile(multipartFile, serverUploadPath, suffixRequires, this.minRequire, this.maxRequire);
}
public MultipartFileUtil upLoadMultipartFileImg(MultipartFile multipartFile, String serverUploadPath) throws IOException {
return upLoadMultipartFile(multipartFile, serverUploadPath, this.getImgSuffixRequires(), this.minRequire, this.maxRequire);
}
public MultipartFileUtil upLoadMultipartFileWord(MultipartFile multipartFile, String serverUploadPath) throws IOException {
return upLoadMultipartFile(multipartFile, serverUploadPath, this.getWordSuffixRequires(), this.minRequire, this.maxRequire);
}
public List<MultipartFileUtil> upLoadMultipartFiles(MultipartFile[] multipartFiles, String serverUploadPath, List<String> suffixRequires, double minRequire, double maxRequire) throws IOException {
List<MultipartFileUtil> multipartFileUtil2List = new ArrayList<>();
if (multipartFilesIsEmpty(multipartFiles)) {
for (MultipartFile multipartFile : multipartFiles) {
MultipartFileUtil multipartFileUtil = new MultipartFileUtil().upLoadMultipartFile(multipartFile, serverUploadPath, suffixRequires, minRequire, maxRequire);
multipartFileUtil2List.add(multipartFileUtil);
}
System.out.println("多文件上传:执行完毕");
} else {
System.out.println("多文件上传异常:上传文件校验为空");
this.errorMessage = "多文件上传异常:上传文件校验为空";
}
return multipartFileUtil2List;
}
public List<MultipartFileUtil> upLoadMultipartFilesNoSizes(MultipartFile[] multipartFiles, String serverUploadPath, List<String> suffixRequires) throws IOException {
List<MultipartFileUtil> multipartFileUtil2List = new ArrayList<>();
if (multipartFilesIsEmpty(multipartFiles)) {
for (MultipartFile multipartFile : multipartFiles) {
MultipartFileUtil multipartFileUtil = new MultipartFileUtil().upLoadMultipartFileNoSize(multipartFile, serverUploadPath, suffixRequires);
multipartFileUtil2List.add(multipartFileUtil);
}
System.out.println("多文件上传:执行完毕");
} else {
System.out.println("多文件上传异常:上传文件校验为空");
this.errorMessage = "多文件上传异常:上传文件校验为空";
}
return multipartFileUtil2List;
}
public List<MultipartFileUtil> upLoadMultipartFilesImgs(MultipartFile[] multipartFiles, String serverUploadPath) throws IOException {
List<MultipartFileUtil> multipartFileUtil2List = new ArrayList<>();
if (multipartFilesIsEmpty(multipartFiles)) {
for (MultipartFile multipartFile : multipartFiles) {
MultipartFileUtil multipartFileUtil = new MultipartFileUtil().upLoadMultipartFileImg(multipartFile, serverUploadPath);
multipartFileUtil2List.add(multipartFileUtil);
}
System.out.println("多文件上传:执行完毕");
} else {
System.out.println("多文件上传异常:上传文件校验为空");
this.errorMessage = "多文件上传异常:上传文件校验为空";
}
return multipartFileUtil2List;
}
public List<MultipartFileUtil> upLoadMultipartFilesWords(MultipartFile[] multipartFiles, String serverUploadPath) throws IOException {
List<MultipartFileUtil> multipartFileUtil2List = new ArrayList<>();
if (multipartFilesIsEmpty(multipartFiles)) {
for (MultipartFile multipartFile : multipartFiles) {
MultipartFileUtil multipartFileUtil = new MultipartFileUtil().upLoadMultipartFileWord(multipartFile, serverUploadPath);
multipartFileUtil2List.add(multipartFileUtil);
}
System.out.println("多文件上传:执行完毕");
} else {
System.out.println("多文件上传异常:上传文件校验为空");
this.errorMessage = "多文件上传异常:上传文件校验为空";
}
return multipartFileUtil2List;
}
public String getProjectRealPath(HttpSession session, String catalogue) {
return session.getServletContext().getRealPath(catalogue);
}
public String getProjectContextPath(HttpSession session, String catalogue) {
return session.getServletContext().getContextPath() + catalogue;
}
public long getMBToBytes(double megabytes) {
if (megabytes < 0) {
throw new IllegalArgumentException("兆字节数不能为负数");
}
return (long) (megabytes * 1024 * 1024);
}
public double getBytesToMB(long bytes) {
if (bytes < 0) {
throw new IllegalArgumentException("字节数不能为负数");
}
return (double) bytes / (1024 * 1024);
}
public List<String> getImgSuffixRequires() {
return new ArrayList<>(Arrays.asList("jpg", "jpeg", "png", "gif", "bmp", "tif", "tiff", "ico"));
}
public List<String> getWordSuffixRequires() {
return new ArrayList<>(Arrays.asList("pdf", "doc", "docx", "ppt", "pptx", "xls", "xlsx", "txt"));
}
public String getServerUploadPath() {
return serverUploadPath;
}
public boolean isServerUploadPathExists() {
return serverUploadPathExists;
}
public String getOriginalFileName() {
return originalFileName;
}
public String getOriginalFileSuffix() {
return originalFileSuffix;
}
public long getOriginalFileSize() {
return originalFileSize;
}
public double getMaxRequire() {
return maxRequire;
}
public double getMinRequire() {
return minRequire;
}
public String getUploadFileName() {
return uploadFileName;
}
public String getServerFinalFileUploadPath() {
return serverFinalFileUploadPath;
}
public boolean isUploadFile() {
return isUploadFile;
}
public String getErrorMessage() {
return errorMessage;
}
@Override
public String toString() {
return "MultipartFileUtil{" +
"serverUploadPath='" + serverUploadPath + '\'' +
", serverUploadPathExists=" + serverUploadPathExists +
", originalFileName='" + originalFileName + '\'' +
", originalFileSuffix='" + originalFileSuffix + '\'' +
", originalFileSize=" + originalFileSize +
", maxRequire=" + maxRequire +
", minRequire=" + minRequire +
", uploadFileName='" + uploadFileName + '\'' +
", serverFinalFileUploadPath='" + serverFinalFileUploadPath + '\'' +
", isUploadFile=" + isUploadFile +
", errorMessage='" + errorMessage + '\'' +
'}';
}
}

- 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
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
- 315
- 316
- 317
- 318
- 319
- 320
- 321
- 322
- 323
- 324
- 325
- 326
- 327
- 328
- 329
- 330
- 331
- 332
- 333
- 334
- 335
- 336
- 337