diff --git a/ruoyi-admin/src/main/resources/application.yml b/ruoyi-admin/src/main/resources/application.yml index f39fb29fa..288e8fdf4 100644 --- a/ruoyi-admin/src/main/resources/application.yml +++ b/ruoyi-admin/src/main/resources/application.yml @@ -8,8 +8,8 @@ ruoyi: copyrightYear: 2019 # 实例演示开关 demoEnabled: true - # 文件路径 - profile: D:/profile/ + # 文件路径 示例( Windows配置D:/ruoyi/uploadPath,Linux配置 /home/ruoyi/uploadPath) + profile: D:/ruoyi/uploadPath # 获取ip地址开关 addressEnabled: true diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/config/Global.java b/ruoyi-common/src/main/java/com/ruoyi/common/config/Global.java index 3b0308c08..cd1c1d54a 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/config/Global.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/config/Global.java @@ -121,7 +121,7 @@ public class Global */ public static String getAvatarPath() { - return getConfig("ruoyi.profile") + "avatar/"; + return getProfile() + "/avatar"; } /** @@ -129,7 +129,7 @@ public class Global */ public static String getDownloadPath() { - return getConfig("ruoyi.profile") + "download/"; + return getProfile() + "/download"; } /** @@ -137,6 +137,6 @@ public class Global */ public static String getUploadPath() { - return getConfig("ruoyi.profile") + "upload/"; + return getProfile() + "/upload"; } } diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/file/FileUploadUtils.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/file/FileUploadUtils.java index cf4de4fbc..6190a566b 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/file/FileUploadUtils.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/file/FileUploadUtils.java @@ -113,12 +113,7 @@ public class FileUploadUtils File desc = getAbsoluteFile(baseDir, fileName); file.transferTo(desc); - String pathFileName = baseDir + fileName; - if (StringUtils.contains(baseDir, ":")) - { - // windows 去除盘符 - pathFileName = StringUtils.substringAfterLast(baseDir, ":") + fileName; - } + String pathFileName = getPathFileName(baseDir, fileName); return pathFileName; } @@ -127,15 +122,15 @@ public class FileUploadUtils */ public static final String extractFilename(MultipartFile file) { - String filename = file.getOriginalFilename(); + String fileName = file.getOriginalFilename(); String extension = getExtension(file); - filename = DateUtils.datePath() + "/" + encodingFilename(filename) + "." + extension; - return filename; + fileName = DateUtils.datePath() + "/" + encodingFilename(fileName) + "." + extension; + return fileName; } - private static final File getAbsoluteFile(String uploadDir, String filename) throws IOException + private static final File getAbsoluteFile(String uploadDir, String fileName) throws IOException { - File desc = new File(uploadDir + File.separator + filename); + File desc = new File(uploadDir + File.separator + fileName); if (!desc.getParentFile().exists()) { @@ -148,14 +143,22 @@ public class FileUploadUtils return desc; } + private static final String getPathFileName(String uploadDir, String fileName) throws IOException + { + int dirLastIndex = uploadDir.lastIndexOf("/") + 1; + String currentDir = StringUtils.substring(uploadDir, dirLastIndex); + String pathFileName = "/profile/" + currentDir + "/" + fileName; + return pathFileName; + } + /** * 编码文件名 */ - private static final String encodingFilename(String filename) + private static final String encodingFilename(String fileName) { - filename = filename.replace("_", " "); - filename = Md5Utils.hash(filename + System.nanoTime() + counter++); - return filename; + fileName = fileName.replace("_", " "); + fileName = Md5Utils.hash(fileName + System.nanoTime() + counter++); + return fileName; } /** @@ -175,28 +178,28 @@ public class FileUploadUtils throw new FileSizeLimitExceededException(DEFAULT_MAX_SIZE / 1024 / 1024); } - String filename = file.getOriginalFilename(); + String fileName = file.getOriginalFilename(); String extension = getExtension(file); if (allowedExtension != null && !isAllowedExtension(extension, allowedExtension)) { if (allowedExtension == MimeTypeUtils.IMAGE_EXTENSION) { throw new InvalidExtensionException.InvalidImageExtensionException(allowedExtension, extension, - filename); + fileName); } else if (allowedExtension == MimeTypeUtils.FLASH_EXTENSION) { throw new InvalidExtensionException.InvalidFlashExtensionException(allowedExtension, extension, - filename); + fileName); } else if (allowedExtension == MimeTypeUtils.MEDIA_EXTENSION) { throw new InvalidExtensionException.InvalidMediaExtensionException(allowedExtension, extension, - filename); + fileName); } else { - throw new InvalidExtensionException(allowedExtension, extension, filename); + throw new InvalidExtensionException(allowedExtension, extension, fileName); } } diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/ResourcesConfig.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/ResourcesConfig.java index 57d139159..2428f87bf 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/ResourcesConfig.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/ResourcesConfig.java @@ -33,8 +33,8 @@ public class ResourcesConfig implements WebMvcConfigurer @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { - /** 文件上传路径 */ - registry.addResourceHandler("/profile/**").addResourceLocations("file:" + Global.getProfile()); + /** 本地文件上传路径 */ + registry.addResourceHandler("/profile/**").addResourceLocations("file:" + Global.getProfile() + "/"); /** swagger配置 */ registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");