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)