1、数据库是blob类型,存放的base64格式(data:text/plain;base64,PGJ1dHRvbiBuZ2Ytc2VsZWN0PSJ1cGxvYWRGaWxlcygkZmlsZXMsICRpbnZhbGlkR)
2、实体对象是byte[] (private byte[] fileContent;)
伪代码如下:
List<CertificateAttachment> certificateAttachmentList = certificateAttachmentService.selectListByQueryWithBlobs(certificateAttachmentQuery);
String fileName=certificateAttachmentList.get(0).getFileName();
//从数据库取值
byte[] b=certificateAttachmentList.get(0).getFileContent();
//转字符串(进行字符串截取,否则,解码生成图片,图片不正确,不能被正常打开)
String t = new String(b);//注意,这个地方输出的t内容和数据库字段里面存放的一模一样的。
String imgStr=t.split("base64,")[1];
String imgType=t.split(";base64")[0].split(":")[1];
//解码,生成图片
byte[] bImage = Base64Utils.decodeFromString(imgStr);
response.reset();
response.setContentType(imgType);// "image/png"
response.setHeader( "Content-Disposition ","attachment;filename="+fileName);
ServletOutputStream out=response.getOutputStream();
out.write(bImage);
out.flush();
out.close();
response.flushBuffer();