【发布时间】:2016-10-22 10:10:31
【问题描述】:
我正在创建一个简单的 Spring Boot 应用程序,我试图在其中访问外部 config.properties 文件。
IndexController.java
@Controller
public class IndexController {
XmlOperation xmlOperation = new XmlOperation();
@RequestMapping("/")
public String greeting() {
return "greeting";
}
@RequestMapping(params = "btnOpen", method = RequestMethod.POST)
public String uploadFile(@RequestParam("file") MultipartFile file, Model model) {
try {
InputStream is = file.getInputStream();
model.addAttribute("fileContent", xmlOperation.readXml(is));
} catch (IOException e) {
System.out.println(e.getMessage());
}
return "greeting";
}
}
XmlOperation.java
@PropertySource("classpath:config.properties")
public class XmlOperation {
@Autowired
Environment env;
public String readXml(InputStream is) throws IOException {
System.out.println(env.getProperty("filepath"));
StringWriter writer = new StringWriter();
IOUtils.copy(is, writer, StandardCharsets.UTF_8);
String fileContent = writer.toString();
return fileContent;
}
config.properties 文件位于src/main/resources。我无法从属性文件中获取值。
任何帮助将不胜感激......
【问题讨论】:
标签: spring-boot properties-file