下载文件java spring rest api
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了下载文件java spring rest api,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1428字,纯文字阅读大概需要3分钟。
内容图文
![下载文件java spring rest api](/upload/InfoBanner/zyjiaocheng/811/6c554a928ad340d5b123638ef8eaf1b6.jpg)
我想制作一个休息api控制器(弹簧启动),当用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(作为映射)文件correclty,但现在我想让下载的文件有一些特定的名称,如:evaluateName.xls或userDateEndDate.xls或其他一些东西,是有没有办法编辑响应实体呢?这样我就不必将映射命名为“download.xls”
解决方法:
在HttpServletResponse响应的上下文中,您可以这样做
response.setContentType("application/csv");
response.setHeader("Content-Disposition", "attachment; filename=" + csvName);
对于ResponseEntity,我假设你可以使用这样的东西:
ResponseEntity.ok().header("Content-Disposition","attachment; filename=" + csvName );
内容总结
以上是互联网集市为您收集整理的下载文件java spring rest api全部内容,希望文章能够帮你解决下载文件java spring rest api所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。