企业级spring-boot案例-Spring Boot 上传文件(图片)

企业级spring-boot案例-Spring Boot 上传文件(图片),第1张

[](()3. 添加Knife4j配置类


@EnableKnife4j

@Configuration

public class Knife4jConfig {

/**

  • 创建Docket对象

  • @return Docket

*/

@Bean

public Docket createRestApi() {

return new Docket(DocumentationType.OAS_30)

.apiInfo(apiInfo())

.select()

.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))

.paths(PathSelectors.any())

.build();

}

/**

  • API基础信息

  • @return ApiInfo

*/

private ApiInfo apiInfo() {

return new ApiInfoBuilder()

.title(“Knife4j-API接口文档”)

.description(“API接口文档”)

.contact(new Contact(“JourWon”, “https://thinkwon.blog.csdn.net/”, “JourWon@163.com”))

.version(“1.0.0”)

.build();

}

}

[](()4. 添加枚举与实体类


[](()4.1 响应编码枚举

@Getter

@AllArgsConstructor

public enum CommonResponseCodeEnum {

/**

  • 成功

*/

SUCCESS(“00000”, “成功”),

/**

  • 用户请求参数错误

*/

REQUEST_PARAMETER_ILLEGAL(“A0400”, “用户请求参数错误”),

/**

  • 访问未授权

*/

UNAUTHORIZED_ACCESS(“A0301”, “访问未授权”),

/**

  • 不支持当前请求类型

*/

NONSUPPORT_REQUEST_TYPE(“A0444”, “不支持当前请求类型”),

/**

  • 用户id不存在

*/

USER_ID_NOT_EXIST(“A0445”, “用户id不存在”),

/**

  • 数据库字段重复

*/

DATABSE_FIELD_DUPLICATE(“A0446”, “数据库字段重复”),

/**

  • 系统执行出错

*/

SYSTEM_EXCEPTION(“B0001”, “系统执行出错”),

/**

  • 系统执行超时

*/

SYSTEM_EXECUTION_TIMEOUT(“B0100”, “系统执行超时”),

;

/**

  • 响应编码

*/

private final String code;

/**

  • 响应信息

*/

private final String message;

}

[](()4.2 上传文件信息

@Data

@NoArgsConstructor

@AllArgsConstructor

public class UploadFile {

/**

  • 文件名

*/

private String fileName;

/**

  • 文件url

*/

private String url;

}

[](()4.3 统一返回前端的响应对象

@Data

@NoArgsConstructor

@AllArgsConstructor

@ApiModel(value = “CommonResponse-统一返回前端的响应对象”)

public class CommonResponse implements Serializable {

private static final long serialVersionUID = -1338376281028943181L;

/**

  • MDC_KEY

*/

public static final String MDC_KEY = “traceId”;

@ApiModelProperty(value = “响应编码”)

private String code;

@ApiModelProperty(value = “响应信息”)

private String message;

@ApiModelProperty(value = “业务数据”)

private T data;

@ApiModelProperty(value = “traceId”)

private String traceId = MDC.get(MDC_KEY);

@ApiModelProperty(value = “响应日期时间”)

@JsonFormat(timezone = “GMT+8”, pattern = “yyyy-MM-dd HH:mm:ss.SSS”)

private LocalDateTime localDateTime = LocalDateTime.now();

public CommonResponse(String code, String message) {

this.code = code;

this.message = message;

}

public CommonResponse(CommonResponseCodeEnum commonResponseCodeEnum) {

this.code = commonResponseCodeEnum.getCode();

this.message = commonResponseCodeEnum.getMessage();

}

public CommonResponse(T data) {

this.code = CommonResponseCodeEnum.SUCCESS.getCode();

this.message = CommonResponseCodeEnum.SUCCESS.getMessage();

this.data = data;

}

public CommonResponse(CommonResponseCodeEnum commonResponseCodeEnum, T data) {

this.code = commonResponseCodeEnum.getCode();

this.message = commonResponseCodeEnum.getMessage();

this.data = data;

}

public static CommonResponse success() {

return new CommonResponse<>(CommonResponseCodeEnum.SUCCESS);

}

public static CommonResponse success(String message) {

return new CommonResponse<>(CommonResponseCodeEnum.SUCCESS.getCode(), message);

}

public static CommonResponse success(T data) {

return new CommonResponse<>(CommonResponseCodeEnum.SUCCESS, data);

}

public static CommonResponse success(CommonRespon 《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】 seCodeEnum commonResponseCodeEnum, T data) {

return new CommonResponse<>(commonResponseCodeEnum, data);

}

public static CommonResponse failure(CommonResponseCodeEnum commonResponseCodeEnum) {

return new CommonResponse<>(commonResponseCodeEnum);

}

public static CommonResponse failure(CommonResponseCodeEnum commonResponseCodeEnum, T data) {

return new CommonResponse<>(commonResponseCodeEnum, data);

}

}

[](()5. 文件上传接口与实现类


[](()5.1 文件上传接口

public interface FileStorageService {

/**

  • 初始化方法,创建文件夹

*/

void init();

/**

  • 保存文件

  • @param multipartFile

*/

void save(MultipartFile multipartFile);

/**

  • 根据文件名加载文件

  • @param filename

  • @return

*/

Resource load(String filename);

/**

  • 加载所有的文件

  • @return

*/

Stream load();

/**

  • 递归删除文件

*/

void clear();

}

[](()5.2 文件上传接口实现类

@Service

public class FileStorageServiceImpl implements FileStorageService {

private final Path path = Paths.get(“fileStorage”);

@Override

public void init() {

try {

if (!Files.exists(path)) {

Files.createDirectory(path);

}

} catch (IOException e) {

throw new RuntimeException(“Could not initialize folder for upload!”);

}

}

@Override

public void save(MultipartFile multipartFile) {

try {

Files.copy(multipartFile.getInputStream(), this.path.resolve(multipartFile.getOriginalFilename()));

} catch (IOException e) {

throw new RuntimeException(“Could not store the file. Error:” + e.getMessage());

}

}

@Override

public Resource load(String filename) {

Path file = path.resolve(filename);

try {

Resource resource = new UrlResource(file.toUri());

if (resource.exists() || resource.isReadable()) {

return resource;

} else {

throw new RuntimeException(“Could not read the file.”);

}

} catch (MalformedURLException e) {

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/801219.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-07
下一篇 2022-05-07

发表评论

登录后才能评论

评论列表(0条)

保存