53 lines
1.5 KiB
Java
53 lines
1.5 KiB
Java
package com.xkrs.straw.utilsold;
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
import java.io.File;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.UUID;
|
|
|
|
public class FileUploadUtils {
|
|
|
|
public static final boolean release = false;
|
|
|
|
private FileUploadUtils() {
|
|
}
|
|
|
|
/**
|
|
* 上传单个文件
|
|
*/
|
|
public static String uploadFile(MultipartFile file) throws Exception {
|
|
String uploadDirPath = "/Users/liuchengqian/Desktop/DaJiang/";
|
|
String originalFilename = file.getOriginalFilename();
|
|
int lastPointIndex = originalFilename.lastIndexOf(".");
|
|
String extensionName = originalFilename.substring(lastPointIndex);
|
|
String newFileName = UUID.randomUUID().toString() + extensionName;
|
|
File uploadDir = new File(uploadDirPath);
|
|
if ((!uploadDir.exists()) || (!uploadDir.isDirectory())) {
|
|
uploadDir.mkdirs();
|
|
}
|
|
String uploadFilePath = uploadDirPath + newFileName;
|
|
//实现上传
|
|
file.transferTo(new File(uploadFilePath));
|
|
|
|
return uploadFilePath;
|
|
}
|
|
|
|
/**
|
|
* 上传多个文件
|
|
*/
|
|
public static List<String> uploadFileArray(MultipartFile[] fileArray) throws Exception {
|
|
List<String> pathList = new ArrayList<>();
|
|
if (fileArray == null || fileArray.length == 0) {
|
|
return pathList;
|
|
}
|
|
for (MultipartFile file : fileArray) {
|
|
if (file != null) {
|
|
pathList.add(uploadFile(file));
|
|
}
|
|
}
|
|
return pathList;
|
|
}
|
|
}
|