我的项目结构是这样的:

和下面的控制器:
@RestController
public class StubController {
@GetMapping("/stub_mapping_template")
public FileSystemResource getMappingTemplate() {
return new FileSystemResource("/stub/mapping_template.csv");
}
}但当我在浏览器中打开时
localhost:8080/stub_mapping_template不下载任何内容。
在debug中,我尝试键入:
new FileSystemResource("/stub/mapping_template.csv").exists()它返回false。
我试着写下:
new FileSystemResource("stub/mapping_template.csv").exists()但结果是一样的
发布于 2017-12-25 17:27:42
使用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();
}发布于 2020-09-02 02:08:03
或者,如果你想继续使用FileSystemResource,你可以这样做:
new FileSystemResource("src/main/resources/stub/mapping_template.csv");发布于 2017-12-25 17:37:10
也许FileSystemResource会获取你的文件的完整url。其中一种方法是,将路径从pc上的“我的电脑”复制。并从url的开头继续删除。
https://stackoverflow.com/questions/47967331
复制相似问题