附件下载,效果和平时下载东西的模式一样

 

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();  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值