图片下载
public String fileDownload(MultipartFile file,Long attachmentId,HttpServletResponse response) throws Exception {
//需求,通过id找到文件,读取下载到桌面
//1.通过id找到图片文件
FndAppAttachment date = fndAppAttachmentMapper.getByIdAtmAttachment(attachmentId);
//2.读取图片
InputStream is = new ByteArrayInputStream(date.getContent());
response.setContentType("image/**");
try {
//通过文件流将图片传给前端
ServletOutputStream out = response.getOutputStream();
int len = 0;
byte[] buf = new byte[1024];
//使用一个输入流从buffer里吧数据读出来
while ((len=is.read(buf,0,1024))!=1) {
//用输出流往buffer里写数据,中间参数代表从哪个位置读,len代表读取的长度
out.write(buf,0,len);
}
out.flush();
out.close();
return "下载成功";
}catch(IOException e) {
e.getMessage();
}
return "下载失败";