我想制作一个rest api控制器(spring boot),当用get请求时将允许我下载一个excel文件。目前我有这个端点:
@RequestMapping(value = "/download.xls", method = RequestMethod.GET)
public ResponseEntity Survey_Reports(@RequestParam(value = "evaluated") String evaluated){
return surveyService.getSurveysFile(evaluated);
}
最终调用这个方法:
public static ResponseEntity getDownloadResponse() {
File file2Upload = new File("Survey_Reports.xls");
Path path = Paths.get(file2Upload.getAbsolutePath());
ByteArrayResource resource = null;
try {
resource = new ByteArrayResource(Files.readAllBytes(path));
} catch (IOException e) {
logger.error("there was an error getting the file bytes ", e);
}
return ResponseEntity.ok()
.contentLength(file2Upload.length())
//this line doesnt seem to work as i set the file format in the controller request mapping
.contentType(MediaType.parseMediaType("application/vnd.ms-excel"))
.body(resource);
}
当我得到 download.xls(作为映射)文件正确时,一切似乎都工作得很好,但现在我想让下载的文件具有一些特定的名称,例如:evaluatedName.xls 或 userDateEndDate.xls 或其他一些东西,是有没有办法编辑响应实体来做到这一点?这样我就不必将映射命名为“download.xls”
在上下文中HttpServletResponse 响应你可以这样做
response.setContentType("application/csv");
response.setHeader("Content-Disposition", "attachment; filename=" + csvName);
and for 响应实体我假设你可以使用这样的东西:
ResponseEntity.ok().header("Content-Disposition","attachment; filename=" + csvName );
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)