我想从控制器中读取文件,如下所示:
@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路径更改为我项目中的路径?
答案 0 :(得分:0)
我解决了我的问题。
URL url = HomeController.class.getResource("Sum_and_clients.xml");
File file = new File(url.getPath())
此文件应放入[{Your project name} \ target \ {Your project name} \ WEB-INF \ classes \ {Your package to HomeController}