1.application.xml
config:
app-home: ${SECURITYDOC_HOME:E:\fagui}
2…defaultConfig.java
package com.beagledata.gaea.securitydoc.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
/**
-
Created by liulu on 2019/8/12.
/
@Configuration
@ConfigurationProperties(prefix = “config”)
public class DefaultConfigs {
/*- 项目home
*/
private String appHome;
public String getAppHome() {
return appHome;
}public void setAppHome(String appHome) {
this.appHome = appHome;
}@Override
public String toString() {
StringBuilder sb = new StringBuilder(“DefaultConfigs{”);
sb.append(“appHome=’”).append(appHome).append(’’’);
sb.append(’}’);
return sb.toString();
}
}
3.ResourceResolver.java
package com.beagledata.gaea.securitydoc.common; - 项目home
import com.beagledata.gaea.securitydoc.config.DefaultConfigs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.MultipartProperties;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.io.File;
/**
-
Created by liulu on 2019/7/23.
*/
@Component
public class ResourceResolver {
@Autowired
private DefaultConfigs configs;
@Autowired
private MultipartProperties multipartProperties;@PostConstruct
public void init() {
File tmpDir = new File(getTagsPath());
if (!tmpDir.exists()) {
tmpDir.mkdirs();
}
}/**
- @return 获取标签列表路径
*/
public String getTagsPath() {
return new File(configs.getAppHome(), “tags”).getAbsolutePath();
}
}
4.使用路径
public Result<List> tagsList() {
List list = new ArrayList<>();
try {
FileReader fr = new FileReader(resourceResolver.getTagsPath() + “/” + “tag.txt”);
BufferedReader bf = new BufferedReader(fr);
String str;
// 按行读取字符串
while ((str = bf.readLine()) != null) {
list.add(str);
}
bf.close();
fr.close();
} catch (IOException e) {
logger.info(“获取tags内容失败” + e);
e.printStackTrace();
}
return Result.newSuccess().withData(list);
}
- @return 获取标签列表路径