|
@@ -39,6 +39,7 @@ public class FileUploadUtils
|
|
|
|
|
|
public static void setDefaultBaseDir(String defaultBaseDir)
|
|
|
{
|
|
|
+
|
|
|
FileUploadUtils.defaultBaseDir = defaultBaseDir;
|
|
|
}
|
|
|
|
|
@@ -87,6 +88,26 @@ public class FileUploadUtils
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 根据文件路径上传
|
|
|
+ *
|
|
|
+ * @param baseDir 相对应用的基目录
|
|
|
+ * @param file 上传的文件
|
|
|
+ * @return 文件名称
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public static final String upload2(String baseDir, MultipartFile file) throws IOException
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ return upload2(baseDir, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
|
|
|
+ }
|
|
|
+ catch (Exception e)
|
|
|
+ {
|
|
|
+ throw new IOException(e.getMessage(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 文件上传
|
|
|
*
|
|
|
* @param baseDir 相对应用的基目录
|
|
@@ -118,6 +139,37 @@ public class FileUploadUtils
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 文件上传
|
|
|
+ *
|
|
|
+ * @param baseDir 相对应用的基目录
|
|
|
+ * @param file 上传的文件
|
|
|
+ * @param allowedExtension 上传文件类型
|
|
|
+ * @return 返回上传成功的文件名
|
|
|
+ * @throws FileSizeLimitExceededException 如果超出最大大小
|
|
|
+ * @throws FileNameLengthLimitExceededException 文件名太长
|
|
|
+ * @throws IOException 比如读写文件出错时
|
|
|
+ * @throws InvalidExtensionException 文件校验异常
|
|
|
+ */
|
|
|
+ public static final String upload2(String baseDir, MultipartFile file, String[] allowedExtension)
|
|
|
+ throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException,
|
|
|
+ InvalidExtensionException
|
|
|
+ {
|
|
|
+ int fileNamelength = Objects.requireNonNull(file.getOriginalFilename()).length();
|
|
|
+ if (fileNamelength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH)
|
|
|
+ {
|
|
|
+ throw new FileNameLengthLimitExceededException(FileUploadUtils.DEFAULT_FILE_NAME_LENGTH);
|
|
|
+ }
|
|
|
+
|
|
|
+ assertAllowed(file, allowedExtension);
|
|
|
+
|
|
|
+ String fileName = extractFilename2(file);
|
|
|
+
|
|
|
+ String absPath = getAbsoluteFile2(baseDir, fileName).getAbsolutePath();
|
|
|
+ file.transferTo(Paths.get(absPath));
|
|
|
+ return getPathFileName2(baseDir, fileName);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 编码文件名
|
|
|
*/
|
|
|
public static final String extractFilename(MultipartFile file)
|
|
@@ -125,11 +177,33 @@ public class FileUploadUtils
|
|
|
return StringUtils.format("{}/{}_{}.{}", DateUtils.datePath(),
|
|
|
FilenameUtils.getBaseName(file.getOriginalFilename()), Seq.getId(Seq.uploadSeqType), getExtension(file));
|
|
|
}
|
|
|
+ /**
|
|
|
+ * 编码文件名
|
|
|
+ */
|
|
|
+ public static final String extractFilename2(MultipartFile file)
|
|
|
+ {
|
|
|
+ return StringUtils.format("{}/{}_{}.{}", StringUtils.substringBefore(file.getOriginalFilename(),"_"),
|
|
|
+ StringUtils.substringBefore(StringUtils.substringAfter(file.getOriginalFilename(),"_"),"."), Seq.getId(Seq.uploadSeqType).substring(14,17), getExtension(file));
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
public static final File getAbsoluteFile(String uploadDir, String fileName) throws IOException
|
|
|
{
|
|
|
File desc = new File(uploadDir + File.separator + fileName);
|
|
|
+ if (!desc.exists())
|
|
|
+ {
|
|
|
+ if (!desc.getParentFile().exists())
|
|
|
+ {
|
|
|
+ desc.getParentFile().mkdirs();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return desc;
|
|
|
+ }
|
|
|
|
|
|
+ public static final File getAbsoluteFile2(String uploadDir, String fileName) throws IOException
|
|
|
+ {
|
|
|
+ //File desc = new File(uploadDir + File.separator + fileName);
|
|
|
+ File desc = new File(uploadDir + "/" + fileName);
|
|
|
if (!desc.exists())
|
|
|
{
|
|
|
if (!desc.getParentFile().exists())
|
|
@@ -144,9 +218,22 @@ public class FileUploadUtils
|
|
|
{
|
|
|
int dirLastIndex = RuoYiConfig.getProfile().length() + 1;
|
|
|
String currentDir = StringUtils.substring(uploadDir, dirLastIndex);
|
|
|
- return Constants.RESOURCE_PREFIX + "/" + currentDir + "/" + fileName;
|
|
|
+ return Constants.RESOURCE_PREFIX +
|
|
|
+ "/" + currentDir +
|
|
|
+ "/" + fileName;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static final String getPathFileName2(String uploadDir, String fileName) throws IOException
|
|
|
+ {
|
|
|
+ int dirLastIndex = RuoYiConfig.getUploadPath2().length() + 1;
|
|
|
+ String currentDir = StringUtils.substring(uploadDir, dirLastIndex);
|
|
|
+ return Constants.RESOURCE_PREFIX +
|
|
|
+ //"/" + currentDir +
|
|
|
+ "/" + fileName;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* 文件大小校验
|
|
|
*
|