检索在 MYSQL DB 上存储为 BLOB 的图像

发布于 2024-08-19 08:59:27 字数 200 浏览 3 评论 0原文

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

表名称:IMAGENES_REGISTROS

BLOB 字段名称:IMAGEN

有什么想法吗?

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. And I don't know how to do it. The examples I've found shows how to retrieve it and save it as a File (but on disk) and I needed to reside on memory.

Table name: IMAGENES_REGISTROS

BLOB Field name: IMAGEN

Any Ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

默嘫て 2024-08-26 08:59:28

在您的 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);

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

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.

知你几分 2024-08-26 08:59:28
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 获取可调整的图像

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

长安忆 2024-08-26 08:59:28
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();
            }
        }
    }
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();
            }
        }
    }
羁〃客ぐ 2024-08-26 08:59:28
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);               
            }
        }
    }
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);               
            }
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文