java - java中如何将String转换为blob?

标签 java blob

我有一个包含大邮件正文的字符串。我需要将它以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 列中。如何做到这一点??

最佳答案

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

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

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

关于java - java中如何将String转换为blob?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32425023/

相关文章:

java - @WebServlet 注释无法识别;初始化不运行

java - 使用 Chilli Source 模型导出器导出空或基于节点的 blender 模型

mysql - 比较存储在 mysql 数据库中的图像

命中 "a limit"(MyISAM) 后 Mysql BLOB 性能下降

php - Zend/PHP : Problem uploading/downloading file to/from MySQL's BLOB field

java - 更新 xml 标记的属性

java - 休息服务 : The proper way of returning exception

c++ - OpenCV中的 Blob 提取

java - 了解 Java 中的事件和监听器

c++ - 从 blob SQLite 读取二进制图像并使用 OpenCV imdecode 对其进行解码的最佳方法是什么?