我有这样的项目结构:
和以下控制器:
@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()
但结果相同
代替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] 删除。
我来说两句