我想像这样从 Controller 读取文件:
@Controller
public class HomeController {
@RequestMapping(value = {"/"}, method = RequestMethod.GET)
public String showHomePage(ModelMap model) throws IOException, SAXException, ParserConfigurationException {
List<User> users = XmlParser.parse("Sum_and_clients.xml");
return "home";
}
但是当我启动服务器时,它想要从 apache-tomcat\bin 下载它。尽管 Sum_and_clients.xml 位于 HomeController.java 附近。如何将这个绝对的 tomcat 路径更改为我项目中的路径?
最佳答案
我解决了我的问题。
URL url = HomeController.class.getResource("Sum_and_clients.xml");
File file = new File(url.getPath())
这个文件应该放在 [{你的项目名}\target\{你的项目名}\WEB-INF\classes\{你的包到 HomeController}
关于java - 将文件读入 Spring Controller 。使用相对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29230790/