Use Hibernate to store files in a database (less than 64k files)

Source: Internet
Author: User
Tags rollback

First, there are e:\\a.jpg files, less than 64k

The following is the JavaBean object code

 Public classFilebean {intID; byte[] file;  Public intgetId () {returnID; }     Public voidSetId (intID) { This. ID =ID; }     Public byte[] GetFile () {returnfile; }     Public voidSetfile (byte[] file) {         This. File =file; } @Override PublicString toString () {return"Filebean [id=" + ID + ", file=" + arrays.tostring (file) + "]"; }    }

================================================================================

The following is the mapping configuration file

<?XML version= "1.0"?><!DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "Http://hibernate.sourcefo Rge.net/hibernate-mapping-3.0.dtd "><hibernate-mapping Package= "Com.hibernate.file">        <classname= "Filebean"Table= "Filetese">        <IDname= "id"type= "int"column= "id">        <Generatorclass= "Native"/>    </ID>            < Propertyname= "File"type= "byte[]"length= "5000000"/>            </class>    </hibernate-mapping>

As long as it is, it's no use, or it can only insert 64k files.

Tinyblob: Only 255 characters
BLOB: Maximum limit to 65K bytes
Mediumblob: Limit to 16M bytes
Longblob: Up to 4GB

Here is the database table structure

Here is the picture file into the database code

     Public voidTestin () {org.hibernate.Transaction TS=NULL; Session Sess=NULL; Try{File File=NewFile ("e:\\a.jpg");//Create a file picture objectFileInputStream in=NewFileInputStream (file);//Get input stream            byte[] infile=New byte[In.available ()];//input flow to binaryIn.read (InFile);//read-in binaryIn.close ();//Close the streamFilebean bean=NewFilebean (); Bean.setfile (inFile);//Set binary FilesSess = Sessionfactory.opensession ();//Open SessionTs=sess.begintransaction ();//Open TransactionSess.save (Bean);//Save ObjectTs.commit ();//Commit a transaction}Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); System.out.println ("File not Found");        Ts.rollback (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); System.out.println ("IO exception");        Ts.rollback (); }finally{sess.close (); }    }

===================================================

The output code for the picture resource in the database

     Public voidtestout () {Session sess=sessionfactory.opensession (); Org.hibernate.Transaction TS=NULL; Try{FileOutputStream out=NewFileOutputStream ("e:\\b.jpg");//Get output streamts=sess.begintransaction (); Filebean Bean= (Filebean) sess.get (Filebean.class, 2);            Ts.commit (); Out.write (Bean.getfile ());//write a binary fileOut.close (); } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); System.out.println ("File not Found" +e.tostring ());        Ts.rollback (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); System.out.println ("IO exception" +e.tostring ());        Ts.rollback (); }finally{sess.close (); }    }

Use Hibernate to store files in a database (less than 64k files)

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.