From 14525bf79fb0fdaa4d2a9852144b498dbd60b87e Mon Sep 17 00:00:00 2001 From: 1137095129 <wang1137095129@foxmail.com> Date: 星期三, 14 七月 2021 16:55:42 +0800 Subject: [PATCH] 更新上传文件模块控制,新增了上传富文本图片的接口 --- web-module/src/main/java/com/thhy/project/webmodule/annotation/ConditionFileUploadEnable.java | 14 + web-module/src/main/java/com/thhy/project/webmodule/service/impl/BaseFileServiceImpl.java | 3 web-module/src/main/resources/META-INF/spring.factories | 4 web-module/src/main/java/com/thhy/project/webmodule/annotation/ConditionRichTextEnable.java | 15 + web-module/src/main/java/com/thhy/project/webmodule/controller/RichTextFileController.java | 95 +++++++ web-module/src/main/java/com/thhy/project/webmodule/config/FileUploadProperties.java | 2 web-module/src/main/java/com/thhy/project/webmodule/annotation/ConditionFileEnable.java | 14 + web-module/src/main/java/com/thhy/project/webmodule/controller/BaseFileUploadController.java | 2 web-module/src/main/java/com/thhy/project/webmodule/config/WebFileConfig.java | 38 ++ web-module/src/main/resources/META-INF/additional-spring-configuration-metadata.json | 12 web-module/src/main/java/com/thhy/project/webmodule/utils/CommonsUtils.java | 320 ++++++++++++------------ util-module/src/main/java/com/thhy/project/utilmodule/utils/ResponseMap.java | 204 ++++++++------- web-module/src/main/java/com/thhy/project/webmodule/config/RichTextProperties.java | 56 ++++ 13 files changed, 511 insertions(+), 268 deletions(-) diff --git a/util-module/src/main/java/com/thhy/project/utilmodule/utils/ResponseMap.java b/util-module/src/main/java/com/thhy/project/utilmodule/utils/ResponseMap.java index 0ba3116..d8e98c5 100644 --- a/util-module/src/main/java/com/thhy/project/utilmodule/utils/ResponseMap.java +++ b/util-module/src/main/java/com/thhy/project/utilmodule/utils/ResponseMap.java @@ -1,6 +1,8 @@ package com.thhy.project.utilmodule.utils; +import org.springframework.web.bind.annotation.RequestParam; + import java.io.Serializable; /** @@ -8,129 +10,133 @@ */ public class ResponseMap<T> { - public static <T> ResponseMap.ResultData<T> notFound(){ - return notFound("资源未找到!"); - } + public static <T> ResponseMap.ResultData<T> notFound() { + return notFound("资源未找到!"); + } - public static <T> ResponseMap.ResultData<T> notFound(String msg){ - return new ResultData<>(ResultCode.SOURCE_NOT_FOUND_ERROR,"fail",msg); - } + public static <T> ResponseMap.ResultData<T> notFound(String msg) { + return new ResultData<>(ResultCode.SOURCE_NOT_FOUND_ERROR, "fail", msg); + } - public static <T> ResponseMap.ResultData<T> error() { - return error(ResultCode.SYSTEM_ERROR, "未知异常,请联系管理员"); - } + public static <T> ResponseMap.ResultData<T> error() { + return error(ResultCode.SYSTEM_ERROR, "未知异常,请联系管理员"); + } - public static <T> ResponseMap.ResultData<T> error(String msg) { - return error(ResultCode.SYSTEM_ERROR, msg); - } + public static <T> ResponseMap.ResultData<T> error(String msg) { + return error(ResultCode.SYSTEM_ERROR, msg); + } - public static <T> ResponseMap.ResultData<T> error(int code, String msg) { - return new ResultData<>(code, "fail", msg); - } + public static <T> ResponseMap.ResultData<T> error(int code, String msg) { + return new ResultData<>(code, "fail", msg); + } - public static <T> ResponseMap.ResultData<T> toLogError() { - return toLogError("请先登录!"); - } + public static <T> ResponseMap.ResultData<T> toLogError() { + return toLogError("请先登录!"); + } - public static <T> ResponseMap.ResultData<T> toLogError(String msg) { - return new ResultData<>(ResultCode.IDENTITY_AUTHENTICATION_ERROR, "toLogIN", msg); - } + public static <T> ResponseMap.ResultData<T> toLogError(String msg) { + return new ResultData<>(ResultCode.IDENTITY_AUTHENTICATION_ERROR, "toLogIN", msg); + } - public static <T> ResponseMap.ResultData<T> jurisdictionError() { - return new ResultData<>(ResultCode.PERMISSION_DENIED_ERROR, "fail", "权限不足!"); - } + public static <T> ResponseMap.ResultData<T> jurisdictionError() { + return new ResultData<>(ResultCode.PERMISSION_DENIED_ERROR, "fail", "权限不足!"); + } - public static <T> ResponseMap.ResultData<T> ok(String msg) { - return new ResultData<>(ResultCode.SUCCESS, "ok", msg); - } + public static <T> ResponseMap.ResultData<T> ok(String msg) { + return new ResultData<>(ResultCode.SUCCESS, "ok", msg); + } - public static <T> ResponseMap.ResultData<T> fail(String msg) { - return new ResultData<>(ResultCode.PARAM_ERROR, "fail", msg); - } + public static <T> ResponseMap.ResultData<T> fail(String msg) { + return new ResultData<>(ResultCode.PARAM_ERROR, "fail", msg); + } - public static <T> ResponseMap.ResultData<T> ok(T data) { - return new ResultData<>(ResultCode.SUCCESS, "ok", "请求成功", data); - } + public static <T> ResponseMap.ResultData<T> ok(T data) { + return new ResultData<>(ResultCode.SUCCESS, "ok", "请求成功", data); + } - public static <T> ResponseMap.ResultData<T> ok() { - return new ResultData<>(ResultCode.SUCCESS, "ok", "请求成功!"); - } + public static <T> ResponseMap.ResultData<T> okWithData(T data) { + return ok(data); + } - public static class ResultData<T> implements Serializable { - private static final long serialVersionUID = -2467879912967236962L; - private int code; - private String sym; - private String msg; - private T data; + public static <T> ResponseMap.ResultData<T> ok() { + return new ResultData<>(ResultCode.SUCCESS, "ok", "请求成功!"); + } - public ResultData() { - } + public static class ResultData<T> implements Serializable { + private static final long serialVersionUID = -2467879912967236962L; + private int code; + private String sym; + private String msg; + private T data; - ResultData(int code, String sym, String msg) { - this.code = code; - this.sym = sym; - this.msg = msg; - } + public ResultData() { + } - ResultData(int code, String sym, String msg, T data) { - this.code = code; - this.sym = sym; - this.msg = msg; - this.data = data; - } + ResultData(int code, String sym, String msg) { + this.code = code; + this.sym = sym; + this.msg = msg; + } - public int getCode() { - return code; - } + ResultData(int code, String sym, String msg, T data) { + this.code = code; + this.sym = sym; + this.msg = msg; + this.data = data; + } - public void setCode(int code) { - this.code = code; - } + public int getCode() { + return code; + } - public String getSym() { - return sym; - } + public void setCode(int code) { + this.code = code; + } - public void setSym(String sym) { - this.sym = sym; - } + public String getSym() { + return sym; + } - public String getMsg() { - return msg; - } + public void setSym(String sym) { + this.sym = sym; + } - public void setMsg(String msg) { - this.msg = msg; - } + public String getMsg() { + return msg; + } - public T getData() { - return data; - } + public void setMsg(String msg) { + this.msg = msg; + } - public void setData(T data) { - this.data = data; - } + public T getData() { + return data; + } - public boolean isError(){ - return !this.sym.equals("ok"); - } - } + public void setData(T data) { + this.data = data; + } - public static class ResultCode { - //系统错误 - public final static int SYSTEM_ERROR = 5001; - //参数错误 - public final static int PARAM_ERROR = 4001; - //身份认证错误 - public final static int IDENTITY_AUTHENTICATION_ERROR = 4031; - //权限被拒绝错误 - public final static int PERMISSION_DENIED_ERROR = 4032; - //资源未找到错误 - public final static int SOURCE_NOT_FOUND_ERROR = 4041; - //请求(操作)成功 - public final static int SUCCESS = 2000; - //远程过程调用错误 - public final static int REMOTE_ERROR = 5002; - } + public boolean isError() { + return !this.sym.equals("ok"); + } + } + + public static class ResultCode { + //系统错误 + public final static int SYSTEM_ERROR = 5001; + //参数错误 + public final static int PARAM_ERROR = 4001; + //身份认证错误 + public final static int IDENTITY_AUTHENTICATION_ERROR = 4031; + //权限被拒绝错误 + public final static int PERMISSION_DENIED_ERROR = 4032; + //资源未找到错误 + public final static int SOURCE_NOT_FOUND_ERROR = 4041; + //请求(操作)成功 + public final static int SUCCESS = 2000; + //远程过程调用错误 + public final static int REMOTE_ERROR = 5002; + } } diff --git a/web-module/src/main/java/com/thhy/project/webmodule/annotation/ConditionFileEnable.java b/web-module/src/main/java/com/thhy/project/webmodule/annotation/ConditionFileEnable.java new file mode 100644 index 0000000..76713e8 --- /dev/null +++ b/web-module/src/main/java/com/thhy/project/webmodule/annotation/ConditionFileEnable.java @@ -0,0 +1,14 @@ +package com.thhy.project.webmodule.annotation; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.METHOD, ElementType.TYPE}) +@ConditionalOnExpression(value = "${spring.web.file-upload.enable:${spring.web.rich-text.enable:false}}") +public @interface ConditionFileEnable { +} diff --git a/web-module/src/main/java/com/thhy/project/webmodule/annotation/ConditionFileUploadEnable.java b/web-module/src/main/java/com/thhy/project/webmodule/annotation/ConditionFileUploadEnable.java new file mode 100644 index 0000000..781db67 --- /dev/null +++ b/web-module/src/main/java/com/thhy/project/webmodule/annotation/ConditionFileUploadEnable.java @@ -0,0 +1,14 @@ +package com.thhy.project.webmodule.annotation; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.METHOD,ElementType.TYPE}) +@ConditionalOnProperty(value = "spring.web.file-upload.enable",matchIfMissing = false) +public @interface ConditionFileUploadEnable { +} diff --git a/web-module/src/main/java/com/thhy/project/webmodule/annotation/ConditionRichTextEnable.java b/web-module/src/main/java/com/thhy/project/webmodule/annotation/ConditionRichTextEnable.java new file mode 100644 index 0000000..c5421ce --- /dev/null +++ b/web-module/src/main/java/com/thhy/project/webmodule/annotation/ConditionRichTextEnable.java @@ -0,0 +1,15 @@ +package com.thhy.project.webmodule.annotation; + + +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.METHOD,ElementType.TYPE}) +@ConditionalOnProperty(value = "spring.web.rich-text.enable",matchIfMissing = false) +public @interface ConditionRichTextEnable { +} diff --git a/web-module/src/main/java/com/thhy/project/webmodule/config/FileUploadProperties.java b/web-module/src/main/java/com/thhy/project/webmodule/config/FileUploadProperties.java index 44843ff..63708f8 100644 --- a/web-module/src/main/java/com/thhy/project/webmodule/config/FileUploadProperties.java +++ b/web-module/src/main/java/com/thhy/project/webmodule/config/FileUploadProperties.java @@ -1,10 +1,12 @@ package com.thhy.project.webmodule.config; +import com.thhy.project.webmodule.annotation.ConditionFileUploadEnable; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "file-upload") +@ConditionFileUploadEnable public class FileUploadProperties { /** diff --git a/web-module/src/main/java/com/thhy/project/webmodule/config/RichTextProperties.java b/web-module/src/main/java/com/thhy/project/webmodule/config/RichTextProperties.java new file mode 100644 index 0000000..eb683fc --- /dev/null +++ b/web-module/src/main/java/com/thhy/project/webmodule/config/RichTextProperties.java @@ -0,0 +1,56 @@ +package com.thhy.project.webmodule.config; + +import com.thhy.project.webmodule.annotation.ConditionRichTextEnable; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +import java.io.Serializable; + +@Component +@ConfigurationProperties(prefix = "spring.web.rich-text") +@ConditionRichTextEnable +public class RichTextProperties implements Serializable { + private String serverHost; + private Integer port; + private String uploadFolder; + private String staticAccessPath; + + public RichTextProperties() { + this.serverHost = "localhost"; + this.port = 8080; + this.uploadFolder = "/tmp/rich-text-file/"; + this.staticAccessPath = "/rich-text-file/"; + } + + public String getServerHost() { + return serverHost; + } + + public void setServerHost(String serverHost) { + this.serverHost = serverHost; + } + + public Integer getPort() { + return port; + } + + public void setPort(Integer port) { + this.port = port; + } + + public String getUploadFolder() { + return uploadFolder; + } + + public void setUploadFolder(String uploadFolder) { + this.uploadFolder = uploadFolder; + } + + public String getStaticAccessPath() { + return staticAccessPath; + } + + public void setStaticAccessPath(String staticAccessPath) { + this.staticAccessPath = staticAccessPath; + } +} diff --git a/web-module/src/main/java/com/thhy/project/webmodule/config/WebFileConfig.java b/web-module/src/main/java/com/thhy/project/webmodule/config/WebFileConfig.java index 357beee..6affe29 100644 --- a/web-module/src/main/java/com/thhy/project/webmodule/config/WebFileConfig.java +++ b/web-module/src/main/java/com/thhy/project/webmodule/config/WebFileConfig.java @@ -1,20 +1,42 @@ package com.thhy.project.webmodule.config; +import com.thhy.project.webmodule.annotation.ConditionFileUploadEnable; +import com.thhy.project.webmodule.annotation.ConditionRichTextEnable; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration -public class WebFileConfig implements WebMvcConfigurer { +public class WebFileConfig { - @Autowired - private FileUploadProperties fileUploadProperties; - @Override - public void addResourceHandlers(ResourceHandlerRegistry registry) { - registry - .addResourceHandler(fileUploadProperties.getStaticAccessPath() + "**") - .addResourceLocations("file:" + fileUploadProperties.getUploadFolder()); + + @Bean("File-Upload-Config") + @ConditionFileUploadEnable + public WebMvcConfigurer fileUploadConfig(FileUploadProperties fileUploadProperties){ + return new WebMvcConfigurer() { + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry + .addResourceHandler(fileUploadProperties.getStaticAccessPath() + "**") + .addResourceLocations("file:" + fileUploadProperties.getUploadFolder()); + } + }; } + + @Bean("Rich-Text-Config") + @ConditionRichTextEnable + public WebMvcConfigurer richTextConfig(RichTextProperties richTextProperties){ + return new WebMvcConfigurer() { + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry + .addResourceHandler(richTextProperties.getStaticAccessPath() + "**") + .addResourceLocations("file:" + richTextProperties.getUploadFolder()); + } + }; + } + } diff --git a/web-module/src/main/java/com/thhy/project/webmodule/controller/BaseFileUploadController.java b/web-module/src/main/java/com/thhy/project/webmodule/controller/BaseFileUploadController.java index 4a2a4c9..8ab1204 100644 --- a/web-module/src/main/java/com/thhy/project/webmodule/controller/BaseFileUploadController.java +++ b/web-module/src/main/java/com/thhy/project/webmodule/controller/BaseFileUploadController.java @@ -7,6 +7,7 @@ import com.thhy.project.utilmodule.utils.ResponseMap; import com.thhy.project.utilmodule.utils.core.UserInfo; import com.thhy.project.utilmodule.utils.datastruct.queue.priority.AbstractPriorityQueue; +import com.thhy.project.webmodule.annotation.ConditionFileUploadEnable; import com.thhy.project.webmodule.config.FileUploadProperties; import com.thhy.project.webmodule.entity.FileSliceInfo; import com.thhy.project.webmodule.service.BaseFileService; @@ -36,6 +37,7 @@ import java.util.concurrent.ConcurrentMap; @RestController +@ConditionFileUploadEnable public class BaseFileUploadController { private static final Logger logger = LoggerFactory.getLogger(BaseFileUploadController.class); diff --git a/web-module/src/main/java/com/thhy/project/webmodule/controller/RichTextFileController.java b/web-module/src/main/java/com/thhy/project/webmodule/controller/RichTextFileController.java new file mode 100644 index 0000000..694a33e --- /dev/null +++ b/web-module/src/main/java/com/thhy/project/webmodule/controller/RichTextFileController.java @@ -0,0 +1,95 @@ +package com.thhy.project.webmodule.controller; + + +import com.thhy.project.webmodule.annotation.ConditionRichTextEnable; +import com.thhy.project.webmodule.config.RichTextProperties; +import com.thhy.project.webmodule.utils.CommonsUtils; +import com.thhy.project.webmodule.vo.FileKindException; +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.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; +import org.springframework.web.util.UriComponentsBuilder; + +import java.io.IOException; + +@RestController +@ConditionRichTextEnable +public class RichTextFileController { + + @Autowired + private RichTextProperties richTextProperties; + + @Value("${spring.application.name}") + private String applicationName; + + @PostMapping("/richTextFileUpload") + public RichTextResult richTextFileUpload(@RequestParam("file") MultipartFile file) throws IOException, FileKindException { + String path = CommonsUtils.getUploadFileRelativePath(file, richTextProperties.getUploadFolder()); + String webPath = UriComponentsBuilder.newInstance() + .scheme("http") + .host(richTextProperties.getServerHost()) + .port(richTextProperties.getPort()) + .path(applicationName) + .path(richTextProperties.getStaticAccessPath()) + .path(path) + .build() + .toUri() + .toString(); + + RichTextResult result = new RichTextResult(0); + String[] fileInfos = new String[1]; + fileInfos[0] = webPath; + result.setData(fileInfos); + return result; + } + + public static class RichTextResult { + private int errno; + private String[] data; + + public RichTextResult() { + } + + public RichTextResult(int errno) { + this.errno = errno; + } + + public int getErrno() { + return errno; + } + + public void setErrno(int errno) { + this.errno = errno; + } + + public String[] getData() { + return data; + } + + public void setData(String[] data) { + this.data = data; + } + } + + public static class RichTextFileInfo { + private String url; + + public RichTextFileInfo(String url) { + this.url = url; + } + + public RichTextFileInfo() { + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + } +} diff --git a/web-module/src/main/java/com/thhy/project/webmodule/service/impl/BaseFileServiceImpl.java b/web-module/src/main/java/com/thhy/project/webmodule/service/impl/BaseFileServiceImpl.java index 7b50eb4..7b27269 100644 --- a/web-module/src/main/java/com/thhy/project/webmodule/service/impl/BaseFileServiceImpl.java +++ b/web-module/src/main/java/com/thhy/project/webmodule/service/impl/BaseFileServiceImpl.java @@ -3,6 +3,8 @@ import com.thhy.project.utilmodule.utils.DefaultIOInterfaceImpl; import com.thhy.project.utilmodule.utils.FilePathUtil; import com.thhy.project.utilmodule.utils.IOInterface; +import com.thhy.project.webmodule.annotation.ConditionFileEnable; +import com.thhy.project.webmodule.annotation.ConditionFileUploadEnable; import com.thhy.project.webmodule.config.FileUploadProperties; import com.thhy.project.webmodule.mapper.BaseFileMapper; import com.thhy.project.webmodule.service.BaseFileService; @@ -22,6 +24,7 @@ import java.io.IOException; @Service +@ConditionFileUploadEnable @ConditionalOnMissingBean(BaseFileService.class) public class BaseFileServiceImpl implements BaseFileService { diff --git a/web-module/src/main/java/com/thhy/project/webmodule/utils/CommonsUtils.java b/web-module/src/main/java/com/thhy/project/webmodule/utils/CommonsUtils.java index 86c76a2..0ab3156 100644 --- a/web-module/src/main/java/com/thhy/project/webmodule/utils/CommonsUtils.java +++ b/web-module/src/main/java/com/thhy/project/webmodule/utils/CommonsUtils.java @@ -15,180 +15,180 @@ public class CommonsUtils { - // 生成UUID的方法 - public static String getUUID() { - return UUID.randomUUID().toString().trim().replace("-", ""); - } + // 生成UUID的方法 + public static String getUUID() { + return UUID.randomUUID().toString().trim().replace("-", ""); + } - public static String getUploadFileRealPath(MultipartFile file, String realPath) throws IOException, FileKindException { - return realPath + getUploadFilePath(file, realPath, null, false); - } + public static String getUploadFileRealPath(MultipartFile file, String realPath) throws IOException, FileKindException { + return FilePathUtil.getPath(realPath, getUploadFilePath(file, realPath, null, false)); + } - public static String getUploadFileRelativePath(MultipartFile file, String realPath) throws IOException, FileKindException { - return getUploadFilePath(file, realPath, null, false); - } + public static String getUploadFileRelativePath(MultipartFile file, String realPath) throws IOException, FileKindException { + return getUploadFilePath(file, realPath, null, false); + } - // 文件上传后返回路径的方法 - public static String getUploadFilePath(MultipartFile file, String realPath, String suffixName, boolean checkSuffix) - throws IllegalStateException, IOException, FileKindException { + // 文件上传后返回路径的方法 + public static String getUploadFilePath(MultipartFile file, String realPath, String suffixName, boolean checkSuffix) + throws IllegalStateException, IOException, FileKindException { - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); - String format = sdf.format(new Date()); + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + String format = sdf.format(new Date()); - // 获取文件名 - String filename = file.getOriginalFilename(); - // 获取文件后缀名 - String extensionname = filename.substring(filename.lastIndexOf(".") + 1); - if (checkSuffix && !suffixName.equals(extensionname)) { - throw new FileKindException("文件格式不正确!"); - } - String relativeDir = extensionname + "/" + format; - // 给上传的文件起别名,有很多种方式 - String newFilename = CommonsUtils.getUUID() + "." + extensionname; - String localFilePath = FilePathUtil.getPath(realPath, relativeDir, newFilename); - // 创建File对象,传入目标路径参数,和新的文件别名 - File localFile = new File(localFilePath); - String dirPath = FilePathUtil.getPath(realPath, relativeDir); - File dir = new File(dirPath); - if (!dir.exists()) { - dir.mkdirs(); - } - // 如果dir代表的文件不存在,则创建它, - if (!localFile.exists()) { - localFile.createNewFile(); - } - file.transferTo(localFile); - return relativeDir + "/" + newFilename; - } + // 获取文件名 + String filename = file.getOriginalFilename(); + // 获取文件后缀名 + String extensionname = filename.substring(filename.lastIndexOf(".") + 1); + if (checkSuffix && !suffixName.equals(extensionname)) { + throw new FileKindException("文件格式不正确!"); + } + String relativeDir = extensionname + "/" + format; + // 给上传的文件起别名,有很多种方式 + String newFilename = CommonsUtils.getUUID() + "." + extensionname; + String localFilePath = FilePathUtil.getPath(realPath, relativeDir, newFilename); + // 创建File对象,传入目标路径参数,和新的文件别名 + File localFile = new File(localFilePath); + String dirPath = FilePathUtil.getPath(realPath, relativeDir); + File dir = new File(dirPath); + if (!dir.exists()) { + dir.mkdirs(); + } + // 如果dir代表的文件不存在,则创建它, + if (!localFile.exists()) { + localFile.createNewFile(); + } + file.transferTo(localFile); + return relativeDir + "/" + newFilename; + } //----------------------------------------------------------------------------------------------------------------------- - /** - * 随机生成一段字母加数字 length是生成字符的长度 - * - * @param length - * @return - */ - public static String getStringRandom(int length) { - String val = ""; - Random random = new Random(); - // 参数length,表示生成几位随机数 - for (int i = 0; i < length; i++) { - String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num"; - // 输出字母还是数字 - if ("char".equalsIgnoreCase(charOrNum)) { - // 输出是大写字母还是小写字母 - int temp = random.nextInt(2) % 2 == 0 ? 65 : 97; - val += (char) (random.nextInt(26) + temp); - } else if ("num".equalsIgnoreCase(charOrNum)) { - val += String.valueOf(random.nextInt(10)); - } - } - return val; - } + /** + * 随机生成一段字母加数字 length是生成字符的长度 + * + * @param length + * @return + */ + public static String getStringRandom(int length) { + String val = ""; + Random random = new Random(); + // 参数length,表示生成几位随机数 + for (int i = 0; i < length; i++) { + String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num"; + // 输出字母还是数字 + if ("char".equalsIgnoreCase(charOrNum)) { + // 输出是大写字母还是小写字母 + int temp = random.nextInt(2) % 2 == 0 ? 65 : 97; + val += (char) (random.nextInt(26) + temp); + } else if ("num".equalsIgnoreCase(charOrNum)) { + val += String.valueOf(random.nextInt(10)); + } + } + return val; + } - /** - * 随机生成姓名 - * - * @return - */ - public static String getName() { - Random random = new Random(); - String[] Surname = {"赵", "钱", "孙", "李", "周", "吴", "郑", "王", "冯", "陈", "褚", "卫", "蒋", "沈", "韩", "杨", "朱", "秦", - "尤", "许", "何", "吕", "施", "张", "孔", "曹", "严", "华", "金", "魏", "陶", "姜", "戚", "谢", "邹", "喻", "柏", "水", "窦", - "章", "云", "苏", "潘", "葛", "奚", "范", "彭", "郎", "鲁", "韦", "昌", "马", "苗", "凤", "花", "方", "俞", "任", "袁", "柳", - "酆", "鲍", "史", "唐", "费", "廉", "岑", "薛", "雷", "贺", "倪", "汤", "滕", "殷", "罗", "毕", "郝", "邬", "安", "常", "乐", - "于", "时", "傅", "皮", "卞", "齐", "康", "伍", "余", "元", "卜", "顾", "孟", "平", "黄", "和", "穆", "萧", "尹", "姚", "邵", - "湛", "汪", "祁", "毛", "禹", "狄", "米", "贝", "明", "臧", "计", "伏", "成", "戴", "谈", "宋", "茅", "庞", "熊", "纪", "舒", - "屈", "项", "祝", "董", "梁", "杜", "阮", "蓝", "闵", "席", "季"}; - String girl = "秀娟英华慧巧美娜静淑惠珠翠雅芝玉萍红娥玲芬芳燕彩春菊兰凤洁梅琳素云莲真环雪荣爱妹霞香月莺媛艳瑞凡佳嘉琼勤珍贞莉桂娣叶璧璐娅琦晶妍茜秋珊莎锦黛青倩婷姣婉娴瑾颖露瑶怡婵雁蓓纨仪荷丹蓉眉君琴蕊薇菁梦岚苑婕馨瑗琰韵融园艺咏卿聪澜纯毓悦昭冰爽琬茗羽希宁欣飘育滢馥筠柔竹霭凝晓欢霄枫芸菲寒伊亚宜可姬舒影荔枝思丽 "; - String boy = "伟刚勇毅俊峰强军平保东文辉力明永健世广志义兴良海山仁波宁贵福生龙元全国胜学祥才发武新利清飞彬富顺信子杰涛昌成康星光天达安岩中茂进林有坚和彪博诚先敬震振壮会思群豪心邦承乐绍功松善厚庆磊民友裕河哲江超浩亮政谦亨奇固之轮翰朗伯宏言若鸣朋斌梁栋维启克伦翔旭鹏泽晨辰士以建家致树炎德行时泰盛雄琛钧冠策腾楠榕风航弘"; - int index = random.nextInt(Surname.length - 1); - String name = Surname[index]; - int i = random.nextInt(3); - if (i == 2) { - int j = random.nextInt(girl.length() - 2); - if (j % 2 == 0) { - name = name + girl.substring(j, j + 2); - } else { - name = name + girl.substring(j, j + 1); - } + /** + * 随机生成姓名 + * + * @return + */ + public static String getName() { + Random random = new Random(); + String[] Surname = {"赵", "钱", "孙", "李", "周", "吴", "郑", "王", "冯", "陈", "褚", "卫", "蒋", "沈", "韩", "杨", "朱", "秦", + "尤", "许", "何", "吕", "施", "张", "孔", "曹", "严", "华", "金", "魏", "陶", "姜", "戚", "谢", "邹", "喻", "柏", "水", "窦", + "章", "云", "苏", "潘", "葛", "奚", "范", "彭", "郎", "鲁", "韦", "昌", "马", "苗", "凤", "花", "方", "俞", "任", "袁", "柳", + "酆", "鲍", "史", "唐", "费", "廉", "岑", "薛", "雷", "贺", "倪", "汤", "滕", "殷", "罗", "毕", "郝", "邬", "安", "常", "乐", + "于", "时", "傅", "皮", "卞", "齐", "康", "伍", "余", "元", "卜", "顾", "孟", "平", "黄", "和", "穆", "萧", "尹", "姚", "邵", + "湛", "汪", "祁", "毛", "禹", "狄", "米", "贝", "明", "臧", "计", "伏", "成", "戴", "谈", "宋", "茅", "庞", "熊", "纪", "舒", + "屈", "项", "祝", "董", "梁", "杜", "阮", "蓝", "闵", "席", "季"}; + String girl = "秀娟英华慧巧美娜静淑惠珠翠雅芝玉萍红娥玲芬芳燕彩春菊兰凤洁梅琳素云莲真环雪荣爱妹霞香月莺媛艳瑞凡佳嘉琼勤珍贞莉桂娣叶璧璐娅琦晶妍茜秋珊莎锦黛青倩婷姣婉娴瑾颖露瑶怡婵雁蓓纨仪荷丹蓉眉君琴蕊薇菁梦岚苑婕馨瑗琰韵融园艺咏卿聪澜纯毓悦昭冰爽琬茗羽希宁欣飘育滢馥筠柔竹霭凝晓欢霄枫芸菲寒伊亚宜可姬舒影荔枝思丽 "; + String boy = "伟刚勇毅俊峰强军平保东文辉力明永健世广志义兴良海山仁波宁贵福生龙元全国胜学祥才发武新利清飞彬富顺信子杰涛昌成康星光天达安岩中茂进林有坚和彪博诚先敬震振壮会思群豪心邦承乐绍功松善厚庆磊民友裕河哲江超浩亮政谦亨奇固之轮翰朗伯宏言若鸣朋斌梁栋维启克伦翔旭鹏泽晨辰士以建家致树炎德行时泰盛雄琛钧冠策腾楠榕风航弘"; + int index = random.nextInt(Surname.length - 1); + String name = Surname[index]; + int i = random.nextInt(3); + if (i == 2) { + int j = random.nextInt(girl.length() - 2); + if (j % 2 == 0) { + name = name + girl.substring(j, j + 2); + } else { + name = name + girl.substring(j, j + 1); + } - } else { - int j = random.nextInt(girl.length() - 2); - if (j % 2 == 0) { - name = name + boy.substring(j, j + 2); - } else { - name = name + boy.substring(j, j + 1); - } + } else { + int j = random.nextInt(girl.length() - 2); + if (j % 2 == 0) { + name = name + boy.substring(j, j + 2); + } else { + name = name + boy.substring(j, j + 1); + } - } - return name; - } + } + return name; + } - /** - * 随机生成电话号码 - * - * @return - */ - public static String getMobileNo() { - String mobileNo = startMobileNo() + endMobileNo(); - return mobileNo; - } + /** + * 随机生成电话号码 + * + * @return + */ + public static String getMobileNo() { + String mobileNo = startMobileNo() + endMobileNo(); + return mobileNo; + } - // 随机生成电话号码前三个数字 - public static int startMobileNo() { - int[] mobileStart = {139, 138, 137, 136, 135, 134, 159, 158, 157, 150, 151, 152, 188, 130, 131, 132, 156, 155, - 133, 153, 189, 180, 177, 176}; - Random r = new Random(); - ArrayList<Integer> mobileList = new ArrayList<>(); - for (int i = 0; i < mobileStart.length; i++) { - mobileList.add(mobileStart[i]); - } - return mobileList.get(r.nextInt(mobileList.size())); - } + // 随机生成电话号码前三个数字 + public static int startMobileNo() { + int[] mobileStart = {139, 138, 137, 136, 135, 134, 159, 158, 157, 150, 151, 152, 188, 130, 131, 132, 156, 155, + 133, 153, 189, 180, 177, 176}; + Random r = new Random(); + ArrayList<Integer> mobileList = new ArrayList<>(); + for (int i = 0; i < mobileStart.length; i++) { + mobileList.add(mobileStart[i]); + } + return mobileList.get(r.nextInt(mobileList.size())); + } - // 随机生成电话号码后八个数字 - public static String endMobileNo() { - Random r = new Random(); - String temp = ""; - for (int i = 0; i < 8; i++) { - temp += r.nextInt(10); - } - return temp; - } + // 随机生成电话号码后八个数字 + public static String endMobileNo() { + Random r = new Random(); + String temp = ""; + for (int i = 0; i < 8; i++) { + temp += r.nextInt(10); + } + return temp; + } - /** - * 随机生成文字参数length就是文字长度 - * - * @param length - * @return - */ - public static String getChineseString(int length) { - StringBuilder str = new StringBuilder(); - for (int i = 0; i <= length; i++) { - str.append(getRandomChar()); - } - return str.toString(); - } + /** + * 随机生成文字参数length就是文字长度 + * + * @param length + * @return + */ + public static String getChineseString(int length) { + StringBuilder str = new StringBuilder(); + for (int i = 0; i <= length; i++) { + str.append(getRandomChar()); + } + return str.toString(); + } - // 随机生成一个文字 - public static char getRandomChar() { - String str = ""; - int hightPos; // - int lowPos; - Random random = new Random(); - hightPos = (176 + Math.abs(random.nextInt(39))); - lowPos = (161 + Math.abs(random.nextInt(93))); - byte[] b = new byte[2]; - b[0] = (Integer.valueOf(hightPos)).byteValue(); - b[1] = (Integer.valueOf(lowPos)).byteValue(); - try { - str = new String(b, "GBK"); - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); - System.out.println("错误"); - } - return str.charAt(0); - } + // 随机生成一个文字 + public static char getRandomChar() { + String str = ""; + int hightPos; // + int lowPos; + Random random = new Random(); + hightPos = (176 + Math.abs(random.nextInt(39))); + lowPos = (161 + Math.abs(random.nextInt(93))); + byte[] b = new byte[2]; + b[0] = (Integer.valueOf(hightPos)).byteValue(); + b[1] = (Integer.valueOf(lowPos)).byteValue(); + try { + str = new String(b, "GBK"); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + System.out.println("错误"); + } + return str.charAt(0); + } } diff --git a/web-module/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/web-module/src/main/resources/META-INF/additional-spring-configuration-metadata.json index eb015a0..eef6815 100644 --- a/web-module/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/web-module/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -5,6 +5,18 @@ "type": "java.lang.Boolean", "defaultValue": true, "description": "是否自动注入cors过滤器." + }, + { + "name": "spring.web.file-upload.enable", + "type": "java.lang.Boolean", + "defaultValue": false, + "description": "是否自动注入上传文件bean." + }, + { + "name": "spring.web.rich-text.enable", + "type": "java.lang.Boolean", + "defaultValue": false, + "description": "是否开启富文本上传功能." } ] } \ No newline at end of file diff --git a/web-module/src/main/resources/META-INF/spring.factories b/web-module/src/main/resources/META-INF/spring.factories index d3ed6c5..7408b58 100644 --- a/web-module/src/main/resources/META-INF/spring.factories +++ b/web-module/src/main/resources/META-INF/spring.factories @@ -9,4 +9,6 @@ com.thhy.project.webmodule.utils.ResponseUtil,\ com.thhy.project.webmodule.config.WebFileConfig,\ com.thhy.project.webmodule.config.FileUploadProperties,\ -com.thhy.project.webmodule.service.impl.BaseFileServiceImpl \ No newline at end of file +com.thhy.project.webmodule.service.impl.BaseFileServiceImpl,\ +com.thhy.project.webmodule.config.RichTextProperties,\ +com.thhy.project.webmodule.controller.RichTextFileController \ No newline at end of file -- Gitblit v1.9.3