FileSystemResource:如何设置相对路径

gst*_*low 0 java filesystems resources spring

我有这样的项目结构:

在此输入图像描述

和以下控制器:

@RestController
public class StubController {

    @GetMapping("/stub_mapping_template")
    public FileSystemResource getMappingTemplate() {
        return new FileSystemResource("/stub/mapping_template.csv");
    }
}
Run Code Online (Sandbox Code Playgroud)

但是当我在浏览器中打开时

localhost:8080/stub_mapping_template
Run Code Online (Sandbox Code Playgroud)

没有任何下载。

在调试中我尝试输入:

new FileSystemResource("/stub/mapping_template.csv").exists()
Run Code Online (Sandbox Code Playgroud)

它返回了false

我试着写:

new FileSystemResource("stub/mapping_template.csv").exists()
Run Code Online (Sandbox Code Playgroud)

但结果是一样的

pvp*_*ran 5

而不是FileSystemResource使用ClassPathResource

 @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();
}
Run Code Online (Sandbox Code Playgroud)