jpg file to blob in MySQL - JDBC JAVA

This is a discussion on jpg file to blob in MySQL - JDBC JAVA ; 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 ...

+ Reply to Thread
Results 1 to 3 of 3

jpg file to blob in MySQL

  1. Default jpg file to blob in MySQL

    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




  2. Default Re: jpg file to blob in MySQL

    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

  3. Default Re: jpg file to blob in MySQL

    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

+ Reply to Thread

Similar Threads

  1. StringIO MySQL data blob Image problem
    By Application Development in forum Python
    Replies: 0
    Last Post: 09-05-2007, 02:14 PM
  2. Re: StringIO MySQL data blob Image problem
    By Application Development in forum Python
    Replies: 0
    Last Post: 09-05-2007, 08:10 AM
  3. [MySQL] Index, varchar and blob
    By Application Development in forum JDBC JAVA
    Replies: 0
    Last Post: 04-21-2005, 02:38 AM
  4. Blob update problem(mysql)
    By Application Development in forum JDBC JAVA
    Replies: 2
    Last Post: 04-01-2005, 06:48 AM
  5. MySQL and BLOB Error
    By Application Development in forum basic.visual
    Replies: 1
    Last Post: 02-04-2005, 10:15 AM