凌风515
时不我待,只争朝夕

@GetMapping("/downloadFxRateTemplate")
public void downloadFxRateTemplate(HttpServletResponse response) throws IOException {
// 假设文件位于 src/main/resources/files 目录下
Resource resource = new ClassPathResource("template/fxRate/FX template.xls");
// 设置响应头,使浏览器识别为附件下载
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=\"" + resource.getFilename() + "\"");

// 创建文件输入流
try (InputStream inputStream = resource.getInputStream();
OutputStream outputStream = response.getOutputStream()) {
// 读取文件并写入到响应输出流中
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
} catch (IOException e) {
log.info("DcFxRateController downloadFxRateTemplate error", e);

posted on 2025-01-20 17:16  凌风515  阅读(12)  评论(0)    收藏  举报