[英]FileSystemResource: how to set relative path
我有这样的项目结构:
和以下控制器:
@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()
但结果一样
使用ClassPathResource
代替FileSystemResource
@GetMapping("/stub_mapping_template")
public FileSystemResource getMappingTemplate(HttpServletResponse response) {
ClassPathResource classPathResource = new ClassPathResource("/stub/mapping_template.csv");
File file = classPathResource.getFile();
InputStream in = new FileInputStream(file);
response.setContentType(....);
response.setHeader("Content-Disposition", "attachment; filename=" + file.getName());
response.setHeader("Content-Length", String.valueOf(file.length()));
FileCopyUtils.copy(in, response.getOutputStream());
response.flushBuffer();
}
或者,如果你想坚持使用 FileSystemResource,你可以这样做:
new FileSystemResource("src/main/resources/stub/mapping_template.csv");
也许 FileSystemResource 需要您文件的完整 url。 其中一种技术是,您将路径从“我的电脑”复制到您的 PC 上。 并继续从网址的开头删除
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.