Home > Back-end >  JAVA BLOB download read from the database
JAVA BLOB download read from the database

Time:09-16

 @ Path ("/download ") 
@ POST
@ Produces (MediaType APPLICATION_OCTET_STREAM)
Public Response download (@ FormParam (dar) String dar) {
List List=ndsbBLOImpl. QueryFileById (dar);
NdsbQyjbxxDTO dto=list. Get (0);
String FJMC=dto. GetFjmc ();//attachment name
Byte FJNR []=dto. GetFjnr ();//attachment content
ResponseBuilder builder=Response. Ok (FJNR, "application/x - download; charset=utf-8");
Try {
Builder. The header (" the Content - the Disposition ", "legislation; Filename="+ URLEncoder. Encode (FJMC," utf-8 ") + "");
} the catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
The Response the Response=builder. The build ();
return response;
}

Download file has been 1 KB, open the display as follows:

CodePudding user response:

You should read from a database is a byte array, use the form of a stream to return
 @ Path ("/download ") 
@ POST
@ Produces (MediaType APPLICATION_OCTET_STREAM)
Public Response download (@ FormParam (dar) String dar) {
List List=ndsbBLOImpl. QueryFileById (dar);
NdsbQyjbxxDTO dto=list. Get (0);
String FJMC=dto. GetFjmc ();//attachment name
Byte FJNR []=dto. GetFjnr ();//attachment content
StreamingOutput stream=new StreamingOutput () {//implementation stream
@ Override
Public void write (OutputStream output) throws IOException, WebApplicationException {
Try {
The output. The write (FJNR);//read the database output to the stream of bytes
} the catch (Exception e) {
Throw new WebApplicationException (e);
}
}
};
//ResponseBuilder builder=Response. Ok (FJNR, "application/x - download; charset=utf-8");
ResponseBuilder builder=Response. Ok (stream);//the stream back to
Try {
Builder. The header (" the Content - the Disposition ", "legislation; Filename="+ URLEncoder. Encode (FJMC," utf-8 ") + "");
} the catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
The Response the Response=builder. The build ();
return response;
}
  • Related