Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

jpg file to blob in MySQL

13 views
Skip to first unread message

Ike

unread,
Jun 14, 2006, 9:27:42 AM6/14/06
to
I am reading a jpeg file into a byte []:

File flimage = new File("c:\temp.jpg");
byte[] b = new byte[(int)flimage.length()];
try{
InputStream isImage = new FileInputStream(flimage);
isImage.read(b);
}catch(Exception e){
e.printStackTrace();
return null;
}

It returns a valid byte[].

I then update my blobs table with this.

String qstring = "UPDATE blobs SET blb= ? WHERE
id="+Integer.toString(cs.idinblobs);

Looking at the db tables, all looks well. But, when I go to read the blob
back and create an ImageIcon from it:


ArrayList v2....//this is from the resultset of
the blobs table

byte[] jack = (byte[])v2.get(colinrow);
ii = new ImageIcon(jack);

if(ii.getImageLoadStatus() ==
MediaTracker.COMPLETE){
jEditTextField[e].setText(ii.getImage());
}else if(ii.getImageLoadStatus() ==
MediaTracker.ABORTED){
System.out.println("Image loading ABORTED");
jEditTextField[e].setText(null);
}else if(ii.getImageLoadStatus() ==
MediaTracker.ERRORED){
System.out.println("Image loading ERRORED");
jEditTextField[e].setText(null);
}else if(ii.getImageLoadStatus() ==
MediaTracker.LOADING){
System.out.println("Image loading LOADING");
}
}catch(Exception ignorefornow){
ignorefornow.printStackTrace();
}

which gives me the following exception:

sun.awt.image.ImageFormatException: Invalid JPEG file structure: two
SOF markers
at sun.awt.image.JPEGImageDecoder.readImage(Native Method)
at
sun.awt.image.JPEGImageDecoder.produceImage(JPEGImageDecoder.java:144)
at
sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:254)
at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:172)
at sun.awt.image.ImageFetcher.run(ImageFetcher.java:136)
Image loading ERRORED


Does anyone have any idea why this giving me this problem? I have not hat
trouble saving blobs before, but, for some reason, with these pictures, it
is failing me. Thanks, Ike

Mladen Adamovic

unread,
Jun 14, 2006, 1:39:17 PM6/14/06
to
I find your problem quite interesting.
Although I don't know what is the problem and it needs time to prepare
for running your code I will try to give you some hints which you could
use to find solution by yourslelf. I hope it will help.

Ike wrote:
> I am reading a jpeg file into a byte []:
>
> File flimage = new File("c:\temp.jpg");
> byte[] b = new byte[(int)flimage.length()];
> try{
> InputStream isImage = new FileInputStream(flimage);
> isImage.read(b);
> }catch(Exception e){
> e.printStackTrace();
> return null;
> }

check after this code could you reconstruct the image properly from
bytes. Then you will know is problem in blog or in image manipulation.

> ArrayList v2....//this is from the resultset of
> the blobs table
>
> byte[] jack = (byte[])v2.get(colinrow);
> ii = new ImageIcon(jack);

I assume you should use getByte() method on the result set.
I don't have the clue what is in v2, how did you convert ResultSet to
ArrayList.

You did explicit conversion of "some type of object" to byte[]... Are
you sure your source object was byte[]. Probably it was otherwise you
might get ClassCastException but it might be an good idea to do
something like:

System.out.println(vs.get(colinrow).getClass().getName());

> Does anyone have any idea why this giving me this problem?

You have
1) problem with dealing with blobs
OR
2) problem with dealing with pictures.
You should perform some things I have suggested to find out where is
your problem.

In case you found the solution post it here, in case you haven't find it
provide some more details.

--
Mladen Adamovic
http://www.online-utility.org http://www.shortopedia.com
http://www.froola.com http://www.gift-idea4u.com

JScoobyCed

unread,
Jun 14, 2006, 11:46:20 PM6/14/06
to
Ike wrote:
> I am reading a jpeg file into a byte []:
>
> File flimage = new File("c:\temp.jpg");
> byte[] b = new byte[(int)flimage.length()];
> try{
> InputStream isImage = new FileInputStream(flimage);
> isImage.read(b);
> }catch(Exception e){
> e.printStackTrace();
> return null;
> }
>
> It returns a valid byte[].

Is the actual content of this byte[] the same as the image file? I.e.:
- was the file read fully?
- saving this byte[] back to a FileOutputStream with different name
gives a correct image?
- use this byte in your next code (the one to display the image from the
blob) and see if it works

Another option would be to convert the initial byte[] (from the
inputstream) to a non-binary array (base64 or hexa values) and save it
to the blog. Then when reading it back, converting to the original
format. But this solution needs processing time and takes more space in
the DB.

--
JSC

0 new messages