28 changed files with 1261 additions and 221 deletions
@ -0,0 +1,29 @@
@@ -0,0 +1,29 @@
|
||||
package com.ruoyi.system.api; |
||||
|
||||
import org.springframework.cloud.openfeign.FeignClient; |
||||
import org.springframework.http.MediaType; |
||||
import org.springframework.web.bind.annotation.PostMapping; |
||||
import org.springframework.web.bind.annotation.RequestPart; |
||||
import org.springframework.web.multipart.MultipartFile; |
||||
import com.ruoyi.common.core.constant.ServiceNameConstants; |
||||
import com.ruoyi.common.core.domain.R; |
||||
import com.ruoyi.system.api.domain.SysFile; |
||||
import com.ruoyi.system.api.factory.RemoteFileFallbackFactory; |
||||
|
||||
/** |
||||
* 文件服务 |
||||
* |
||||
* @author ruoyi |
||||
*/ |
||||
@FeignClient(contextId = "remoteFileService", value = ServiceNameConstants.FILE_SERVICE, fallbackFactory = RemoteFileFallbackFactory.class) |
||||
public interface RemoteFileService |
||||
{ |
||||
/** |
||||
* 上传文件 |
||||
* |
||||
* @param file 文件信息 |
||||
* @return 结果 |
||||
*/ |
||||
@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) |
||||
public R<SysFile> upload(@RequestPart(value = "file") MultipartFile file); |
||||
} |
@ -0,0 +1,50 @@
@@ -0,0 +1,50 @@
|
||||
package com.ruoyi.system.api.domain; |
||||
|
||||
import org.apache.commons.lang3.builder.ToStringBuilder; |
||||
import org.apache.commons.lang3.builder.ToStringStyle; |
||||
|
||||
/** |
||||
* 文件信息 |
||||
* |
||||
* @author ruoyi |
||||
*/ |
||||
public class SysFile |
||||
{ |
||||
/** |
||||
* 文件名称 |
||||
*/ |
||||
private String name; |
||||
|
||||
/** |
||||
* 文件地址 |
||||
*/ |
||||
private String url; |
||||
|
||||
public String getName() |
||||
{ |
||||
return name; |
||||
} |
||||
|
||||
public void setName(String name) |
||||
{ |
||||
this.name = name; |
||||
} |
||||
|
||||
public String getUrl() |
||||
{ |
||||
return url; |
||||
} |
||||
|
||||
public void setUrl(String url) |
||||
{ |
||||
this.url = url; |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE) |
||||
.append("name", getName()) |
||||
.append("url", getUrl()) |
||||
.toString(); |
||||
} |
||||
} |
@ -0,0 +1,35 @@
@@ -0,0 +1,35 @@
|
||||
package com.ruoyi.system.api.factory; |
||||
|
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
import org.springframework.stereotype.Component; |
||||
import org.springframework.web.multipart.MultipartFile; |
||||
import com.ruoyi.common.core.domain.R; |
||||
import com.ruoyi.system.api.RemoteFileService; |
||||
import com.ruoyi.system.api.domain.SysFile; |
||||
import feign.hystrix.FallbackFactory; |
||||
|
||||
/** |
||||
* 文件服务降级处理 |
||||
* |
||||
* @author ruoyi |
||||
*/ |
||||
@Component |
||||
public class RemoteFileFallbackFactory implements FallbackFactory<RemoteFileService> |
||||
{ |
||||
private static final Logger log = LoggerFactory.getLogger(RemoteFileFallbackFactory.class); |
||||
|
||||
@Override |
||||
public RemoteFileService create(Throwable throwable) |
||||
{ |
||||
log.error("文件服务调用失败:{}", throwable.getMessage()); |
||||
return new RemoteFileService() |
||||
{ |
||||
@Override |
||||
public R<SysFile> upload(MultipartFile file) |
||||
{ |
||||
return R.fail("上传文件失败:" + throwable.getMessage()); |
||||
} |
||||
}; |
||||
} |
||||
} |
@ -0,0 +1,19 @@
@@ -0,0 +1,19 @@
|
||||
package com.ruoyi.common.core.exception.file; |
||||
|
||||
import com.ruoyi.common.core.exception.BaseException; |
||||
|
||||
/** |
||||
* 文件信息异常类 |
||||
* |
||||
* @author ruoyi |
||||
*/ |
||||
public class FileException extends BaseException |
||||
{ |
||||
private static final long serialVersionUID = 1L; |
||||
|
||||
public FileException(String code, Object[] args) |
||||
{ |
||||
super("file", code, args, null); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,16 @@
@@ -0,0 +1,16 @@
|
||||
package com.ruoyi.common.core.exception.file; |
||||
|
||||
/** |
||||
* 文件名称超长限制异常类 |
||||
* |
||||
* @author ruoyi |
||||
*/ |
||||
public class FileNameLengthLimitExceededException extends FileException |
||||
{ |
||||
private static final long serialVersionUID = 1L; |
||||
|
||||
public FileNameLengthLimitExceededException(int defaultFileNameLength) |
||||
{ |
||||
super("upload.filename.exceed.length", new Object[] { defaultFileNameLength }); |
||||
} |
||||
} |
@ -0,0 +1,16 @@
@@ -0,0 +1,16 @@
|
||||
package com.ruoyi.common.core.exception.file; |
||||
|
||||
/** |
||||
* 文件名大小限制异常类 |
||||
* |
||||
* @author ruoyi |
||||
*/ |
||||
public class FileSizeLimitExceededException extends FileException |
||||
{ |
||||
private static final long serialVersionUID = 1L; |
||||
|
||||
public FileSizeLimitExceededException(long defaultMaxSize) |
||||
{ |
||||
super("upload.exceed.maxSize", new Object[] { defaultMaxSize }); |
||||
} |
||||
} |
@ -0,0 +1,81 @@
@@ -0,0 +1,81 @@
|
||||
package com.ruoyi.common.core.exception.file; |
||||
|
||||
import java.util.Arrays; |
||||
import org.apache.commons.fileupload.FileUploadException; |
||||
|
||||
/** |
||||
* 文件上传 误异常类 |
||||
* |
||||
* @author ruoyi |
||||
*/ |
||||
public class InvalidExtensionException extends FileUploadException |
||||
{ |
||||
private static final long serialVersionUID = 1L; |
||||
|
||||
private String[] allowedExtension; |
||||
private String extension; |
||||
private String filename; |
||||
|
||||
public InvalidExtensionException(String[] allowedExtension, String extension, String filename) |
||||
{ |
||||
super("filename : [" + filename + "], extension : [" + extension + "], allowed extension : [" + Arrays.toString(allowedExtension) + "]"); |
||||
this.allowedExtension = allowedExtension; |
||||
this.extension = extension; |
||||
this.filename = filename; |
||||
} |
||||
|
||||
public String[] getAllowedExtension() |
||||
{ |
||||
return allowedExtension; |
||||
} |
||||
|
||||
public String getExtension() |
||||
{ |
||||
return extension; |
||||
} |
||||
|
||||
public String getFilename() |
||||
{ |
||||
return filename; |
||||
} |
||||
|
||||
public static class InvalidImageExtensionException extends InvalidExtensionException |
||||
{ |
||||
private static final long serialVersionUID = 1L; |
||||
|
||||
public InvalidImageExtensionException(String[] allowedExtension, String extension, String filename) |
||||
{ |
||||
super(allowedExtension, extension, filename); |
||||
} |
||||
} |
||||
|
||||
public static class InvalidFlashExtensionException extends InvalidExtensionException |
||||
{ |
||||
private static final long serialVersionUID = 1L; |
||||
|
||||
public InvalidFlashExtensionException(String[] allowedExtension, String extension, String filename) |
||||
{ |
||||
super(allowedExtension, extension, filename); |
||||
} |
||||
} |
||||
|
||||
public static class InvalidMediaExtensionException extends InvalidExtensionException |
||||
{ |
||||
private static final long serialVersionUID = 1L; |
||||
|
||||
public InvalidMediaExtensionException(String[] allowedExtension, String extension, String filename) |
||||
{ |
||||
super(allowedExtension, extension, filename); |
||||
} |
||||
} |
||||
|
||||
public static class InvalidVideoExtensionException extends InvalidExtensionException |
||||
{ |
||||
private static final long serialVersionUID = 1L; |
||||
|
||||
public InvalidVideoExtensionException(String[] allowedExtension, String extension, String filename) |
||||
{ |
||||
super(allowedExtension, extension, filename); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,47 @@
@@ -0,0 +1,47 @@
|
||||
package com.ruoyi.common.core.utils.file; |
||||
|
||||
import java.io.File; |
||||
import org.apache.commons.lang3.StringUtils; |
||||
|
||||
/** |
||||
* 文件类型工具类 |
||||
* |
||||
* @author ruoyi |
||||
*/ |
||||
public class FileTypeUtils |
||||
{ |
||||
/** |
||||
* 获取文件类型 |
||||
* <p> |
||||
* 例如: ruoyi.txt, 返回: txt |
||||
* |
||||
* @param file 文件名 |
||||
* @return 后缀(不含".") |
||||
*/ |
||||
public static String getFileType(File file) |
||||
{ |
||||
if (null == file) |
||||
{ |
||||
return StringUtils.EMPTY; |
||||
} |
||||
return getFileType(file.getName()); |
||||
} |
||||
|
||||
/** |
||||
* 获取文件类型 |
||||
* <p> |
||||
* 例如: ruoyi.txt, 返回: txt |
||||
* |
||||
* @param fileName 文件名 |
||||
* @return 后缀(不含".") |
||||
*/ |
||||
public static String getFileType(String fileName) |
||||
{ |
||||
int separatorIndex = fileName.lastIndexOf("."); |
||||
if (separatorIndex < 0) |
||||
{ |
||||
return ""; |
||||
} |
||||
return fileName.substring(separatorIndex + 1).toLowerCase(); |
||||
} |
||||
} |
@ -0,0 +1,59 @@
@@ -0,0 +1,59 @@
|
||||
package com.ruoyi.common.core.utils.file; |
||||
|
||||
/** |
||||
* 媒体类型工具类 |
||||
* |
||||
* @author ruoyi |
||||
*/ |
||||
public class MimeTypeUtils |
||||
{ |
||||
public static final String IMAGE_PNG = "image/png"; |
||||
|
||||
public static final String IMAGE_JPG = "image/jpg"; |
||||
|
||||
public static final String IMAGE_JPEG = "image/jpeg"; |
||||
|
||||
public static final String IMAGE_BMP = "image/bmp"; |
||||
|
||||
public static final String IMAGE_GIF = "image/gif"; |
||||
|
||||
public static final String[] IMAGE_EXTENSION = { "bmp", "gif", "jpg", "jpeg", "png" }; |
||||
|
||||
public static final String[] FLASH_EXTENSION = { "swf", "flv" }; |
||||
|
||||
public static final String[] MEDIA_EXTENSION = { "swf", "flv", "mp3", "wav", "wma", "wmv", "mid", "avi", "mpg", |
||||
"asf", "rm", "rmvb" }; |
||||
|
||||
public static final String[] VIDEO_EXTENSION = { "mp4", "avi", "rmvb" }; |
||||
|
||||
public static final String[] DEFAULT_ALLOWED_EXTENSION = { |
||||
// 图片
|
||||
"bmp", "gif", "jpg", "jpeg", "png", |
||||
// word excel powerpoint
|
||||
"doc", "docx", "xls", "xlsx", "ppt", "pptx", "html", "htm", "txt", |
||||
// 压缩文件
|
||||
"rar", "zip", "gz", "bz2", |
||||
// 视频格式
|
||||
"mp4", "avi", "rmvb", |
||||
// pdf
|
||||
"pdf" }; |
||||
|
||||
public static String getExtension(String prefix) |
||||
{ |
||||
switch (prefix) |
||||
{ |
||||
case IMAGE_PNG: |
||||
return "png"; |
||||
case IMAGE_JPG: |
||||
return "jpg"; |
||||
case IMAGE_JPEG: |
||||
return "jpeg"; |
||||
case IMAGE_BMP: |
||||
return "bmp"; |
||||
case IMAGE_GIF: |
||||
return "gif"; |
||||
default: |
||||
return ""; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,81 @@
@@ -0,0 +1,81 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||
xmlns="http://maven.apache.org/POM/4.0.0" |
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
||||
<parent> |
||||
<groupId>com.ruoyi</groupId> |
||||
<artifactId>ruoyi-modules</artifactId> |
||||
<version>2.2.0</version> |
||||
</parent> |
||||
<modelVersion>4.0.0</modelVersion> |
||||
|
||||
<artifactId>ruoyi-modules-file</artifactId> |
||||
|
||||
<description> |
||||
ruoyi-modules-file文件服务 |
||||
</description> |
||||
|
||||
<dependencies> |
||||
|
||||
<!-- SpringCloud Ailibaba Nacos --> |
||||
<dependency> |
||||
<groupId>com.alibaba.cloud</groupId> |
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> |
||||
</dependency> |
||||
|
||||
<!-- SpringCloud Ailibaba Nacos Config --> |
||||
<dependency> |
||||
<groupId>com.alibaba.cloud</groupId> |
||||
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> |
||||
</dependency> |
||||
|
||||
<!-- SpringCloud Ailibaba Sentinel --> |
||||
<dependency> |
||||
<groupId>com.alibaba.cloud</groupId> |
||||
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> |
||||
</dependency> |
||||
|
||||
<!-- SpringBoot Actuator --> |
||||
<dependency> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<artifactId>spring-boot-starter-actuator</artifactId> |
||||
</dependency> |
||||
|
||||
<!-- Swagger --> |
||||
<dependency> |
||||
<groupId>io.springfox</groupId> |
||||
<artifactId>springfox-swagger-ui</artifactId> |
||||
<version>${swagger.fox.version}</version> |
||||
</dependency> |
||||
|
||||
<!-- Ruoyi Common Security --> |
||||
<dependency> |
||||
<groupId>com.ruoyi</groupId> |
||||
<artifactId>ruoyi-common-security</artifactId> |
||||
</dependency> |
||||
|
||||
<!-- Ruoyi Common Swagger --> |
||||
<dependency> |
||||
<groupId>com.ruoyi</groupId> |
||||
<artifactId>ruoyi-common-swagger</artifactId> |
||||
</dependency> |
||||
|
||||
</dependencies> |
||||
|
||||
<build> |
||||
<plugins> |
||||
<plugin> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<artifactId>spring-boot-maven-plugin</artifactId> |
||||
<executions> |
||||
<execution> |
||||
<goals> |
||||
<goal>repackage</goal> |
||||
</goals> |
||||
</execution> |
||||
</executions> |
||||
</plugin> |
||||
</plugins> |
||||
</build> |
||||
|
||||
</project> |
@ -0,0 +1,35 @@
@@ -0,0 +1,35 @@
|
||||
package com.ruoyi.file; |
||||
|
||||
import org.springframework.boot.SpringApplication; |
||||
import org.springframework.boot.autoconfigure.SpringBootApplication; |
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; |
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; |
||||
import com.ruoyi.common.security.annotation.EnableRyFeignClients; |
||||
import com.ruoyi.common.swagger.annotation.EnableCustomSwagger2; |
||||
|
||||
/** |
||||
* 文件服务 |
||||
* |
||||
* @author ruoyi |
||||
*/ |
||||
@EnableCustomSwagger2 |
||||
@EnableRyFeignClients |
||||
@EnableDiscoveryClient |
||||
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class }) |
||||
public class RuoYFileApplication |
||||
{ |
||||
public static void main(String[] args) |
||||
{ |
||||
SpringApplication.run(RuoYFileApplication.class, args); |
||||
System.out.println("(♥◠‿◠)ノ゙ 文件服务模块启动成功 ლ(´ڡ`ლ)゙ \n" + |
||||
" .-------. ____ __ \n" + |
||||
" | _ _ \\ \\ \\ / / \n" + |
||||
" | ( ' ) | \\ _. / ' \n" + |
||||
" |(_ o _) / _( )_ .' \n" + |
||||
" | (_,_).' __ ___(_ o _)' \n" + |
||||
" | |\\ \\ | || |(_,_)' \n" + |
||||
" | | \\ `' /| `-' / \n" + |
||||
" | | \\ / \\ / \n" + |
||||
" ''-' `'-' `-..-' "); |
||||
} |
||||
} |
@ -0,0 +1,36 @@
@@ -0,0 +1,36 @@
|
||||
package com.ruoyi.file.config; |
||||
|
||||
import java.io.File; |
||||
import org.springframework.beans.factory.annotation.Value; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; |
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
||||
|
||||
/** |
||||
* 通用映射配置 |
||||
* |
||||
* @author ruoyi |
||||
*/ |
||||
@Configuration |
||||
public class ResourcesConfig implements WebMvcConfigurer |
||||
{ |
||||
/** |
||||
* 上传文件存储在本地的根路径 |
||||
*/ |
||||
@Value("${file.path}") |
||||
private String localFilePath; |
||||
|
||||
/** |
||||
* 资源映射路径 前缀 |
||||
*/ |
||||
@Value("${file.prefix}") |
||||
public String localFilePrefix; |
||||
|
||||
@Override |
||||
public void addResourceHandlers(ResourceHandlerRegistry registry) |
||||
{ |
||||
/** 本地文件上传路径 */ |
||||
registry.addResourceHandler(localFilePrefix + "/**") |
||||
.addResourceLocations("file:" + localFilePath + File.separator); |
||||
} |
||||
} |
@ -0,0 +1,67 @@
@@ -0,0 +1,67 @@
|
||||
package com.ruoyi.file.controller; |
||||
|
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.beans.factory.annotation.Value; |
||||
import org.springframework.web.bind.annotation.PostMapping; |
||||
import org.springframework.web.bind.annotation.RestController; |
||||
import org.springframework.web.multipart.MultipartFile; |
||||
import com.ruoyi.common.core.domain.R; |
||||
import com.ruoyi.file.service.ISysFileService; |
||||
import com.ruoyi.system.api.domain.SysFile; |
||||
|
||||
/** |
||||
* 文件请求处理 |
||||
* |
||||
* @author ruoyi |
||||
*/ |
||||
@RestController |
||||
public class SysFileController |
||||
{ |
||||
private static final Logger log = LoggerFactory.getLogger(SysFileController.class); |
||||
|
||||
/** |
||||
* 上传文件存储在本地的根路径 |
||||
*/ |
||||
@Value("${file.path}") |
||||
private String localFilePath; |
||||
|
||||
/** |
||||
* 资源映射路径 前缀 |
||||
*/ |
||||
@Value("${file.prefix}") |
||||
public String localFilePrefix; |
||||
|
||||
/** |
||||
* 域名或本机访问地址 |
||||
*/ |
||||
@Value("${file.domain}") |
||||
public String domain; |
||||
|
||||
@Autowired |
||||
private ISysFileService sysFileService; |
||||
|
||||
/** |
||||
* 文件上传请求 |
||||
*/ |
||||
@PostMapping("upload") |
||||
public R<SysFile> upload(MultipartFile file) |
||||
{ |
||||
try |
||||
{ |
||||
// 上传并返回新文件名称
|
||||
String fileName = sysFileService.uploadFile(file, localFilePath); |
||||
String url = domain + localFilePrefix + fileName; |
||||
SysFile sysFile = new SysFile(); |
||||
sysFile.setName(fileName); |
||||
sysFile.setUrl(url); |
||||
return R.ok(sysFile); |
||||
} |
||||
catch (Exception e) |
||||
{ |
||||
log.error("上传文件失败", e); |
||||
return R.fail(e.getMessage()); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,21 @@
@@ -0,0 +1,21 @@
|
||||
package com.ruoyi.file.service; |
||||
|
||||
import org.springframework.web.multipart.MultipartFile; |
||||
|
||||
/** |
||||
* 文件上传接口 |
||||
* |
||||
* @author ruoyi |
||||
*/ |
||||
public interface ISysFileService |
||||
{ |
||||
/** |
||||
* 文件上传接口 |
||||
* |
||||
* @param file 上传的文件 |
||||
* @param baseDir 相对应用的基目录 |
||||
* @return 文件名称 |
||||
* @throws Exception |
||||
*/ |
||||
public String uploadFile(MultipartFile file, String baseDir) throws Exception; |
||||
} |
@ -0,0 +1,27 @@
@@ -0,0 +1,27 @@
|
||||
package com.ruoyi.file.service; |
||||
|
||||
import org.springframework.stereotype.Service; |
||||
import org.springframework.web.multipart.MultipartFile; |
||||
import com.ruoyi.file.utils.FileUploadUtils; |
||||
|
||||
/** |
||||
* 本地文件存储 |
||||
* |
||||
* @author ruoyi |
||||
*/ |
||||
@Service |
||||
public class LocalSysFileServiceImpl implements ISysFileService |
||||
{ |
||||
/** |
||||
* 文件上传接口 |
||||
* |
||||
* @param file 上传的文件 |
||||
* @param baseDir 相对应用的基目录 |
||||
* @return 文件名称 |
||||
* @throws Exception |
||||
*/ |
||||
public String uploadFile(MultipartFile file, String baseDir) throws Exception |
||||
{ |
||||
return FileUploadUtils.upload(baseDir, file); |
||||
} |
||||
} |
@ -0,0 +1,204 @@
@@ -0,0 +1,204 @@
|
||||
package com.ruoyi.file.utils; |
||||
|
||||
import java.io.File; |
||||
import java.io.IOException; |
||||
import org.apache.commons.io.FilenameUtils; |
||||
import org.springframework.beans.factory.annotation.Value; |
||||
import org.springframework.web.multipart.MultipartFile; |
||||
import com.ruoyi.common.core.exception.file.FileNameLengthLimitExceededException; |
||||
import com.ruoyi.common.core.exception.file.FileSizeLimitExceededException; |
||||
import com.ruoyi.common.core.exception.file.InvalidExtensionException; |
||||
import com.ruoyi.common.core.utils.DateUtils; |
||||
import com.ruoyi.common.core.utils.IdUtils; |
||||
import com.ruoyi.common.core.utils.StringUtils; |
||||
import com.ruoyi.common.core.utils.file.MimeTypeUtils; |
||||
|
||||
/** |
||||
* 文件上传工具类 |
||||
* |
||||
* @author ruoyi |
||||
*/ |
||||
public class FileUploadUtils |
||||
{ |
||||
/** |
||||
* 默认大小 50M |
||||
*/ |
||||
public static final long DEFAULT_MAX_SIZE = 50 * 1024 * 1024; |
||||
|
||||
/** |
||||
* 默认的文件名最大长度 100 |
||||
*/ |
||||
public static final int DEFAULT_FILE_NAME_LENGTH = 100; |
||||
|
||||
/** |
||||
* 资源映射路径 前缀 |
||||
*/ |
||||
@Value("${file.prefix}") |
||||
public String localFilePrefix; |
||||
|
||||
/** |
||||
* 根据文件路径上传 |
||||
* |
||||
* @param baseDir 相对应用的基目录 |
||||
* @param file 上传的文件 |
||||
* @return 文件名称 |
||||
* @throws IOException |
||||
*/ |
||||
public static final String upload(String baseDir, MultipartFile file) throws IOException |
||||
{ |
||||
try |
||||
{ |
||||
return upload(baseDir, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION); |
||||
} |
||||
catch (Exception e) |
||||
{ |
||||
throw new IOException(e.getMessage(), e); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 文件上传 |
||||
* |
||||
* @param baseDir 相对应用的基目录 |
||||
* @param file 上传的文件 |
||||
* @param extension 上传文件类型 |
||||
* @return 返回上传成功的文件名 |
||||
* @throws FileSizeLimitExceededException 如果超出最大大小 |
||||
* @throws FileNameLengthLimitExceededException 文件名太长 |
||||
* @throws IOException 比如读写文件出错时 |
||||
* @throws InvalidExtensionException 文件校验异常 |
||||
*/ |
||||
public static final String upload(String baseDir, MultipartFile file, String[] allowedExtension) |
||||
throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException, |
||||
InvalidExtensionException |
||||
{ |
||||
int fileNamelength = file.getOriginalFilename().length(); |
||||
if (fileNamelength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH) |
||||
{ |
||||
throw new FileNameLengthLimitExceededException(FileUploadUtils.DEFAULT_FILE_NAME_LENGTH); |
||||
} |
||||
|
||||
assertAllowed(file, allowedExtension); |
||||
|
||||
String fileName = extractFilename(file); |
||||
|
||||
File desc = getAbsoluteFile(baseDir, fileName); |
||||
file.transferTo(desc); |
||||
String pathFileName = getPathFileName(fileName); |
||||
return pathFileName; |
||||
} |
||||
|
||||
/** |
||||
* 编码文件名 |
||||
*/ |
||||
public static final String extractFilename(MultipartFile file) |
||||
{ |
||||
String fileName = file.getOriginalFilename(); |
||||
String extension = getExtension(file); |
||||
fileName = DateUtils.datePath() + "/" + IdUtils.fastUUID() + "." + extension; |
||||
return fileName; |
||||
} |
||||
|
||||
private 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; |
||||
} |
||||
|
||||
private static final String getPathFileName(String fileName) throws IOException |
||||
{ |
||||
String pathFileName = "/" + fileName; |
||||
return pathFileName; |
||||
} |
||||
|
||||
/** |
||||
* 文件大小校验 |
||||
* |
||||
* @param file 上传的文件 |
||||
* @return |
||||
* @throws FileSizeLimitExceededException 如果超出最大大小 |
||||
* @throws InvalidExtensionException |
||||
*/ |
||||
public static final void assertAllowed(MultipartFile file, String[] allowedExtension) |
||||
throws FileSizeLimitExceededException, InvalidExtensionException |
||||
{ |
||||
long size = file.getSize(); |
||||
if (DEFAULT_MAX_SIZE != -1 && size > DEFAULT_MAX_SIZE) |
||||
{ |
||||
throw new FileSizeLimitExceededException(DEFAULT_MAX_SIZE / 1024 / 1024); |
||||
} |
||||
|
||||
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); |
||||
} |
||||
else if (allowedExtension == MimeTypeUtils.FLASH_EXTENSION) |
||||
{ |
||||
throw new InvalidExtensionException.InvalidFlashExtensionException(allowedExtension, extension, |
||||
fileName); |
||||
} |
||||
else if (allowedExtension == MimeTypeUtils.MEDIA_EXTENSION) |
||||
{ |
||||
throw new InvalidExtensionException.InvalidMediaExtensionException(allowedExtension, extension, |
||||
fileName); |
||||
} |
||||
else if (allowedExtension == MimeTypeUtils.VIDEO_EXTENSION) |
||||
{ |
||||
throw new InvalidExtensionException.InvalidVideoExtensionException(allowedExtension, extension, |
||||
fileName); |
||||
} |
||||
else |
||||
{ |
||||
throw new InvalidExtensionException(allowedExtension, extension, fileName); |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 判断MIME类型是否是允许的MIME类型 |
||||
* |
||||
* @param extension |
||||
* @param allowedExtension |
||||
* @return |
||||
*/ |
||||
public static final boolean isAllowedExtension(String extension, String[] allowedExtension) |
||||
{ |
||||
for (String str : allowedExtension) |
||||
{ |
||||
if (str.equalsIgnoreCase(extension)) |
||||
{ |
||||
return true; |
||||
} |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
/** |
||||
* 获取文件名的后缀 |
||||
* |
||||
* @param file 表单文件 |
||||
* @return 后缀名 |
||||
*/ |
||||
public static final String getExtension(MultipartFile file) |
||||
{ |
||||
String extension = FilenameUtils.getExtension(file.getOriginalFilename()); |
||||
if (StringUtils.isEmpty(extension)) |
||||
{ |
||||
extension = MimeTypeUtils.getExtension(file.getContentType()); |
||||
} |
||||
return extension; |
||||
} |
||||
} |
@ -0,0 +1,10 @@
@@ -0,0 +1,10 @@
|
||||
Spring Boot Version: ${spring-boot.version} |
||||
Spring Application Name: ${spring.application.name} |
||||
_ __ _ _ |
||||
(_) / _|(_)| | |
||||
_ __ _ _ ___ _ _ _ ______ | |_ _ | | ___ |
||||
| '__|| | | | / _ \ | | | || ||______|| _|| || | / _ \ |
||||
| | | |_| || (_) || |_| || | | | | || || __/ |
||||
|_| \__,_| \___/ \__, ||_| |_| |_||_| \___| |
||||
__/ | |
||||
|___/ |
@ -0,0 +1,24 @@
@@ -0,0 +1,24 @@
|
||||
# Tomcat |
||||
server: |
||||
port: 9300 |
||||
|
||||
# Spring |
||||
spring: |
||||
application: |
||||
# 应用名称 |
||||
name: ruoyi-file |
||||
profiles: |
||||
# 环境配置 |
||||
active: dev |
||||
cloud: |
||||
nacos: |
||||
discovery: |
||||
# 服务注册地址 |
||||
server-addr: 127.0.0.1:8848 |
||||
config: |
||||
# 配置中心地址 |
||||
server-addr: 127.0.0.1:8848 |
||||
# 配置文件格式 |
||||
file-extension: yml |
||||
# 共享配置 |
||||
shared-dataids: application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} |
@ -0,0 +1,74 @@
@@ -0,0 +1,74 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<configuration scan="true" scanPeriod="60 seconds" debug="false"> |
||||
<!-- 日志存放路径 --> |
||||
<property name="log.path" value="logs/ruoyi-file" /> |
||||
<!-- 日志输出格式 --> |
||||
<property name="log.pattern" value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{20} - [%method,%line] - %msg%n" /> |
||||
|
||||
<!-- 控制台输出 --> |
||||
<appender name="console" class="ch.qos.logback.core.ConsoleAppender"> |
||||
<encoder> |
||||
<pattern>${log.pattern}</pattern> |
||||
</encoder> |
||||
</appender> |
||||
|
||||
<!-- 系统日志输出 --> |
||||
<appender name="file_info" class="ch.qos.logback.core.rolling.RollingFileAppender"> |
||||
<file>${log.path}/info.log</file> |
||||
<!-- 循环政策:基于时间创建日志文件 --> |
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> |
||||
<!-- 日志文件名格式 --> |
||||
<fileNamePattern>${log.path}/info.%d{yyyy-MM-dd}.log</fileNamePattern> |
||||
<!-- 日志最大的历史 60天 --> |
||||
<maxHistory>60</maxHistory> |
||||
</rollingPolicy> |
||||
<encoder> |
||||
<pattern>${log.pattern}</pattern> |
||||
</encoder> |
||||
<filter class="ch.qos.logback.classic.filter.LevelFilter"> |
||||
<!-- 过滤的级别 --> |
||||
<level>INFO</level> |
||||
<!-- 匹配时的操作:接收(记录) --> |
||||
<onMatch>ACCEPT</onMatch> |
||||
<!-- 不匹配时的操作:拒绝(不记录) --> |
||||
<onMismatch>DENY</onMismatch> |
||||
</filter> |
||||
</appender> |
||||
|
||||
<appender name="file_error" class="ch.qos.logback.core.rolling.RollingFileAppender"> |
||||
<file>${log.path}/error.log</file> |
||||
<!-- 循环政策:基于时间创建日志文件 --> |
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> |
||||
<!-- 日志文件名格式 --> |
||||
<fileNamePattern>${log.path}/error.%d{yyyy-MM-dd}.log</fileNamePattern> |
||||
<!-- 日志最大的历史 60天 --> |
||||
<maxHistory>60</maxHistory> |
||||
</rollingPolicy> |
||||
<encoder> |
||||
<pattern>${log.pattern}</pattern> |
||||
</encoder> |
||||
<filter class="ch.qos.logback.classic.filter.LevelFilter"> |
||||
<!-- 过滤的级别 --> |
||||
<level>ERROR</level> |
||||
<!-- 匹配时的操作:接收(记录) --> |
||||
<onMatch>ACCEPT</onMatch> |
||||
<!-- 不匹配时的操作:拒绝(不记录) --> |
||||
<onMismatch>DENY</onMismatch> |
||||
</filter> |
||||
</appender> |
||||
|
||||
<!-- 系统模块日志级别控制 --> |
||||
<logger name="com.ruoyi" level="info" /> |
||||
<!-- Spring日志级别控制 --> |
||||
<logger name="org.springframework" level="warn" /> |
||||
|
||||
<root level="info"> |
||||
<appender-ref ref="console" /> |
||||
</root> |
||||
|
||||
<!--系统操作日志--> |
||||
<root level="info"> |
||||
<appender-ref ref="file_info" /> |
||||
<appender-ref ref="file_error" /> |
||||
</root> |
||||
</configuration> |
Loading…
Reference in new issue