Generally, websites usually adopt two policies when processing images uploaded by users: one is to directly store images into BLOB fields in the database, and the other is to store only the path information of images on the server in the database, images are stored in different types of files. When used, you can read the path information from the database to the IMG element on the page. I will not discuss the advantages and disadvantages of the two solutions here. I just wrote a hibernate example to implement the first strategy. the example is very simple. The t_user table has two main fields: name and photo. The photo field type is blob. in this example, I use MySQL in the database. The Blob field in Oracle is special. You must customize the type. For details, please search by yourself. There are many materials in this regard.
// User. Java
Package com. denny_blue.hibernate;
Import java. Io. serializable;
Import java. SQL. Blob;
Public class user implements serializable {
Private integer ID;
Private string name;
Private blob photo;
/**
* @ Return the ID
*/
Public user (){
}
Public integer GETID (){
Return ID;
}
/**
* @ Param ID the ID to set
*/
Public void setid (integer ID ){
This. ID = ID;
}
/**
* @ Return the name
*/
Public String getname (){
Return name;
}
/**
* @ Param name the name to set
*/
Public void setname (string name ){
This. Name = Name;
}
/**
* @ Return the photo
*/
Public blob getphoto (){
Return photo;
}
/**
* @ Param photo the photo set
*/
Public void setphoto (BLOB photo ){
This. Photo = photo;
}
}
Class user has three attributes: ID, name, photo, corresponding getter and setter methods, and a non-argument constructor. Note that Java. SQL. blob of photo type
The corresponding user. HBM. xml should be as follows:
<? XML version = "1.0"?>
<! Doctype hibernate-mapping public
"-// Hibernate/hibernate mapping DTD 3.0 // en"
Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd>
<Hibernate-Mapping
Package = "com. denny_blue.hibernate">
<Class name = "com. denny_blue.hibernate.user"
Table = "t_user"
Dynamic-update = "true"
Dynamic-insert = "true"
Batch-size = "3">
<ID name = "ID"
Column = "ID"
Type = "Java. Lang. Integer">
<Generator class = "native"/>
</ID>
<Property name = "name" column = "name" type = "Java. Lang. String" lazy = "true"/>
<Property name = "photo" column = "photo" type = "Java. SQL. Blob"/>
</Class>
</Hibernate-mapping>
The corresponding hibernate. cfg. xml configuration file is no longer listed. Please refer to the hibernate document to set it yourself.
OK. After this step, we will write a test class for unit testing:
Package com. denny_blue.test;
Import java. Io. fileinputstream;
Import java. Io. filenotfoundexception;
Import java. Io. fileoutputstream;
Import java. Io. ioexception;
Import java. Io. inputstream;
Import java. SQL. Blob;
Import org. hibernate. hibernate;
Import org. hibernate. hibernateexception;
Import org. hibernate. Session;
Import org. hibernate. sessionfactory;
Import org. hibernate. transaction;
Import org. hibernate. cfg. configuration;
Import com. denny_blue.hibernate.user;
Import JUnit. Framework. testcase;
Public class hibernatetest extends testcase {
Private session;
Protected void setup () throws exception {
Try {
Configuration Config = new configuration (). Configure ();
Sessionfactory Sf = config. buildsessionfactory ();
Session = SF. opensession ();
} Catch (hibernateexception e ){
E. printstacktrace ();
}
}
Protected void teardown () throws exception {
Try {
Session. Close ();
} Catch (hibernateexception e ){
E. printstacktrace ();
}
}
Public void testsave () throws filenotfoundexception, ioexception {
User user = new user ();
User. setname ("Jordan ");
Fileinputstream in = new fileinputstream ("C: // test.gif ");
Blob photo = hibernate. createblob (in );
User. setphoto (photo );
Transaction Tx = NULL;
Try {
Tx = session. begintransaction ();
Session. saveorupdate (User );
TX. Commit ();
} Catch (hibernateexception e ){
If (TX! = NULL)
TX. rollback ();
E. printstacktrace ();
} Finally {
In. Close ();
}
}
Public void testload () throws exception {
Try {
User user = (User) Session. Load (user. Class, new INTEGER (1 ));
Blob photo = user. getphoto ();
Inputstream in = photo. getbinarystream ();
Fileoutputstream out = new fileoutputstream ("C: // Out // test2.gif ");
Byte [] Buf = new byte [1, 1024];
Int Len;
While (LEN = in. Read (BUF ))! =-1 ){
Out. Write (BUF, 0, Len );
}
In. Close ();
Out. Close ();
} Catch (hibernateexception e ){
E. printstacktrace ();
}
}
}
Read the test.gif file in the C directory and store it in the database. Then, extract the file and write it to the C:/out directory. In this case, you can view the photo in the data table as blob, indicating that the file has been successfully saved. the following code snippet is worth noting:
Fileinputstream in = new fileinputstream ("C: // test.gif ");
Blob photo = hibernate. createblob (in );
Here we read images from the disk. in actual application, you can use the Upload Component to obtain the binary data stream of the image and use hibernate. createblob method to construct the corresponding BLOB Object. the image is used.
Inputstream in = photo. getbinarystream ();
This is just a simple test class. If I want to retrieve images from the database and actually do it on the page, what should I do? In fact, it is also very simple. First we need to write a servlet, retrieve the image in its service method, and "Draw" it to the specified page.
Package com. easyjf. asp. Action;
Import java. Io. inputstream;
Import java. Io. outputstream;
Import java. SQL. Blob;
Import javax. servlet. servletexception;
Import javax. servlet. http. httpservlet;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;
Import org. hibernate. hibernateexception;
Import org. hibernate. Session;
Import org. hibernate. sessionfactory;
Import org. hibernate. cfg. configuration;
Import com. Denny) Blue. hibernate. user;
Public class test extends httpservlet {
/**
* Destruction of the servlet. <br>
*/
Private session;
Public void destroy (){
Try {
Session. Close ();
} Catch (hibernateexception e ){
E. printstacktrace ();
}
}
/**
* Initialization of the servlet. <br>
*
* @ Throws servletexception if an error occure
*/
Public void Init () throws servletexception {
Try {
Configuration Config = new configuration (). Configure ();
Sessionfactory Sf = config. buildsessionfactory ();
Session = SF. opensession ();
} Catch (hibernateexception e ){
E. printstacktrace ();
}
}
Public void doget (httpservletrequest request, httpservletresponse response)
{
Try {
User user = (User) Session. Load (user. Class, new INTEGER (1 ));
Blob photo = user. getphoto ();
Inputstream in = photo. getbinarystream ();
Outputstream out = response. getoutputstream ();
Byte [] Buf = new byte [1, 1024];
Int Len;
While (LEN = in. Read (BUF ))! =-1 ){
Out. Write (BUF, 0, Len );
}
In. Close ();
Out. Close ();
} Catch (exception e ){
E. printstacktrace ();
}
}
}
The output stream is obtained through response. getoutputstream, and the others are the same as the previous code. The servlet has been written. How can this be called on the page? It is simpler to call the servlet directly on the src attribute of the IMG tag on the page, for example:
This is a simple example.