从MYSQL数据库检索存储为BLOB的图像

16

我正在尝试基于数据库中的信息创建PDF。现在我需要从Java中从存储为BLOB的TIFF图像的mysql数据库检索它。但我不知道该怎么做。我找到的示例展示了如何检索并将其保存为文件(但在磁盘上),而我需要它驻留在内存中。

表名:IMAGENES_REGISTROS

BLOB字段名称:IMAGEN

有任何想法吗?


是的,Bozho,使用Java的MySQL库进行普通JDBC。 - Sheldon
请参见我的更新答案——结果发现有另一种方法可以做到 :) - Bozho
4个回答

21

在你的ResultSet调用中:

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

或者,你可以调用:

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

正如BalusC在评论中所指出的,你最好使用:

InputStream binaryStream = resultSet.getBinaryStream(yourBlobColumnIndex);

然后,代码将取决于您要如何读取和嵌入图像。


谢谢,只是一个评论。imageBlob返回一个长整型。而getBytes需要一个整型。我解析(Integer.ParseInt..)长整型,它可以工作。我不知道最终是否会带来问题。还有其他方法吗?谢谢 - Sheldon
3
请使用ResultSet#getBinaryStream()方法,不要使用ResultSet#getBlob()ResultSet#getBytes()方法。 - BalusC
这只是一种方便的方法,还是其中有更多的含义? - Bozho
我猜是因为预计数据量很大,所以会出现内存溢出异常。 - Bozho

2
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);

尝试使用以下代码从Netbeans中的博客Mysql获取可调整大小的图像:

这并不明显如何创建PDF,或者如何匹配原始帖子中的数据库参数。 - rwp
图像变量是谁? - gumuruh

2
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();
            }
        }
    }

2
你应该编辑你的答案,将那些信息包含进去,而不是添加评论。更多的解释是好的——仅有代码的答案是不被鼓励的。 - Ajean

1
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);               
            }
        }
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接