File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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: 206
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: 206
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: 206
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: 206
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: 206
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....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to upload file into mysql database using struts and hibernate?
 
Similar Threads
Collection Mapping
Not able to find the added record in table.
Problem in insert query
[Hibernate] NoClassDefFoundError
hibernate session: to close or not to close