浏览代码

健康小屋-文件下载

huangzhiyong 6 年之前
父节点
当前提交
2241036576

+ 30 - 0
svr/svr-healthy-house/src/main/java/com/yihu/jw/healthyhouse/controller/dfs/FastDFSController.java

@ -371,4 +371,34 @@ public class FastDFSController extends EnvelopRestEndpoint {
    }
    @GetMapping("/down")
    @ApiOperation(value = "文件下载")
    public void down(HttpServletResponse response,
                     @ApiParam(name = "fileName", value = "文件名", required = true)
                     @RequestParam(value = "fileName") String fileName) throws IOException {
        response.setHeader("Pragma", "No-cache");//设置响应头信息,告诉浏览器不要缓存此内容
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expire", 0);
        try {
            // 由于是读取本机的文件,file是一定要加上的, path是在application配置文件中的路径
            Resource resource = resourceLoader.getResource("file:" + img_path1 + fileName);
            File file = resource.getFile();
            FileInputStream fis;
            fis = new FileInputStream(file);
            long size = file.length();
            byte[] temp = new byte[(int) size];
            fis.read(temp, 0, (int) size);
            fis.close();
            byte[] data = temp;
            OutputStream out = response.getOutputStream();
            out.write(data);
            out.flush();
            out.close();
        } catch (Exception e) {
            throw e;
        }
    }
}