简体   繁体   中英

how to convert a java.sql.blob to a base64 encoded string in java

i am trying to get an image out of a mysql database where it is stored as a blob. so far i have tried three more or less different ways, non of which seem to take me anywhere. i must be making some stupid mistake here.

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

in all cases the console gives me this:
系统错误

The Exception with null in getMessage() and getCause() is the NullPointerException.

The JavaDoc for ResultSet#getBinaryStream() states that it returns null for the column containing SQL NULL. Even though the JavaDoc for the ResultSet#getBlob() omits to mention the same, it looks to me that the test rows do not contain any data in the blob column, ie the blob is NULL in database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM