Attachment download, the effect is the same as the usual mode of downloading things

 

1. The database is blob type, stored in base64 format (data:text/plain;base64,PGJ1dHRvbiBuZ2Ytc2VsZWN0PSJ1cGxvYWRGaWxlcygkZmlsZXMsICRpbnZhbGlkR)

2. The entity object is byte[] (private byte[] fileContent;)

 

The pseudo code is as follows:

List<CertificateAttachment> certificateAttachmentList = certificateAttachmentService.selectListByQueryWithBlobs(certificateAttachmentQuery);

String fileName=certificateAttachmentList.get(0).getFileName();

// get value from database

byte[] b=certificateAttachmentList.get(0).getFileContent();

//Turn the string (to intercept the string, otherwise, the decoding will generate a picture, the picture is incorrect and cannot be opened normally)

String t = new String(b); //Note that the content of t output in this place is exactly the same as that stored in the database field.

String imgStr=t.split("base64,")[1];

String imgType=t.split(";base64")[0].split(":")[1];

//decode, generate image

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

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326688776&siteId=291194637