File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Convert file to blob

 
Ivan Kesic
Ranch Hand
Posts: 68
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a file chooser. And he returns File object. I need to store that object in my-sql so i need to convert it somehow to Blob object becouse that is the object preparedstatement needs . How do i do that? Normal casting doesn't work...

edit: I see that Blob is actually interface. Now im eaven more confused...
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that a File object just represents the location of the file. So casting is never going to work - the most you can expect to get from it directly would be the filename or path. But you can read from a file by creating a FileInputStream.

And if you look at PreparedStatement there's another method that should work, that takes an InputStream as an argument: setBinaryStream.

Any use?
 
Ivan Kesic
Ranch Hand
Posts: 68
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok i did that, and its working. Thank you
edit: but i still have a problem. Now i get an exception, that data of an jpg is too long for column in sql. I just made FileInputStream object of file and past it in prepared statement. Why do i get this error?
java.io.FileInputStream@15ad5c6 this is my inputStream, and it needs i think to be smaller, not by size but this text as i figure. But i cant change this...

I fixed it. Just needed to set blob to be longblob, with java everything was fine...

Thank you
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic