|
|
@@ -1,6 +1,7 @@
|
|
|
package com.xunmei.file.utils;
|
|
|
|
|
|
import cn.hutool.core.codec.Base64;
|
|
|
+import cn.hutool.core.io.FileTypeUtil;
|
|
|
import cn.hutool.core.io.FileUtil;
|
|
|
import com.xunmei.common.core.exception.file.FileException;
|
|
|
import com.xunmei.common.core.exception.file.FileNameLengthLimitExceededException;
|
|
|
@@ -18,8 +19,7 @@ import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
-import java.io.File;
|
|
|
-import java.io.IOException;
|
|
|
+import java.io.*;
|
|
|
import java.nio.file.Paths;
|
|
|
import java.util.Objects;
|
|
|
|
|
|
@@ -110,8 +110,13 @@ public class FileUploadUtils
|
|
|
String fileName = extractFilename(file);
|
|
|
|
|
|
String absPath = getAbsoluteFile(baseDir, fileName).getAbsolutePath();
|
|
|
-
|
|
|
- file.transferTo(Paths.get(absPath));
|
|
|
+ String type = FileTypeUtil.getType(file.getInputStream());
|
|
|
+ File target = FileUtils.getFile(absPath);
|
|
|
+ file.transferTo(target);
|
|
|
+ if(!isAllowedExtension(type,allowedExtension)){
|
|
|
+ FileUtil.del(target);
|
|
|
+ throw new RuntimeException("上传文件类型不允许");
|
|
|
+ }
|
|
|
return getPathFileName(fileName);
|
|
|
}
|
|
|
|
|
|
@@ -120,6 +125,11 @@ public class FileUploadUtils
|
|
|
InvalidExtensionException{
|
|
|
|
|
|
byte[] decode = Base64.decode(file.getContent());
|
|
|
+ InputStream inputStream = new ByteArrayInputStream(decode);
|
|
|
+ String type = FileTypeUtil.getType(inputStream);
|
|
|
+ if(!isAllowedExtension(type,MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION.toArray(new String[0]))){
|
|
|
+ throw new RuntimeException("上传文件类型不允许");
|
|
|
+ }
|
|
|
String extension = file.getExtension();
|
|
|
if(StringUtils.isEmpty(extension) || "null".equals(extension))
|
|
|
{
|