FileSystemResource:如何设置相对路径

gstackoverflow

我有这样的项目结构:

在此处输入图片说明

和以下控制器:

@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()

但结果相同

pvpkiran

代替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();
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章