【发布时间】:2017-12-25 09:22:14
【问题描述】:
我有这样的项目结构:
和以下控制器:
@RestController
public class StubController {
@GetMapping("/stub_mapping_template")
public FileSystemResource getMappingTemplate() {
return new FileSystemResource("/stub/mapping_template.csv");
}
}
但是当我在浏览器中打开时
localhost:8080/stub_mapping_template
没有下载。
在调试中我尝试输入:
new FileSystemResource("/stub/mapping_template.csv").exists()
它返回false。
我试着写:
new FileSystemResource("stub/mapping_template.csv").exists()
但结果相同
【问题讨论】:
-
你好,在一个sevlet中,你需要使用getServletContext()来找到你的项目的根,例如:Paths.get(getServletContext().getRealPath("web-inf/myfile");
-
它不是文件系统资源。在运行时,mapping_template.csv 不在文件系统上。它在您部署的 jar 文件中,可由类加载器加载。
标签: java spring resources filesystems