This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to convert FormFile into BLOB object?

 
vikrantjain jain
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See the FormFile Javadocs, specifically the getFileData() method.
 
vikrantjain jain
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help

It would be more helpful,if you can elaborate this more.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please see ItDoesntWorkIsUseless. The code you posted would throw a NullPointerException.
 
vikrantjain jain
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any input from anyone will be greatly appreciated.

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instantiate an instance first?
 
vikrantjain jain
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote:Please see ItDoesntWorkIsUseless.

And TellTheDetails. How can anybody help if they don't know what's happening?
 
vikrantjain jain
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1179
Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
vikrantjain jain
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1179
Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is "theFile.getFileData()" a byte[]??

This is how I did it long time ago:

 
vikrantjain jain
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic