Odczyt obrazu z bazy danych

0

Witam.

Usiłuję odczytać obraz zapisany w bazie danych w polu typu BLOB;

Zapis obrazu do bazy:

 
         try {
            File file = new File("E:\\smile.png");
            InputStream inputImage = new FileInputStream(file);
            pstmt.setBinaryStream(1 ,inputImage, (int)(inputImage.available()));
            pstmt.setInt(2, id);
            pstmt.execute();
        } catch (IOException ex) {
            Logger.getLogger(Contacts.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Contacts.class.getName()).log(Level.SEVERE, null, ex);
        }

Odczyt:

                InputStream imageIn = rs.getBinaryStream("zdjecie");
                
                if(imageIn != null)
                {
                    long length = imageIn.available();
                    byte[] bytes = new byte[(int) length];
                    imageIn.read(bytes);
                }
 

Wynikiem odczytu obrazu z bazy jest:

[B@1d48a65

Nic więcej nie potrafię odczytać z tego pola.

Nawet stosując metodę getBlob, konwertując Blob-a do byte[] wartość nie zmienia się.

Macie jakieś pomysły na temat tego co robię źle?

Pozdrawiam

1

http://download.oracle.com/javase/6/docs/api/java/io/InputStream.html#available%28%29:

Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream. The next invocation might be the same thread or another thread. A single read or skip of this many bytes will not block, but may read or skip fewer bytes.

Note that while some implementations of InputStream will return the total number of bytes in the stream, many will not. It is never correct to use the return value of this method to allocate a buffer intended to hold all data in this stream.

Czyli metoda imageIn.available nie musi zwracać długości całego strumienia i zwykle implementacje tej metody tego nie robią. Przydziel jakiś stały bufor danych.

0 użytkowników online, w tym zalogowanych: 0, gości: 0