How to upload file using struts and hiberate?

How to upload file using struts and hibernate?

I spent 5-6 hours figuring out how can I upload a file into Blob in a database. So I decided to write a short article.

Add enctype=”multipart/form-data” in the form tag (in JSP)

Add 2 fields in ActionForm, one which is populated via struts framework.
Second one which is read by hibernate i.e. uploadedBlob

// This is filled by the code written in Action Class.
// This has to be done manually and is not done automatically.
private Blob uploadedBlob;
public Blob getUploadedBlob() {
return uploadedBlob;

public void setUploadedBlob(Blob uploadedba) {
this.uploadedBlob = uploadedba;

// This is the actual FormFile which gets populated into the ActionForm
// by struts framework.
private FormFile uploadedfile;
public FormFile getUploadedfile() {
return uploadedfile;

public void setUploadedfile(FormFile uploadedfile) {
this.uploadedfile = uploadedfile;

Add the following in formbean.hbm.xml

In Action Class, convert FormFile to Blob and set it in FormBean (while saving)

// Convert FormFile to a byte Array
byte[] byteArray=formbean.getUploadedfile().getFileData();
// Convert this byteArray to a blob
Blob myblob=Hibernate.createBlob(byteArray);

For saving this formbean to database use

private boolean saveOrUpdate(SolutionFormBean formbean) 
Session session = null;

//Transaction object
// Nothing happens without transaction in hibernate.
Transaction transaction = null;
try {
// This step will read hibernate.cfg.xml
// and prepare hibernate for use
SessionFactory sessionFactory = new Configuration()
session = sessionFactory.openSession();
// Using a transaction is mandatory.
transaction = session.beginTransaction();
// Commit it
return true;
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
} finally {
// Actual row insertion will happen at this step
return true;

While reading Blob from database, you would have to do the reverse, i.e.
convert Blob to byteArray and write to ServletOutputStream

Blob dbBlob = dbBean.getUploadedBlob();
int bloblength=(int)dbBlob.length();
byte[] tempByte = dbBlob.getBytes(1, bloblength);
// Now we have converted blob to byte array.
ServletOutputStream out = response.getOutputStream();
// For reading more about this header do check out the following link
response.setHeader("Content-Disposition", "attachment; filename="

About Vineet Verma

Developer/Blogger/Gamer/Lazy Couch Potato...:P Need PDF Books: Knowledge Base
This entry was posted in Hibernate, Struts. Bookmark the permalink.

2 Responses to How to upload file using struts and hiberate?

  1. Razz modi says:

    please give te complete code


  2. Thank you for this article


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s