This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes How to upload file into mysql database using struts and hibernate? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "How to upload file into mysql database using struts and hibernate?" Watch "How to upload file into mysql database using struts and hibernate?" New topic
Author

How to upload file into mysql database using struts and hibernate?

Yogesh Gandhi
Ranch Hand

Joined: Dec 05, 2008
Posts: 207
Dear Friends,

Can you please help me in achieving this.
I want to upload a file into mysql mediumBlob column using struts and hibernate.

JSP Code:




SolutionFormBean.hbm.xml
=================




Saving the Form bean into the database:

In struts action class I get a FormFile object. What next?



The error I get is:
==============
javax.servlet.ServletException: BeanUtils.populate
org.apache.struts.util.RequestUtils.populate(RequestUtils.java:469)
org.apache.struts.action.RequestProcessor.processPopulate(RequestProcessor.java:818)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:194)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause

java.lang.IllegalArgumentException: Cannot invoke com.myapp.struts.SolutionFormBean.setUploadedfile - argument type mismatch
org.apache.commons.beanutils.PropertyUtilsBean.invokeMethod(PropertyUtilsBean.java:1778)
org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(PropertyUtilsBean.java:1759)
org.apache.commons.beanutils.PropertyUtilsBean.setNestedProperty(PropertyUtilsBean.java:1648)
org.apache.commons.beanutils.PropertyUtilsBean.setProperty(PropertyUtilsBean.java:1677)
org.apache.commons.beanutils.BeanUtilsBean.setProperty(BeanUtilsBean.java:1022)
org.apache.commons.beanutils.BeanUtilsBean.populate(BeanUtilsBean.java:811)
org.apache.commons.beanutils.BeanUtils.populate(BeanUtils.java:298)
org.apache.struts.util.RequestUtils.populate(RequestUtils.java:467)
org.apache.struts.action.RequestProcessor.processPopulate(RequestProcessor.java:818)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:194)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)


Can someone direct me to a tutorial where I can see a HELLO WORLD type example which makes me upload a file into database using hibernate and struts.
Yogesh Gandhi
Ranch Hand

Joined: Dec 05, 2008
Posts: 207
I guess I have got the solution

SolutionFormBean.hbm.xml
===================


SolutionFormBean.java
================


Solution.jsp
===========
// Do not forget to specify enctype in the html:form



SolutionAC.java
=============




With this code, atleast I am able to save a record in the database.
Yogesh Gandhi
Ranch Hand

Joined: Dec 05, 2008
Posts: 207
What I have done to make it work, I have created a post for the same of others who seek help on internet.

http://javakafunda.blogspot.in/2012/05/how-to-upload-file-using-struts-and.html

But the problem I am facing is that a .doc file is uploaded correctly, while when I select .jpg file it is not uploaded into the database.

The db field that I have taken is of type mediumblob, which I guess can accomodate data upto 15 MB if I am not wrong.

But still, there's no error when I upload the file, but the file doesn't get uploaded in the database.
Manuel Petermann
Ranch Hand

Joined: Jul 19, 2011
Posts: 175

I am against saving raw data into a database for no reason. You should consider to save the file elsewhere and just save the place where you saved it into the database.


Please correct my English.
Yogesh Gandhi
Ranch Hand

Joined: Dec 05, 2008
Posts: 207
but i guess blob is made for this purpose only. to save the file content in the database.

saving the file on hard disk doesn't seem a good solution to me....
Manuel Petermann
Ranch Hand

Joined: Jul 19, 2011
Posts: 175

I suggest reading blob_vs_file_system_storage which is pro Filesystem loading and
Best-Practice-in-File-Storage-while-Building-Application which is more pro database.
In this blog post the poster came to another conclusion.
I would recommend to use the filesystem up until there is a specific reason not to.
You may want to google database blob vs file as there are many points of view on that matter.
Yogesh Gandhi
Ranch Hand

Joined: Dec 05, 2008
Posts: 207
wow...thats a new interesting world...i realized that i can use the file system approach, but it is cleaner as well...

The only drawback i see is that while taking backup i do have to take backup of the file system uploaded content as well....
 
wood burning stoves
 
subject: How to upload file into mysql database using struts and hibernate?
 
Similar Threads
[Hibernate] NoClassDefFoundError
Collection Mapping
Problem in insert query
hibernate session: to close or not to close
Not able to find the added record in table.