Task:
The content field of Uos.docfile is the Longblob type, which can be downloaded to a file stored in this field via a Web-click link.
1. Controller code:
@RequestMapping ("/downloaddocument") PublicModelandview downloaddocument (httpservletrequest request,httpservletresponse response) {Try { //Take ParametersString id=request.getparameter ("id"); String filename=request.getparameter ("filename"); //Set ResposneResponse.reset (); Response.setheader ("Content-disposition", "attachment; Filename= "+filename); Response.setcontenttype ("Text/x-plain"); //get the output streamServletoutputstream out =Response.getoutputstream (); //Copy the output stream from the databaseBytearrayoutputstream Byteoutputstream =NewBytearrayoutputstream (4096); Service.copydocumentoutputstream (ID, byteoutputstream); Logger.info ("Call Posservice.copydocumentoutputstream successfully."); //Conversions byte[] bt =NULL; BT=Byteoutputstream.tobytearray (); //write output to clientOut.write (BT); Out.flush (); Out.close (); return NULL; } Catch(Exception e) {e.printstacktrace (); Logger.error (e); Request.setattribute ("Error", E.getclass ()); Request.setattribute ("Reason", E.getmessage ()); Stacktraceelement[] Arr=E.getstacktrace (); Request.setattribute ("Stacktraceelements", arr); return NewModelandview ("pages/error/index.jsp"); } }
2.Service code, also here is just relay
Public void Copydocumentoutputstream (finalfinalthrows exception{ Getposdao (). Copydocumentoutputstream (ID, OS); }
3.DAO code, here is the real code
Public voidCopydocumentoutputstream (FinalString ID,FinalOutputStream OS)throwsexception{FinalLobhandler lobhandler=NewDefaultlobhandler (); This. Getjdbctemplate (). Query ("Select content from Uos.docfile where id=?",NewString[] {ID},NewAbstractlobstreamingresultsetextractor () {protected voidStreamdata (ResultSet rs)throwssqlexception,ioexception,dataaccessexception{filecopyutils.copy (Lobhandler.getblobasbinarystream (rs,< /c2>1), OS); } }); }
SPRINGMVC processing MySQL blog field download