Spring MVC提供http接口供下载文件

来源:互联网 发布:手机如何查看淘宝粉丝 编辑:程序博客网 时间:2024/05/29 13:01

废话不多说,直接上源码:

@Controllerpublic class DownLoadController {    @RequestMapping(value="/zyg/download/lemmainfo")    public void downloadResource(@RequestParam(value = "fileName", required = true) String fileName, HttpServletResponse response) {        String dataDirectory = "/data/denglinjie/everydayLemmaInfo/";        Path file = Paths.get(dataDirectory, fileName);        if (Files.exists(file)) {            response.setContentType("application/x-gzip");            try {                response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));                Files.copy(file, response.getOutputStream());            } catch (IOException ex) {                ex.printStackTrace();            }        }    }}


0 1