gst*_*low 2 java io spring file spring-mvc
我写了以下弹簧控制器:
@RequestMapping(method = RequestMethod.GET, value = "/getVideoIcon")
@ResponseBody
public FileSystemResource getVideoIcon() throws IOException {
return new FileSystemResource(new File("/resources/images/video_icon.png"));
}
Run Code Online (Sandbox Code Playgroud)
该文件位于 src\main\webapp\resources\images
当我调用控制器时,我得到以下错误:
HTTP Status 500 - resources\images\video_icon.png (The system cannot find the path specified)
Run Code Online (Sandbox Code Playgroud)
请帮助纠正我的错误.
如果您需要获取原始项目的路径,则应该使用 getServletContext().getRealPath
@RequestMapping(method = RequestMethod.GET, value = "/getVideoIcon")
@ResponseBody
public FileSystemResource getVideoIcon(HttpServletRequest request) throws IOException {
String path = request.getSession().getServletContext().getRealPath("/resources/images")+"/video_icon.png";
return new FileSystemResource(new File(path));
}
Run Code Online (Sandbox Code Playgroud)
如果需要检查path变量的含义,请System.out.println(path);在return语句之前使用.如果您在尝试上述代码后仍然遇到错误消息,请随时询问..