• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Convert file to blob

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
I want my playground back. Here, I'll give you this tiny ad for it:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic