1

Pretty simple question with almost no answers on the web. I am using byte[] as my object. Currently I am reading the entire image into memory and then writing it. Very simple.

    @Column(name = "FILE_DATA")
private byte[] fileData;

I want to Stream it in. So I guess I need to use inputStream, but hibernate does not like that.

How do I set this up?

EDIT:

I tried this but got a -314 db2 error:

Blob b = null;
        try {
            Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.com", 80));
            URL urls = new URL(url);
            URLConnection conn = urls.openConnection(proxy);
            b = new BlobImpl(conn.getInputStream(), conn.getContentLength());
        } catch (Exception e) {
            e.printStackTrace();
        }

        att.setFileData(b);
        this.theDao.save(att);
2
  • Ahhhh...all this time, I was assuming that the file was local. This is a great question, but I think you need to flesh it out more. Try including a simple bare-bones method. Don't worry about catching exceptions. Commented Apr 24, 2012 at 15:14
  • As a test, can conn.getInputStream() be used to store the file on the local file system? Commented Apr 24, 2012 at 15:17

3 Answers 3

2

You can use Blob data type for inserting and fetching the images from database. It wil also provide you the option of Binary input stream. Just Use your db field as blob and create a blob ojbect instead of byte array.

b.getBinaryStream()

Also you can create Blob from bytes[]

Hibernate.createBlob(bytes);

And the simplest solution Saving on the hard drive and keeping information about the image on database.

Sign up to request clarification or add additional context in comments.

6 Comments

So how do I set up the column?
'@Column( name = "FILE_DATA" ) @Lob(type = LobType.BLOB)'
You are giving me just enough to make me want more. Is there some actual code? To create a blob, you need the content length. how do I get that with a stream? Do I have to read the stream first? If so, this is not helping.
You don't have to read the stream. You can use File.length().
I tried this and got a -317. I believe this might work if DB2 was set up correctly.
|
0

One of possible solutions is to keep filenames (URLs) in the databases and stream data from the disk (network resources) and stream them.

I would prefer this approach for web - when you just return an URL and it's handled by browser and Apache/Nginx (so you don't have to worry about streaming at all).

1 Comment

This is the answer that is all over the web, but for some of us, it has been decided to save it to a database. This answer does not answer the question asked.
-1

I'm not sure I understand what you mean by "hibernate does not like that"?

Here's how simple it can be to define.

@Entity Photo {
    @Id @GeneratedValue
    private Long id = null;

    private String filename = null;

    @Lob
    private byte[] bytes = null;
    }

Then to load the data into the object...

String filename = // Where ever this comes from...
File file = new File(filename);
InputStream is = new FileInputStream(file);

Photo photo = new Photo();
photo.setFilename(filename);

// Create and fill the byte array from the file.
byte[] bytes = new byte[(int) file.length()];
is.read(bytes);
photo.setBytes(bytes);

Then eventually tell Hibernate to save it in the database.

session.saveOrUpdate(photo);

2 Comments

This is not streaming. This is reading into memory and then saving it. I need to open the stream, and then send it to the database and save it. I don't want the memory impact of loading lots of images...
Oh. You want to stream it directly into the database, outside of Hibernate? You're right, this isn't that at all. However, java.sql.Blob (docs.oracle.com/javase/1.4.2/docs/api/java/sql/Blob.html) does have methods that might work for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.