【问题标题】:How to convert a String to blob in java?如何在java中将字符串转换为blob?
【发布时间】:2021-07-26 15:20:28
【问题描述】:

我有一个包含大邮件正文的字符串。我需要将它以blob的形式保存到数据库中。如何在java中将其转换为blob? 下面的示例字符串:

<html><center> <body style='background-color: #e5e4e2;'> <table id='mainTable' style='background-color: #ffffff; width: 70%; height: auto;margin-left: auto; margin-right: auto;'><tr> <td> <table style='width: 100%;background-color: #2B547E; height: 50px; margin-top: 30px; margin-left: auto; margin-right: auto;'> <tr> <td width='50%'><img src="cid:image" height='50' width='150' style='padding-left: 10px;'></td> <td width='50%' style='text-align: right; color: white; font-family: verdana; font-size: 12px;'> </td></tr> </table> </td> </tr></table> <table id='mainTable1' style='background-color: #ffffff; width: 70%; height: auto;margin-left: auto; margin-right: auto;'> <tr> <td> <table style='width: 100%; height: 50px; margin-top: 30px; margin-left: auto; margin-right: auto;'><tr> <td width='50%' style='color:red;font-size:21px;'>Congratulations, Joselyn Valdes-Flores </td> <td width='50%' style='text-align: right; color: white; font-family: verdana; font-size: 12px;'> </td> </tr> <tr> <td style='font-size:18px;width:97%;font-style:italic'> This email confirms your Phone Interview  with Manager, Accounts Receivable  in   Burger King Corporation  You will be called at your mobile number </td> </tr> </table> </td> </tr> </table> <table  style='background-color: #ffffff; width: 70%; height: auto;margin-left: auto; margin-right: auto;'><tr style='color:green'> Interview LineUp <th style='color:gray;font-size:21px;text-align:left;font-style: italic'>Interviewe</th><th  style='color:gray;font-size:21px;text-align:left;font-style: italic'>Timing1</th><th  style='color:gray;font-size:21px;text-align:left;font-style: italic'>Timing2</th></tr><tr><td>Jeet Chatterjee</td><td>12:00am</td><td>2:00am</td></tr><tr><td>Ramu</td><td>2:00am</td><td>4:00am</td></tr></table><table id='mainTable2' style='background-color: #ffffff; width: 70%; height: auto;margin-left: auto; margin-right: auto; margin-top:12px;'><tr> <td style='font-size:18px; color:#2B547E;word-wrap: break-word;font-style: italic'> hii</td> </tr> </table> <table  style='background-color: #ffffff; width: 70%; height: auto;margin-left: auto; margin-right: auto;'> <tr> <td style='font-size:18px;color:#2B547E;word-wrap: break-word;font-style: italic'> Best Regards,</td><td style='font-size:24px;color:#2B547E;word-wrap: break-word;font-style: italic'> Jeet Chatterjee</td></tr> </table></body> </center> </html> 

我想将它保存在 blob 列中的数据库中。这个怎么办??

【问题讨论】:

  • 大多数数据库将任意“字节”保存为 BLOB,因此您只需将字符串的字节提供给数据库驱动程序即可。
  • 对于非二进制文本,CLOB 更适合数据库级别的文本操作。

标签: java blob


【解决方案1】:

您可以通过从String 获取byte[] 内容来做到这一点

byte[] byteData = yourString.getBytes("UTF-8");//Better to specify encoding
Blob blobData = dbConnection.createBlob();
blobData.setBytes(1, byteData);

另外,您可以直接将String存储到BLOB列。

【讨论】:

  • 为什么我需要一个 dbConnection?
  • @lucifer 要创建blob 实例,您可以使用数据库连接对象。
  • 没有数据库连接我不能制作 blob 对象?
  • @lucifer 不,除了将 blob 数据插入 blob 列之外,没有使用 Blob 实例。我知道的唯一方法是使用 db 连接对象创建 blob 实例。
  • @JoopEggen 谢谢,那会更优雅。
【解决方案2】:

您可以通过首先从String 获取byte[] 来做到这一点:

byte[] byteData = yourString.getBytes();

然后执行下一行代码:

Blob docInBlob = new SerialBlob(byteData);

Blob 来自 java.sql 包,SerialBlob 来自 javax.sql.rowset.serial package。

【讨论】:

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