Retrieve an Image stored as BLOB on a MYSQL DB

前端 未结 4 704
挽巷
挽巷 2020-11-27 16:37

I\'m trying to create a PDF based on the information that resides on a database. Know I need to retrieve a TIFF image that is stored as a BLOB on a mysql database from Java.

相关标签:
4条回答
  • 2020-11-27 17:13

    On your ResultSet call:

    Blob imageBlob = resultSet.getBlob(yourBlobColumnIndex);
    InputStream binaryStream = imageBlob.getBinaryStream(0, imageBlob.length());
    

    Alternatively, you can call:

    byte[] imageBytes = imageBlob.getBytes(1, (int) imageBlob.length());
    

    As BalusC noted in his comment, you'd better use:

    InputStream binaryStream = resultSet.getBinaryStream(yourBlobColumnIndex);
    

    And then the code depends on how you are going to read and embed the image.

    0 讨论(0)
  • 2020-11-27 17:20
    imagebytes = rs.getBytes("images");
    image=getToolkit().createImage(imageBytes);
    Image img = image.getScaledInstance(100,100,Image.SCALE_SMOOTH);
    ImageIcon icon=new ImageIcon(img);
    jLabel6.setIcon(icon);
    

    Try this code to get adjustable image from blog Mysql in netbeans

    0 讨论(0)
  • 2020-11-27 17:21
    private void loadFileDataBlobFromDataBase()
                 {
                List<Blob> bFile = jdbcTemplate.query(sql, new RowMapper<Blob>() {
                    @Override
                    public Blob mapRow(ResultSet rs, int rowNum)
                            throws SQLException {
                        return rs.getBlob(1);
                    }
                });
                if (bFile != null && bFile.size() > 0) {
                    bufReader = new BufferedReader(new InputStreamReader(bFile.get(
                            0).getBinaryStream()));
                }
                if (null != bufReader) {
                    dataVO record = null;
                    String lineStr = bufReader.readLine();
                    record = (dataVO) lineMapper.mapLine(lineStr, 1);               
                }
            }
        }
    
    0 讨论(0)
  • 2020-11-27 17:24
    final String dbURL = "jdbc:mysql://localhost:3306/portfolio";
        final String dbUser = "root";
        final String dbPass = "";
    
        Connection conn = null;
        Statement stmt = null;
    
        try {
            //DriverManager.registerDriver(new com.mysql.jdbc.Driver());
            Class.forName("com.mysql.jdbc.Driver");
    
            conn = DriverManager.getConnection(dbURL, dbUser, dbPass);
            System.out.println("db connected");
            stmt = (Statement) conn.createStatement();
    
            ResultSet rs1;
            rs1 = stmt.executeQuery("select profileImage from tbl_welcome where id = 1117");
    
            if (rs1.next()) {
                byte[] imgData = rs1.getBytes("profileImage");//Here....... r1.getBytes() extract byte data from resultSet 
                System.out.println(imgData);
                response.setHeader("expires", "0");
                response.setContentType("image/jpg");
    
                OutputStream os = response.getOutputStream(); // output with the help of outputStream 
                os.write(imgData);
                os.flush();
                os.close();
    
            }
        } catch (SQLException ex) {
            // String message = "ERROR: " + ex.getMessage();
            ex.printStackTrace();
        } finally {
            if (conn != null) {
                // closes the database connection
                try {
                    conn.close();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        }
    
    0 讨论(0)
提交回复
热议问题