【问题标题】:Binary to String in MongoDb JavaMongoDb Java中的二进制到字符串
【发布时间】:2017-09-26 12:23:13
【问题描述】:

您好,我正在使用 MongoDb 数据库,我必须将网站的所有内容以二进制形式放入 MongoDb 数据库中。现在在 servlet 部分,我需要获取 id 并将 bson 二进制转换为 String 以写入 iFrame。如何使用 Java 在 MongoDb 中将二进制转换为字符串?

JAVA 代码。

    //Not quite working.

    public String giveSelected(String id){
    MongoClient mongoClient = new MongoClient("localhost",27017);
    MongoDatabase database = mongoClient.getDatabase("dbTest");
    MongoCollection<Document> collection = database.getCollection("colTest");
    Document myDoc = collection.find(eq("_id", id)).first();
    String str=myDoc.getString("content");
    return str;
}

JSP 调用:

 //str is the String form of needed content Binary.
 <script>
 function iFramefunc(){
 var s = document.getElementById('iframe');
 s.contentDocument.documentElement.innerHTML="<%=str%>";
 s.contentDocument.close();
 }
 </script>
 <iframe id="iframe" onload="iFramefunc()"></iframe>

【问题讨论】:

  • 这主要取决于创建您网站的二进制表示的代码。那么,这部分是如何工作的呢?你为什么不直接逆转呢?顺便说一句,这听起来像是一种“有趣”的方法,将网站作为 BLOB 存储在数据库中。
  • 二进制在 MongoDb 中存储的数据较少,它会自动将它们带到另一个程序中。我所需要的只是将它们作为二进制对象并将其更改为字符串。
  • 仍然:本质在另一个程序中自动获取它们 ...该过程定义了数据库的内容。当您不理解时,您无法反转该操作。也许有base64编码,也许有压缩,谁知道呢。

标签: java mongodb bson


【解决方案1】:

您可能还想压缩内容并存储压缩的二进制数据。这是一个例子:

private static byte[] compress(String str) throws IOException {
    ByteArrayOutputStream obj = new ByteArrayOutputStream();
    GZIPOutputStream gzip = new GZIPOutputStream(obj);
    gzip.write(str.getBytes("UTF-8"));
    gzip.close();
    byte[] compressedString = obj.toByteArray();
    return compressedString;
}

private static String uncompress(byte[] compressedContent) throws IOException {
    GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(compressedContent));
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(gis, "UTF-8"));
    String line;
    StringBuilder out = new StringBuilder();
    while ((line = bufferedReader.readLine()) != null) {
        out.append(line);
    }  
    return out.toString();
}

private static void readWrite() throws IOException {
    MongoClient mongoClient = new MongoClient("localhost",27017);
    MongoDatabase database = mongoClient.getDatabase("dbTest");
    MongoCollection<Document> collection = database.getCollection("colTest");
    collection.drop();

    // Write
    String str = "aaaaaaaaaaaaaaaaaaaaBBBBBBBBBBBBBBBBBBBBBBBBBBBccccccccccccccccccc";
    byte[] compressedString = compress(str);
    String id = "abc";
    Document doc = new Document("content", compressedString).append("_id", id);
    collection.insertOne(doc);

    // Read
    Document myDoc = collection.find(eq("_id", id)).first();
    byte[] compressedContent = ((Binary)myDoc.get("content")).getData();
    String content = uncompress(compressedContent);
    System.out.println(content);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-15
    • 2012-11-20
    • 2014-04-28
    • 2011-05-09
    • 1970-01-01
    • 2016-07-25
    • 1970-01-01
    相关资源
    最近更新 更多