|
|
@@ -1,5 +1,6 @@
|
|
|
package com.zd.base.files.file.utils;
|
|
|
|
|
|
+import cn.hutool.core.thread.ThreadUtil;
|
|
|
import com.zd.common.core.exception.ServiceException;
|
|
|
import com.zd.common.core.exception.file.FileNameLengthLimitExceededException;
|
|
|
import com.zd.common.core.exception.file.FileSizeLimitExceededException;
|
|
|
@@ -8,6 +9,7 @@ import com.zd.common.core.utils.DateUtils;
|
|
|
import com.zd.common.core.utils.IdUtils;
|
|
|
import com.zd.common.core.utils.StringUtils;
|
|
|
import com.zd.common.core.utils.file.MimeTypeUtils;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.io.FilenameUtils;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
@@ -19,6 +21,7 @@ import java.io.IOException;
|
|
|
*
|
|
|
* @author zd
|
|
|
*/
|
|
|
+@Slf4j
|
|
|
public class FileUploadUtils {
|
|
|
/**
|
|
|
* 默认大小 50M
|
|
|
@@ -38,9 +41,9 @@ public class FileUploadUtils {
|
|
|
* @return 文件名称
|
|
|
* @throws IOException
|
|
|
*/
|
|
|
- public static final String upload(String baseDir, MultipartFile file) throws IOException {
|
|
|
+ public static final String upload(String baseDir, MultipartFile file, Boolean ifAsy) throws IOException {
|
|
|
try {
|
|
|
- return upload(baseDir, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
|
|
|
+ return upload(baseDir, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION, ifAsy);
|
|
|
} catch (Exception e) {
|
|
|
throw new IOException(e.getMessage(), e);
|
|
|
}
|
|
|
@@ -58,7 +61,7 @@ public class FileUploadUtils {
|
|
|
* @throws IOException 比如读写文件出错时
|
|
|
* @throws InvalidExtensionException 文件校验异常
|
|
|
*/
|
|
|
- public static final String upload(String baseDir, MultipartFile file, String[] allowedExtension)
|
|
|
+ public static final String upload(String baseDir, MultipartFile file, String[] allowedExtension, Boolean ifAsy)
|
|
|
throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException,
|
|
|
InvalidExtensionException {
|
|
|
int fileNamelength = file.getOriginalFilename().length();
|
|
|
@@ -71,9 +74,28 @@ public class FileUploadUtils {
|
|
|
|
|
|
String fileName = extractFilename(file);
|
|
|
|
|
|
- File desc = getAbsoluteFile(baseDir, fileName);
|
|
|
- file.transferTo(desc);
|
|
|
+ if(ifAsy == null || !ifAsy){
|
|
|
+ File desc = getAbsoluteFile(baseDir, fileName);
|
|
|
+ file.transferTo(desc);
|
|
|
+ }else {
|
|
|
+ ThreadUtil.execute(new Runnable() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ try {
|
|
|
+ File desc = getAbsoluteFile(baseDir, fileName);
|
|
|
+ file.transferTo(desc);
|
|
|
+ log.info("文件异步上传成功:" + fileName);
|
|
|
+ log.info("文件成功上传时间:" + DateUtils.getTime());
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ log.error("文件异步上传出现异常:" + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
String pathFileName = getPathFileName(fileName);
|
|
|
+ log.info("文件结束上传时间:" + DateUtils.getTime());
|
|
|
return pathFileName;
|
|
|
}
|
|
|
//
|
|
|
@@ -112,13 +134,10 @@ public class FileUploadUtils {
|
|
|
* 编码文件名
|
|
|
*/
|
|
|
public static final String extractFilename(MultipartFile file) {
|
|
|
- String fileName = file.getOriginalFilename();
|
|
|
- String extension = getExtension(file);
|
|
|
- fileName = DateUtils.datePath() + "/" + IdUtils.fastUUID() + "." + extension;
|
|
|
- return fileName;
|
|
|
+ return DateUtils.datePath() + "/" + IdUtils.fastUUID() + "." + getExtension(file);
|
|
|
}
|
|
|
|
|
|
- private static final File getAbsoluteFile(String uploadDir, String fileName) throws IOException {
|
|
|
+ private static final File getAbsoluteFile(String uploadDir, String fileName){
|
|
|
File desc = new File(uploadDir + File.separator + fileName);
|
|
|
|
|
|
if (!desc.exists()) {
|
|
|
@@ -129,7 +148,7 @@ public class FileUploadUtils {
|
|
|
return desc.isAbsolute() ? desc : desc.getAbsoluteFile();
|
|
|
}
|
|
|
|
|
|
- private static final String getPathFileName(String fileName) throws IOException {
|
|
|
+ private static final String getPathFileName(String fileName){
|
|
|
String pathFileName = "/" + fileName;
|
|
|
return pathFileName;
|
|
|
}
|