【问题标题】:Retrieve an Image stored as BLOB on a MYSQL DB检索在 MYSQL DB 上存储为 BLOB 的图像
【发布时间】:2010-01-27 21:09:14
【问题描述】:

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

表名:IMAGENES_REGISTROS

BLOB 字段名称:IMAGEN

有什么想法吗?

【问题讨论】:

  • 是的 Bozho,带有用于 java 的 MySQL 库的普通 JDBC。
  • 查看我的更新答案 - 原来有另一种方法:)

标签: java mysql image jdbc blob


【解决方案1】:

在您的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 返回一个 Long。 getBytes 需要一个整数。我解析(Integer.ParseInt..)Long,它可以工作。我不知道这最终会不会带来问题。还有其他方法吗?谢谢
  • 不要使用ResultSet#getBlob()ResultSet#getBytes()。只需使用ResultSet#getBinaryStream()
  • 它只是一种方便的方法,还是有更多的东西?
  • 我猜是因为预计数据量很大,你会得到 OutOfMemory 异常
【解决方案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 或匹配原始帖子中的数据库参数并不明显。
  • 图片变量是谁?
【解决方案3】:
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();
            }
        }
    }

【讨论】:

  • 您应该编辑您的答案以包含该信息,而不是添加评论。更多解释是好的 - 不鼓励仅使用代码的答案。
【解决方案4】:
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);               
            }
        }
    }

【讨论】:

    猜你喜欢
    • 2013-09-29
    • 2011-08-03
    • 2015-11-21
    • 2013-07-19
    • 1970-01-01
    • 2016-12-02
    • 2016-08-23
    • 2019-10-03
    相关资源
    最近更新 更多