aspose file tools*
The moose likes JDBC and the fly likes How to convert FormFile into BLOB object? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to convert FormFile into BLOB object?" Watch "How to convert FormFile into BLOB object?" New topic
Author

How to convert FormFile into BLOB object?

vikrantjain jain
Greenhorn

Joined: Jun 13, 2010
Posts: 12
Hi,

I am uploading one excel file using FormFile of struts.

Now I need to convert that Formfile into BLOB object so that I can pass that BLOB into database procedure.
Can anyone help me that how can I convert FormFile into BLOB object?

Any help will be greatly appreciated.

Thanks
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

See the FormFile Javadocs, specifically the getFileData() method.
vikrantjain jain
Greenhorn

Joined: Jun 13, 2010
Posts: 12
Thanks for the help

It would be more helpful,if you can elaborate this more.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Moving to JDBC, as this seems to be an issue of not knowing how to take bytes and make them into a blob.
vikrantjain jain
Greenhorn

Joined: Jun 13, 2010
Posts: 12
David, I have tried the way you told me,but it is not working
-------------
BLOB blob=null;
blob.setBytes(1, theFile.getInputStream());
cs.setBlob(3,blob);
------------
Where cs is the callable statement and theFile is the Form File

Do you know anyother way to do this.

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please see ItDoesntWorkIsUseless. The code you posted would throw a NullPointerException.
vikrantjain jain
Greenhorn

Joined: Jun 13, 2010
Posts: 12
David,

Yes you are right,I am getting "null pointer exception"

Is any other way to do this?

Thanks for your help!
vikrantjain jain
Greenhorn

Joined: Jun 13, 2010
Posts: 12
Any input from anyone will be greatly appreciated.

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Instantiate an instance first?
vikrantjain jain
Greenhorn

Joined: Jun 13, 2010
Posts: 12
can you please explain me how to do that ?

As when i am trying to instantiate the BLOB ,it is gving me error.
BLOB blob=new BLOB();

so i intialize it with null;
BLOB blob=null;

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I wrote:Please see ItDoesntWorkIsUseless.

And TellTheDetails. How can anybody help if they don't know what's happening?
vikrantjain jain
Greenhorn

Joined: Jun 13, 2010
Posts: 12
Please find the description of the complete problem below

I am trying to Upload one file using FormFile of Struts and I need to pass that Form File into one procedure having BLOB as aIn parameter. SO I need to convert that file into BLOB object

The code that I am trying is :
------------------------------------------------
try{

BLOB blob= BLOB.getEmptyBLOB();
System.out.println("before");
blob.setBytes(theFile.getFileData());
System.out.println("after");
cs.setBlob(3, blob);
}catch(Exception e){System.out.println("in the BLOB "+e);}

------------------------------------------------

After running the code,I am getting the following error
----------------------------------------------------------
java.lang.ArrayIndexOutOfBoundsException
at java.lang.System.arraycopy(Native Method)
at oracle.jdbc.driver.DatumBinder.bind(OraclePreparedStatement.java:15839)
at oracle.jdbc.driver.OraclePreparedStatement.setupBindBuffers(OraclePreparedStatement.java:2865)
at oracle.jdbc.driver.OraclePreparedStatement.processCompletedBindRow(OraclePreparedStatement.java:2150)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3279)
at oracle.jdbc.driver.OraclePreparedStatement.execute(OraclePreparedStatement.java:3389)
at oracle.jdbc.driver.OracleCallableStatement.execute(OracleCallableStatement.java:4222)
at dms.bo.impl.LoadManagement.UploadMetadataDAOImpl$1.doInCallableStatement(UploadMetadataDAOImpl.java:38)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:953)
at dms.bo.impl.LoadManagement.UploadMetadataDAOImpl.getBlobString(UploadMetadataDAOImpl.java:28)
at dms.web.action.LoadManagement.UploadMetadataAction.perform(UploadMetadataAction.java:33)
at dms.web.action.common.BaseAction.execute(BaseAction.java:17)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:874)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
at java.lang.Thread.run(Thread.java:619
)----------------------------------------------------------

Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

If it is an Oracle DB you are using, then you need to insert an EMPTY_BLOB() first, and then select it back - and then insert the data into the Blob.


Regards, Rene Larsen
Dropbox Invite
vikrantjain jain
Greenhorn

Joined: Jun 13, 2010
Posts: 12
as you suggested, I have created an function returning BLOB and use this return value to initialize the BLOB object.
But still get the same error at the statement blob.setBytes(theFile.getFileData());
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

Is "theFile.getFileData()" a byte[]??

This is how I did it long time ago:

vikrantjain jain
Greenhorn

Joined: Jun 13, 2010
Posts: 12
Hi,
As you suggested,I have ttried it but it does'nt work
SO, I have followed one different approach for passing BLOB to procedure.
First I have stored File to table in BLOB and then gettign that File into Blob variable.
Now I aghain stuck into one thing of passing that Blob inot procedure.

Please find the snippet of the code:
---------------------------
PreparedStatement pre = con.prepareStatement("insert into tab1 values(?)");
pre.setBinaryStream(1,theFile.getInputStream(),(int)theFile.getFileSize());
pre.executeUpdate();
System.out.println("test prepareStaement");

Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT COL1 FROM tab1");
while(rs.next())
{
//Blob blob= rs.getBlob("COL1");

Blob blob1= rs.getBlob(1);
System.out.println("blob start: "+blob1);
cs.setBlob(3,blob1);
------------------------------------------------------------------------------------------

Now,I am getting this error
----------------------------------------
java.lang.AbstractMethodError: oracle.jdbc.driver.OracleCallableStatement.setBlob(Ljava/lang/String;Ljava/sql/Blob;)V
dms.bo.helper.LoadManagement.UploadMetadataCallStmlCreator.createCallableStatement(UploadMetadataCallStmlCreator.java:78)
org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:947)
dms.bo.impl.LoadManagement.UploadMetadataDAOImpl.getBlobString(UploadMetadataDAOImpl.java:29)
dms.web.action.LoadManagement.UploadMetadataAction.perform(UploadMetadataAction.java:33)
dms.web.action.common.BaseAction.execute(BaseAction.java:17)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

------------------------------------
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to convert FormFile into BLOB object?