栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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


@EnableKnife4j

@Configuration

public class Knife4jConfig {

@Bean

public Docket createRestApi() {

return new Docket(DocumentationType.OAS_30)

.apiInfo(apiInfo())

.select()

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

.paths(PathSelectors.any())

.build();

}

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”, “不支持当前请求类型”),

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;

private String url;

}

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

@Data

@NoArgsConstructor

@AllArgsConstructor

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

public class CommonResponse implements Serializable {

private static final long serialVersionUID = -1338376281028943181L;

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();

void save(MultipartFile multipartFile);

Resource load(String filename);

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) {

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/860832.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号