简体   繁体   English

如何在java中将字符串转换为blob?

[英]How to convert a String to blob in java?

I have a String which contains a large mail body.我有一个包含大型邮件正文的字符串。 I need to save it to the database in the form of blob.我需要将它以blob的形式保存到数据库中。 How to convert it to the blob in java?如何将其转换为java中的blob Sample String below:下面的示例字符串:

<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> 

I want to save it in a database in the blob column.我想将它保存在 blob 列的数据库中。 How to do this??这个怎么做??

You can do it by getting byte[] content from String您可以通过从String获取byte[]内容来实现

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

Moreover, you can directly store String to BLOB column.此外,您可以直接将String存储到BLOB列。

You can do it by first getting the byte[] from String using:您可以通过首先使用以下方法从String获取byte[]来做到这一点:

byte[] byteData = yourString.getBytes();

and after that doing the next line of code:然后做下一行代码:

Blob docInBlob = new SerialBlob(byteData);

Blob is from java.sql package and SerialBlob is from javax.sql.rowset.serial package . Blob 来自java.sql包, SerialBlob 来自javax.sql.rowset.serial package

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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