Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Read an image file into memory

 
Benjamin Hundley
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to do a simple task and I'm wondering what the best way to do it is. I just need to read an image file into memory and then store the image in a database. I saw the Image and BufferedImage classes but I was thinking those might be overkill. I don't need to display these images. From my perspective I don't think I even care that they are image files. I just want to read the file into memory from one place and store it in our database. Can someone maybe point me in the right direction?
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it critical that the image file gets read into memory? Because if the only requirement is to get the file from the disk into the database, here's how I would do it:

(1) Create a PreparedStatement which does an INSERT or an UPDATE or whatever you need to write data to your database, e.g.

where that's assuming you have a table with one column.

(2) Execute that PreparedStatement while referring to data which will be taken directly from the file:
 
Benjamin Hundley
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it is not critical that I load it into memory. I agree, that's a better way to do it. Thanks!

But, if I did want to read it into memory would I just use a byte array?
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Benjamin Hundley wrote:But, if I did want to read it into memory would I just use a byte array?


Yup. Your only goal (as far as I can tell) is to get the bytes from the file into the database column. No need to interpret them on the way.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!