Skip site navigation (1) Skip section navigation (2)

blobs in a client/server environment

From: Matt Fair <matt(at)netasol(dot)com>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: blobs in a client/server environment
Date: 2001-09-26 21:52:40
Message-ID: 3BB24E28.6070901@netasol.com (view raw or flat)
Thread:
Lists: pgsql-jdbc
Hello,
I have been playing around with storing images with setBinaryStream and 
getBinaryStream for a couple of days now, but I am continually running 
into a problem.  I don't know if I am getting the data in the right 
format or not, but I am not able to get my image from the database.
I have a client/server environment and when I get the image back from 
the database that is done on the server.  So I do not want to write the 
InputStream as a file, instead as an array of bytes which are sent back 
to the client.
I recive no errors when putting the image into the database and getting 
the  image out of the database, where I get my error is when I try to 
generate the image from the array of bytes.

To put the image into the database (no errors):       
        FileInputStream fis = new FileInputStream(file);
        PreparedStatement ps = con.prepareStatement("update 
userpreferences set image=?, time=? where username=?");
        ps.setBinaryStream(1, fis, length);
        ps.setInt(2, (int)new java.util.Date().getTime());
        ps.setString(3, user);
        ps.executeUpdate();
        ps.close();
        fis.close();
        con.commit();
        con.close();

To get the image from the database and put it into an array of bytes (no 
errors):       
        con.setAutoCommit(false);
        PreparedStatement ps = con.prepareStatement("select image from 
userpreferences where username=?");
        ps.setString(1, user);
        ResultSet rs = ps.executeQuery();
        con.commit();
        if(rs != null)
          if(rs.next()){
            InputStream is = rs.getBinaryStream(1);
            ByteArrayOutputStream byteStream = new 
ByteArrayOutputStream();           
             int data;
             while ((data = is.read()) != -1)
                byteStream.write((byte)data);           
            byte[] buf = byteStream.toByteArray();
           
            is.close();
            rs.close();
            con.close();           
            return buf;
        

I get my error when I try to create my image with the library Jimi.
byte[] data = (got bytes from above);
Image image = Jimi.getImage(new ByteArrayInputStream(data));

The error stack trace I get is:
        com.sun.jimi.core.JimiException: Error creating image.
        at 
com.sun.jimi.core.raster.JimiRasterImageImporter.importImage(JimiRasterImageImporter.java:48)
        at com.sun.jimi.core.Jimi.createRasterImage(Jimi.java:929)
        at com.sun.jimi.core.Jimi.createRasterImage(Jimi.java:720)
        at com.sun.jimi.core.Jimi.createRasterImage(Jimi.java:699)
        at com.sun.jimi.core.Jimi.putImage(Jimi.java:587)
        at com.sun.jimi.core.Jimi.putImage(Jimi.java:575)
        at org.opensimpx.client.gui.ClientGUI.setupGUI(ClientGUI.java:216)
        at org.opensimpx.client.gui.ClientGUI.<init>(ClientGUI.java:147)
        at org.opensimpx.client.gui.ClientGUI.main(ClientGUI.java:419)
        at org.opensimpx.client.login.Login.startClient(Login.java:535)
        at org.opensimpx.client.login.Login.main(Login.java:340)

Is this because of how I handled my data, are the bytes bad, do I need 
to convert them back to a file since I put them into the database from a 
file?

Any help would be greatly appreciated,
Thanks in advanced.
Matt




Responses

pgsql-jdbc by date

Next:From: Russ McBrideDate: 2001-09-26 22:29:10
Subject: Re. conection error
Previous:From: Nick FankhauserDate: 2001-09-26 21:40:50
Subject: Re: connection error

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group