|
@@ -77,6 +77,22 @@ public class FileUploadUtils {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 根据文件路径上传,不修改上传路径
|
|
|
+ *
|
|
|
+ * @param baseDir 相对应用的基目录
|
|
|
+ * @param file 上传的文件
|
|
|
+ * @return 文件名称
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public static final String uploadPure(String baseDir, MultipartFile file) throws IOException {
|
|
|
+ try {
|
|
|
+ return uploadPure(baseDir, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new IOException(e.getMessage(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 文件上传
|
|
|
*
|
|
@@ -107,6 +123,37 @@ public class FileUploadUtils {
|
|
|
return getPathFileName(baseDir, fileName);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 文件上传,不编码文件目录
|
|
|
+ *
|
|
|
+ * @param baseDir 相对应用的基目录
|
|
|
+ * @param file 上传的文件
|
|
|
+ * @param allowedExtension 上传文件类型
|
|
|
+ * @return 返回上传成功的文件名
|
|
|
+ * @throws FileSizeLimitExceededException 如果超出最大大小
|
|
|
+ * @throws FileNameLengthLimitExceededException 文件名太长
|
|
|
+ * @throws IOException 比如读写文件出错时
|
|
|
+ * @throws InvalidExtensionException 文件校验异常
|
|
|
+ */
|
|
|
+ public static final String uploadPure(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 = file.getOriginalFilename();
|
|
|
+ System.out.println("filename is: " + fileName);
|
|
|
+
|
|
|
+ String absPath = getAbsoluteFile(baseDir, fileName).getAbsolutePath();
|
|
|
+ file.transferTo(Paths.get(absPath));
|
|
|
+
|
|
|
+ return getPathFileName(baseDir, fileName);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 编码文件名
|
|
|
*/
|
|
@@ -114,6 +161,15 @@ public class FileUploadUtils {
|
|
|
return StringUtils.format("{}/{}_{}.{}", DateUtils.datePath(),
|
|
|
FilenameUtils.getBaseName(file.getOriginalFilename()), Seq.getId(Seq.uploadSeqType), getExtension(file));
|
|
|
}
|
|
|
+
|
|
|
+ ///**
|
|
|
+ // * 编码文件名,但不添加后缀和日期
|
|
|
+ // */
|
|
|
+ //public static final String extractFilenamePure(MultipartFile file) {
|
|
|
+ // 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("{}/{}.{}", DateUtils.allDatePath(),
|
|
|
FilenameUtils.getBaseName(file.getOriginalFilename()), getExtension(file));
|