【问题标题】:How to convert Blob to String and String to Blob in java如何在java中将Blob转换为字符串和字符串转换为Blob
【发布时间】:2013-06-28 08:09:03
【问题描述】:

我正在尝试使用

从 BLOB 数据类型中获取字符串
Blob blob = rs.getBlob(cloumnName[i]);
byte[] bdata = blob.getBytes(1, (int) blob.length());
String s = new String(bdata);

它工作正常,但是当我要将 String 转换为 Blob 并尝试插入数据库时​​,没有任何东西插入数据库。我使用以下代码将 String 转换为 Blob:

String value = (s);
byte[] buff = value.getBytes();
Blob blob = new SerialBlob(buff);

谁能帮我在Java中将Blob转换为StringString转换为Blob

【问题讨论】:

  • 首先,您需要确保使用正确的术语:它是 blob,而不是博客。当您使用正确的名称时,您将通过网络搜索获得更多信息。其次,blob 用于 binary 数据,而不是 text 数据。理想情况下,您首先不应该将它们用于文本数据,如果这样做,您应该在将文本数据转换为二进制数据时指定编码(例如 UTF-8)。

标签: java jdbc blob


【解决方案1】:

试试这个(a2 是 BLOB col)

PreparedStatement ps1 = conn.prepareStatement("update t1 set a2=? where id=1");
Blob blob = conn.createBlob();
blob.setBytes(1, str.getBytes());
ps1.setBlob(1, blob);
ps1.executeUpdate();

即使没有 BLOB 也可以工作,驱动程序会自动转换类型:

   ps1.setBytes(1, str.getBytes);
   ps1.setString(1, str);

此外,如果您使用文本 CLOB 似乎是一种更自然的 col 类型

【讨论】:

    【解决方案2】:

    使用它来将 String 转换为 Blob。其中 connection 是与 db 对象的连接。

        String strContent = s;
        byte[] byteConent = strContent.getBytes();
        Blob blob = connection.createBlob();//Where connection is the connection to db object. 
        blob.setBytes(1, byteContent);
    

    【讨论】:

      【解决方案3】:

      您如何将 blob 设置为 DB? 你应该这样做:

       //imagine u have a a prepared statement like:
       PreparedStatement ps = conn.prepareStatement("INSERT INTO table VALUES (?)");
       String blobString= "This is the string u want to convert to Blob";
      oracle.sql.BLOB myBlob = oracle.sql.BLOB.createTemporary(conn, false,oracle.sql.BLOB.DURATION_SESSION);
       byte[] buff = blobString.getBytes();
       myBlob.putBytes(1,buff);
       ps.setBlob(1, myBlob);
       ps.executeUpdate();
      

      【讨论】:

      • 首先,OP从未规定他使用的是Oracle RDMS。如果他不是,这段代码将无法编译。
      【解决方案4】:

      在 Java 中将 Blob 转换为字符串:

      byte[] bytes = baos.toByteArray();//Convert into Byte array
      String blobString = new String(bytes);//Convert Byte Array into String
      

      【讨论】:

        【解决方案5】:

        这是我的解决方案,它总是对我有用

        StringBuffer buf = new StringBuffer();
        String temp;
        BufferedReader bufReader = new BufferedReader(new InputStreamReader(myBlob.getBinaryStream()));
            while ((temp=bufReader.readLine())!=null) {
                bufappend(temp);
            }
        

        【讨论】:

          【解决方案6】:

          这是我的解决方案

          从数据库中检索 blob 列并将其传递给下面的方法。

           public static String blobToString(BLOB blob) throws Exception {
          
                  byte[] data = new byte[(int) blob.length()];
                  BufferedInputStream instream = null;
                  try {
                  instream = new BufferedInputStream(blob.getBinaryStream());
                  instream.read(data);
                  } catch (Exception ex) {
                  throw new Exception(ex.getMessage());
                  } finally {
                  instream.close();
                  }
                  
                  int chunk = 65536;
                  ByteArrayInputStream bis = new ByteArrayInputStream(data);
                  GZIPInputStream gis = new GZIPInputStream(bis);
          
                   int length = 0;
                  byte[] buffer = new byte[chunk];
                  ByteArrayOutputStream bos = new ByteArrayOutputStream();
                  while ((length = gis.read(buffer, 0, chunk)) != -1) {
                  bos.write(buffer, 0, length);
                  }
          
                  gis.close();
                  bis.close();
                  bos.close();
                  
                  String str = bos.toString();
                  System.out.println(str);
                  return str;
          
              }
          

          【讨论】:

            猜你喜欢
            • 2017-07-07
            • 1970-01-01
            • 1970-01-01
            • 2020-01-19
            • 2015-12-20
            • 2012-05-30
            • 1970-01-01
            • 2023-01-25
            • 2015-07-02
            相关资源
            最近更新 更多