读取application.yml 配置信息
读取顺序 网上太多了,就复制过来了
这里说了四种方式可以把配置文件放到外部的。
第一种是在jar包的同一目录下建一个config文件夹,然后把配置文件放到这个文件夹下;
第二种是直接把配置文件放到jar包的同级目录;
第三种在classpath下建一个config文件夹,然后把配置文件放进去;
第四种是在classpath下直接放配置文件。
这四种方式的优先级是从一到四一次降低的。
person: person-name: 张三 person-age: 10
//读取application.yml配置信息 @Component @ConfigurationProperties(prefix = "person") public class TestConfig2 { @Value("${person.person-name}") private String name; @Value("${person.person-age}") private String age; @Override public String toString() { return "TestConfig2{" + "name='" + name + '\'' + ", age='" + age + '\'' + '}'; } }
读取resources下面的配置信息 例如下面有一个config.yml
username: zhangsan pwd: 123456 name: 张三
//读取classpath配置信息 @Component @PropertySource(value = {"classpath:config.yml"},encoding = "utf-8") public class TestConfig { @Value("${username}") private String username; @Value("${pwd}") private String pwd; @Value("${name}") private String name; @Override public String toString() { return "TestConfig{" + "username='" + username + '\'' + ", pwd='" + pwd + '\'' + ", name='" + name + '\'' + '}'; } }
读取外部指定位置的配置信息
username2: file:E:/study/java/config/other.yml pwd: 1234569 name: 张三d
//读取指定位置配置信息 @Component //@PropertySource(value ={"file:${user.dir}/config/custom.properties","file:${user.dir}/config/custom_prison.properties"}, ignoreResourceNotFound = true) @PropertySource(value ={"file:E:/study/java/config/other.yml"}, ignoreResourceNotFound = true,encoding = "utf-8") public class TestConfig3 { @Value("${username2}") private String username; @Value("${pwd}") private String pwd; @Value("${name}") private String name; @Override public String toString() { return "TestConfig3{" + "username='" + username + '\'' + ", pwd='" + pwd + '\'' + ", name='" + name + '\'' + '}'; } }
上传图片到指定目录和浏览图片
- 创建配置文件config.yml
staticAccessPath: /api/file/** uploadFolder: E:/study/java/uploadFiles/ min: 1 max: 5
- 创建配置Bean
@Data @Component @PropertySource(value = {"classpath:config.yml"},encoding = "utf-8") public class FileConfig { @Value("${staticAccessPath}") private String staticAccessPath; @Value("${uploadFolder}") private String uploadFolder; @Value("${min}") private int min; @Value("${max}") private int max; @Override public String toString() { return "FileConfig{" + "staticAccessPath='" + staticAccessPath + '\'' + ", uploadFolder='" + uploadFolder + '\'' + ", min='" + min + '\'' + ", max='" + max + '\'' + '}'; } }
- 创建Config
@Configuration public class UploadFileConfig { @Autowired FileConfig config; @Bean MultipartConfigElement multipartConfigElement() { MultipartConfigFactory factory = new MultipartConfigFactory(); factory.setLocation(config.getUploadFolder()); //文件最大 factory.setMaxFileSize(DataSize.ofMegabytes(config.getMin())); // 设置总上传数据总大小 factory.setMaxRequestSize(DataSize.ofMegabytes(config.getMin())); return factory.createMultipartConfig(); } }
- 配置WebMvcConfg
//http://127.0.0.1:8089/ityustudy/api/file/html169ecca9-375c-471b-abdf-72531f674d8b.jpg 浏览图片 public void addResourceHandlers(ResourceHandlerRegistry registry){ registry.addResourceHandler(fileConfig.getStaticAccessPath()).addResourceLocations("file:" + fileConfig.getUploadFolder()); }
5.上传文件
@ApiOperation(value = "上传多张图片") @PostMapping("/uploadImgs") @ResponseBody public R<List<String>> uploadImgs(MultipartFile[] file) { if (file==null||file.length == 0) { return RUtil.error(0, "请上传图片"); } List<String> array = new ArrayList(); // 要上传的目标文件存放路径 for (MultipartFile f : file) { String originalFilename = getSuffix(f.getOriginalFilename()); String s = "html"+ UUID.randomUUID() + originalFilename; if (Utils.upload(f, fileConfig.getUploadFolder(), s)) { array.add(Utils.getUrl(fileConfig.getStaticAccessPath().replace("**","") + s, 0)); } else { return RUtil.error(0, "上传失败"); } } return RUtil.ok(array); }