• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

bytearray image decoding

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone!. I'm having such a problem with Images stored on database. Using jBoss + seam 2.2.1

DB: postgre.
Name: imagen
Column type: bytea

Entity.
@Column(name = "imagen")
private byte[] imagen;

Uploading:
xhtml:


bean:
The common EntityHome, saving the data.

Displaying the image:
xhtml:


It displays nothing (but the value is NOT null). Firebug shows: "NetworkError: 404 No Encontrado - http://localhost:8080/app/seam/resource/graphicImage/imagen.png"

I tried several approaches just to understand the problem, for example:



It gets an exception on line 5 because the ImageInputStream is null. Already did a research, and seems the header is lost and the bytearray isn't recognized as an image anymore.
https://coderanch.com/t/533840/Streams/java/java-lang-IllegalArgumentException-im-null
http://stackoverflow.com/questions/1212882/convert-byte-array-to-image-in-java-without-knowing-the-type

Also, tried an image servlet as stated here:
http://balusc.blogspot.com/2007/04/imageservlet.html

It displays nothing, and firebug informs that the image is corrupt or truncated

This is another upload method I tried:

bean:


xhtml:


I believe the upload image get corrupted/damaged somehow, maybe losing the header... but I really don't know how.

I'm really stuck here. Any thoughts?
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your logic for extracting the file data into the byte array is horribly complicated. I don't understand it... and neither do you, apparently. I can at least see that the condition in the if-statement at line 29 can never be true, but line 22 is too hard for me to understand. I would replace that whole thing by this:

so that I didn't have to write that sort of code.

But it's also possible that your file upload is being corrupted after that, perhaps by the process of putting it into the database.

As for displaying the image, don't use any code which uses ImageIO to process the image. Just stream the array of bytes out to the response, using an image servlet as already suggested.
 
Juan Javaloyes
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That bunch of horrible code I extracted from some examples (can't remembers where) just to test it, I want to use seam components (s:fileUpload), not that rich:fileUpload, so I didn't look at it too hard. I'm using s:fileUpload to upload files to another servers through samba and it works 100% ok, so, it's more or less the same process, so I think it should work.

I'm now trying to display images already loaded on the DB from another application, and can't do it. The legacy application (Swing) can display the images, but I can't on mine.
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Juan Javaloyes wrote:I'm now trying to display images already loaded on the DB from another application, and can't do it.



Aha. In that case looking at code which uploads images isn't going to be useful. You're going to have to write an image servlet which sends the data from the data to the response, with the correct MIME type. If you have code which gets the data from the DB to a byte array, then half of your work is already done.
 
Juan Javaloyes
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm facing another issue now: "java.lang.IllegalArgumentException: no file extension in servlet path: /modelImageProcessing". It Happens when I try to access my Dao or EntityManager from the servlet.

EntityManager em = JpaResourceBean.getEm();
or even
JpaDao<Entityname> dao = new JpaDao<Entityname>();

It don't even reach the class...

My web xml seems to be ok:



I'm missing something, but can't find it

Edit1: Actually, I managed to get it:
EntityManager em = (EntityManager) getServletContext().getAttribute("entityManager");
But it always returns null... It's been years since I've been working with servlets! Maybe I'll get the EMF and create the EM manually...

Edit 2: Had to go with the last line. Creating an em.

@PersistenceUnit(unitName="pu")
private EntityManagerFactory emf;

Still not getting a image display, firebug throws: "Image corrupt or truncated: http://localhost:8080/app/modelImageProcessing?id=1"
 
Juan Javaloyes
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's doGet part of my servlet:


and here part of my jsf:


it returns
<img id="model:image1" src="/app/modelImageProcessing/1">
and the error (Firebug): "Image corrupt or truncated: http://localhost:8080/app/modelImageProcessing/1"

What am I missing???
 
Juan Javaloyes
Greenhorn
Posts: 13
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All done. The problem was a mix of everything, from corrupted upload to a bad code on page load.
In case anyone needs, my working servlet is:



web.xml:



the upload component:



The "viewer":


Now it even works without a servlet. Just with:

reply
    Bookmark Topic Watch Topic
  • New Topic