-1

I wanna use a fileentry component to save an image to a database(postgres with JPA connection) and JSF. I was searching it, so i think i have to use an inputstream, but i don't know how to use it to convert the image to a bytea type to be saved to a database in a column called for example cover's book(bytea). what code shoud be in the bean. Please help me. I have something like this:

Icefaces 3.0.1 FileEntry: FileEntryListener is never called

Community
  • 1
  • 1
tan
  • 1

1 Answers1

1

You're trying to do it all at once. Break it down step-by-step into pieces you can understand on their own.

First make sure that receiving the image and storing it to a local file works. There are lots of existing examples for this part.

Then figure out how to map bytea in your JPA implementation and store/retrieve files from disk. You didn't mention which JPA provider you're using, so it's hard to be specific about this.

Finally, connect the two up by writing directly from the stream, but only once you know both parts work correctly by themselves. At this point you will find that you can't actually send a stream directly to the database if you want to use a bytea field - you must fetch the whole file into memory as a byte buffer.

If you want to be able to do stream I/O with files in the database you can do that using PostgreSQL's support for "large objects". You're very unlikely to be able to work with this via JPA, though; you'll have to manage the files in the DB directly with JDBC using the PgJDBC extensions for large object suppport. You'll have to unwrap the Connection object from the connection pooler to get the underlying Connection that you can cast to PgConnection in order to access this API.

If you're stuck on any individual part, feel free to post appropriately detailed and specific questions about that part; if you link back to this question that'll help people understand the context. As it stands, this is a really broad "show me how to use technology X and Y to to Z" that would require writing (or finding) a full tutorial to answer.

Craig Ringer
  • 307,061
  • 76
  • 688
  • 778