SPRINGMVC processing MySQL blog field download

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.