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)
<html:form method="POST" action="/solution.do" enctype="multipart/form-data">
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
<property column="uploadedfile" name="uploadedBlob" type="blob"/>
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); formbean.setUploadedBlob(myblob);
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() .configure().buildSessionFactory(); session = sessionFactory.openSession(); // Using a transaction is mandatory. transaction = session.beginTransaction(); session.saveOrUpdate(formbean); // Commit it transaction.commit(); System.out.println("Done"); 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 if(session!=null) { session.flush(); session.close(); } 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 // http://javakafunda.blogspot.com/2011/10/forcing-saveas-using-http-header.html response.setHeader("Content-Disposition", "attachment; filename=" +formbean.getFilename()); out.write(tempByte); out.flush(); out.close();
please give te complete code
ReplyDeleteThank you for this article
ReplyDelete