SpringMVC生成任意文件,访问链接即下载

原理上讲就是返回的 ResponseEntity<byte[]> 形式的值就可以了

复制代码
@RequestMapping("/api/watermark_download")
    public ResponseEntity<byte[]> returnWatermarkImage(HttpServletRequest request, HttpSession session) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        try {
            File file=new File("文件路径");byte[] test=FileUtils.readFileToByteArray(file);
            return new ResponseEntity<byte[]>(test, headers, HttpStatus.CREATED);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResponseEntity<byte[]>(null, headers, HttpStatus.CREATED);
    }
复制代码

 

posted @   曲高终和寡  阅读(1929)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示