做网站运营还是翻译,中国建设手机银行app下载,网站关键字字数,浦城 建网站Spring Boot自定义配置项
配置文件
在application.properties文件添加需要的配置 比如#xff1a;
file.pathD:\\flies\\springboot\\ConfigurationProperties 注解
使用注解ConfigurationProperties将配置项和实体Bean关联起来#xff0c;实现配置项和实体类字段的关联
file.pathD:\\flies\\springboot\\ConfigurationProperties 注解
使用注解ConfigurationProperties将配置项和实体Bean关联起来实现配置项和实体类字段的关联读取配置文件数据。
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;Data
Component
ConfigurationProperties(prefix file)
public class FileConfig {private String path;
}使用
获取配置信息
FileConfig fileConfig new FileConfig();
// 文件保存目录
String filePath fileConfig.getPath();PostMapping(/upload/)ResponseBodypublic Response upload(MultipartFile file) {// 验证是否有文件if(file null || file.isEmpty()){return Response.newFail(Upload failed, please select file,400);}FileConfig fileConfig new FileConfig();// 文件保存目录String filePath fileConfig.getPath();// 验证文件夹File folder new File(filePath);if (!folder.exists()) {folder.mkdirs();}// 文件名String fileName UUID.randomUUID() file.getOriginalFilename();filePath filePath fileName;File saveFile new File(filePath);try {file.transferTo(saveFile);return Response.newSuccess(Upload successful);} catch (IOException e) {e.printStackTrace();return Response.newFail(Upload failed,50001);}}