我在代码中生成csv,生成需要一些时间。因此,一旦生成csv文件,我将发送带有链接的电子邮件。当我点击它时,找不到404错误。当我在html中有相同的链接时,我可以下载它。任何洞察或样本参考
Sample Link -http://localhost:9090/api/report/file?fileName=filename.csv
下载报告的Java代码
@RequestMapping(value = "api/report/file")
public void downloadCSV(HttpServletResponse response, @RequestParam("fileName") String fileName) throws IOException {
File file = new File(fileName);
InputStream is = new FileInputStream(file);
response.setContentType("application/octet-stream");
// Response header
response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"");
// Read from the file and write into the response
OutputStream os = response.getOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
os.flush();
os.close();
is.close();
}
答案 0 :(得分:0)
向此映射添加GET方法:@RequestMapping(value = "api/report/file")