项目启动后创建文件夹:
@Component
public class StartInit implements CommandLineRunner {
@Resource
private UploadProperties uploadProperties;
@Override
public void run(String... args) {
String basePath = uploadProperties.getBasePath();
File folder = new File(basePath);
if (!folder.exists()){
folder.mkdir();
System.out.println("folder created successfully");
}else {
System.out.println("folder already exists");
}
}
}
UploadProperties
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Map;
@Component
@ConfigurationProperties("upload")
public class UploadProperties {
// 获取存放位置
private Map<String, String> localtion;
public Map<String, String> getLocaltion() {
return localtion;
}
public void setLocaltion(Map<String, String> localtion) {
this.localtion = localtion;
}
public String getBasePath() {
String location = "";
String os = System.getProperty("os.name");
if (os.toLowerCase().startsWith("win")) {
location = this.getLocaltion().get("windows");
} else {
location = this.getLocaltion().get("linux");
}
return location;
}
}
application-local.yaml
upload:
localtion:
windows: /d:/ems-images/
linux: /data/ems-images/