Commit 66a5d5dc authored by “xuhd”'s avatar “xuhd”

解决支持文件类型过滤

parent 548a79c3
...@@ -31,6 +31,8 @@ public enum ResultCode implements IResultCode, Serializable { ...@@ -31,6 +31,8 @@ public enum ResultCode implements IResultCode, Serializable {
TOKEN_INVALID_OR_EXPIRED("A0230", "token无效或已过期"), TOKEN_INVALID_OR_EXPIRED("A0230", "token无效或已过期"),
// 请求参数错误,参数校验不通过 // 请求参数错误,参数校验不通过
REQUEST_PARAM_ERROR("Q0001", "请求参数错误"), REQUEST_PARAM_ERROR("Q0001", "请求参数错误"),
//文件类型不匹配
FILE_TYPE_ERROR("F0001", "文件类型不支持"),
// 请求参数错误,参数校验不通过 // 请求参数错误,参数校验不通过
SRV_ORGAN_NOT_QUERY("S0001", "未查询到数据"), SRV_ORGAN_NOT_QUERY("S0001", "未查询到数据"),
/** /**
......
...@@ -4,11 +4,14 @@ import lombok.Data; ...@@ -4,11 +4,14 @@ import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.List;
@Component @Component
@Data @Data
@ConfigurationProperties(prefix = "file.upload") @ConfigurationProperties(prefix = "file.upload")
public class FileUploadConfig { public class FileUploadConfig {
private String path; private String path;
private List<String> type;
} }
...@@ -38,7 +38,16 @@ public class FileUploadController { ...@@ -38,7 +38,16 @@ public class FileUploadController {
if (null == uploadType || null == file) { if (null == uploadType || null == file) {
return Result.failed(ResultCode.REQUEST_PARAM_ERROR); return Result.failed(ResultCode.REQUEST_PARAM_ERROR);
} }
//过滤上传资料类型
String originalFilename = file.getOriginalFilename(); String originalFilename = file.getOriginalFilename();
//文件类型
String fileType = originalFilename.substring(file.getOriginalFilename().lastIndexOf("."));
List<String> fileTypes = fileUploadConfig.getType();
log.info("支持图片类型:" + fileTypes+",上传的图片类型:"+fileType);
if(!fileTypes.contains(fileType)){
log.info("上传文件错误");
return Result.failed(ResultCode.FILE_TYPE_ERROR);
}
String realPath = FileUtils.uploadFile(file, fileUploadConfig.getPath()); String realPath = FileUtils.uploadFile(file, fileUploadConfig.getPath());
log.info("文件上传成功:{}", realPath); log.info("文件上传成功:{}", realPath);
Long attachId = attachService.saveFileInfo(originalFilename, realPath, uploadType); Long attachId = attachService.saveFileInfo(originalFilename, realPath, uploadType);
......
...@@ -35,3 +35,10 @@ mybatis-plus: ...@@ -35,3 +35,10 @@ mybatis-plus:
file: file:
upload: upload:
path: E:/workspace/upload path: E:/workspace/upload
type:
- .JPG
- .JPEG
- .jpg
- .jpeg
- .png
- .PNG
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment