java - 如何在 java 中将 java.sql.blob 转换为 base64 编码的字符串

标签 java mysql base64 blob

我正在尝试从 mysql 数据库中获取图像,图像存储为 blob。到目前为止,我已经尝试了三种或多或少不同的方式,但似乎没有一种方式能带我到任何地方。我一定是在这里犯了一些愚蠢的错误。

    try {
          // create a java mysql database connection
          String myDriver = "org.gjt.mm.mysql.Driver";
          Class.forName(myDriver);
          Connection conn = DriverManager.getConnection(mySettings.dbConnect, mySettings.dbUser, mySettings.dbPass);
          PreparedStatement stmt = conn.prepareStatement("select up.userid, up.name, la.languages, up.photo, up.dob, up.edited from userprofile up join languages la on up.languages_id = la.languages_id where up.userid = ?");

          stmt.setString(1, userid);
          ResultSet rs = stmt.executeQuery();
          while (rs.next()) {
              // version 1
              InputStream photois = rs.getBinaryStream("photo");
              int ch;
              String str = new String();
              while ((ch = photois.read()) != -1) {
                  // here the exception occures when i InputStream.read().
                  // both Exception.getMessage() and Exception.getCause()
                  // of the caught exception yield null
                  str += (char)ch;
              }
              byte[] bdata=str.getBytes();
              byte[] img64 = Base64.encode(bdata);
              String photo64 = new String(img64);

              // version 2
              Blob b = rs.getBlob("photo");
              byte[] ba = b.getBytes(1, b.length()); 
              // b.length() throws exception, no message, no cause
              byte[] img64 = Base64.encode(ba);
              String photo64 = new String(img64);

              // version 3
              InputStream photois = rs.getBinaryStream("photo");
              byte[] buf = IOUtils.toByteArray(photois); 
              // this throws an exception, message and cause both null as above 
              String photo64 = DatatypeConverter.printBase64Binary(buf);

          }
          conn.close();
    }
    catch (Exception e) {
          System.err.println("Got an exception! (reading userprofile from db)");
          System.err.println(e.getMessage());
          System.err.println(e.getCause());
    }

在所有情况下,控制台都会给我这个:
System.err

最佳答案

getMessage() 和 getCause() 中为 null 的异常是 NullPointerException。

ResultSet#getBinaryStream() 的 JavaDoc声明它为包含 SQL NULL 的列返回 null。尽管 ResultSet#getBlob() 的 JavaDoc省略相同的内容,在我看来,测试行不包含 blob 列中的任何数据,即 blob 在数据库中为 NULL。

关于java - 如何在 java 中将 java.sql.blob 转换为 base64 编码的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26402487/

相关文章:

java - Java FileWriter 的 "Persistent streams"?

mysql - 使用条件从分区的 Parquet 数据创建表

base64 - 将 Base64 编码转换为 XLSX

java - 如何使用 Java 中的 secret Base64 编码创建 JWT

java - 如何从 Matrix 光标填充 ListView

java - SmartGwt RPC服务(com.server.GreetingServiceImpl类型没有可用的源代码;您是否忘记继承所需的模块?)

Java - Apache POI - 使用循环填充行和单元格时遇到问题(Excel)

mysql - MySQL 中的错误 1064 (42000)

mysql - 从一个表中的字段中检索最高频率值并将其更新到另一个表中

ios - Base64 字符串解码后,Swift 无法识别/n 换行符