springboot下载文件

Posted 老梁讲Java

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot下载文件相关的知识,希望对你有一定的参考价值。

例子

/**
 * 下载假期模板
 */
@ResponseBody
@RequestMapping(value = "/downloadDolidayTemplate.do")
public ResponseEntity downloadDolidayTemplate(Model model, HttpServletRequest request) throws Exception {
    String path = this.getClass().getResource("/").getPath();
    FileInputStream fis = new FileInputStream(path + "conf/template/holidayTemlate.xls");
    byte[] data = new byte[fis.available()];
    fis.read(data);
    HttpHeaders he = new HttpHeaders();
    he.setContentType(MediaType.valueOf("application/x-msdownload"));
    he.set("content-disposition","attachment;filename="+URLEncoder.encode("模板.xls", "UTF-8"));
    return new ResponseEntity<>(data, he, HttpStatus.OK);
}

以上是关于springboot下载文件的主要内容,如果未能解决你的问题,请参考以下文章