Exportar imagem do banco de dados para arquivo jpg

1 resposta
W

Boa tarde, alguém pode me ajudar a retirar imagens do banco de dados?

estou precisando pegar as imagens que foram salvar no Banco de dados SQL, e salvá-las no meu PC.

se alguém poder ajudar agradeço.

1 Resposta

W

Consegui exportar, vou postar o código.

public static void main(String[] args) throws IOException, Exception {
        Session session = new ConnectionUtil().getSession();
        Associate a = (Associate) session.load(Associate.class, 4171019L);
        ConnectionRS con = new ConnectionRS(session);


        File f;
        FileOutputStream fos = null;
        InputStream in;
        Statement st = null;
        ResultSet rs = null;
        byte buffer[];
        final int bufferSize = 2048;
        int bytesLidos;

        
        try {
            /*
             * Cria o Statement e executa uma query
             */
            st = con.connection.createStatement();
            try {
                rs =
                        st.executeQuery(
                        "select PHOTO from ASSOCIATE where ID = 4171019");
            } catch (SQLException se) {
              
            }
            /*
             * Abre o arquivo para escrita
             */
            f = new File("C:\\Wallet_Printer\\Relatorio\\image.jpg");
            fos = new FileOutputStream(f);
            /*
             * Se a query retornou algum resultado
             */
            if (rs.next()) {
                /*
                 * Pega a stream do objeto desejado
                 */
                in = rs.getBinaryStream("PHOTO");
                /*
                 * Se for diferente de nulo
                 */
                if (in != null) {
                    /*
                     * Cria o buffer
                     */
                    buffer = new byte[bufferSize];
                    /*
                     * Faça...
                     */
                    do {
                        /*
                         * Lê da InputStream
                         */
                        bytesLidos = in.read(buffer);
                        
                        /*
                         * Se leu mais que 0 bytes
                         */
                        
                        if (bytesLidos > 0) {
                            /*
                             * Escreve a quantidade lida no arquivo
                             */
                            fos.write(buffer, 0, bytesLidos);
                        }
                    } /*
                     * Enquanto os bytesLidos forem maiores que 0
                     */ while (bytesLidos > 0);
                } else {
                    /*
                     * Se a InputStream for nula, é um arquivo vazio
                     */
                }
            }
            /*
             * Mostra uma mensagem e fecha tudo
             */
            fos.close();
            st.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fos.close();
            } catch (Exception e) {
            }
            try {
                st.close();
            } catch (Exception e) {
            }
        }
    }

Alguém pode me ajudar a diminuir o DPI da imagem? e setar o tamanho dela.

Obrigado.

Criado 29 de agosto de 2012
Ultima resposta 29 de ago. de 2012
Respostas 1
Participantes 1