Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / June 2006

Tip: Looking for answers? Try searching our database.

jpg file to blob in MySQL

Thread view: 
Ike - 14 Jun 2006 14:27 GMT
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 - 14 Jun 2006 18:39 GMT
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.

> I am reading a jpeg file into a byte []:
>
[quoted text clipped - 7 lines]
>                 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.

Signature

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

JScoobyCed - 15 Jun 2006 04:46 GMT
> I am reading a jpeg file into a byte []:
>
[quoted text clipped - 9 lines]
>
> 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.

Signature

JSC



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.