@PostMapping("/attachment-download") @ApiOperation(value = "附件下载") public void download( Integer id,HttpServletResponse response) throws IOException { ledgerService.download(id,response); }
void download(Integer id, HttpServletResponse response)throws IOException;
@Override public void download(Integer id, HttpServletResponse response) throws IOException{ Attachment attachment=attachmentMapper.selectByid(id); byte[] content=attachment.getFileContent(); if (content==null){ throw new RuntimeException("没有文件"); } response.setContentType("application/octet-stream;charset=utf-8"); response.setHeader("Content-Disposition", "attachment; filename=\"" + new String(attachment.getFileName().getBytes("UTF-8"), "ISO-8859-1") + "\""); OutputStream out =response.getOutputStream(); out.write(content); out.flush(); }