【问题标题】:Save an object with image ( save both object data and image too) inside mongoDB using Java使用Java在mongoDB中保存带有图像的对象(也保存对象数据和图像)
【发布时间】:2013-05-24 10:18:50
【问题描述】:

我想具体了解如何保存其中包含图像的对象。我想要做的是保存一个包含图像的整个对象,必须保存图像。我试过这个,但它只保存带有文件路径的文件实例。它不保存图像。任何帮助,将不胜感激。谢谢你。这是我保存对象的代码,但它保存的是文件实例而不是图像。

import java.io.File;    
import org.springframework.data.mongodb.core.mapping.Document;
import com.discusit.model.Artwork;

@Document(collection="Artwork")
public class ArtworkImpl implements Artwork {

    private String artworkName;
    private String artworkVersion;
    private String fileName;
    private File file;

    public ArtworkImpl() {

    }

    public ArtworkImpl(String name, String version, String fileName, File file) {
        this.artworkName = name;
        this.artworkVersion = version;
        this.fileName = fileName;
        this.file = file;
    }

    public String getArtworkName() {
        return artworkName;
    }

    public void setArtworkName(String artworkName) {
        this.artworkName = artworkName;
    }

    public String getArtworkVersion() {
        return artworkVersion;
    }

    public void setArtworkVersion(String artworkVersion) {
        this.artworkVersion = artworkVersion;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }   
}

这是我的主要方法:- 注意:主要方法工作正常,但不保存图像,而是保存文件实例。

public class MainApplication {

    public static void main(String[] args) {

    ApplicationContext ctx = 
                     new AnnotationConfigApplicationContext(SpringMongoConfig.class);
    GridFsOperations gridOperations = 
                      (GridFsOperations) ctx.getBean("gridFsTemplate");

    DBObject metaData = new BasicDBObject();
    metaData.put("extra1", "anything 1");
    metaData.put("extra2", "anything 2");

    InputStream inputStream = null;
    try {
        inputStream = new FileInputStream("/home/discusit/Downloads/birds.jpg");
        gridOperations.store(inputStream, "birds.jpg", "image/jpg", metaData);

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

        System.out.println("Done");
    }

}

我想用图像保存对象。

更新:我这样做了,但是通过将图像转换为字节数组并获取字节数组并转换回图像,只是想知道有没有其他方法可以直接在 mongoDB 中保存图像但不将其转换为字节数组???

【问题讨论】:

    标签: spring mongodb mongodb-java


    【解决方案1】:

    关于 mongoDB,您需要澄清以下几点:

    1. MongoDB 是一个面向文档的数据库,其中文档以称为 BSON 的格式存储,并且限制为最大 16MB。 “将 BSON 视为 JSON(JavaScript 对象表示法)文档的二进制表示”[1]。

    BSON 格式支持 BinData 类型,您可以在其中存储文件的二进制表示,只要不超过 16MB 的限制。

    2. MongoDB 提供了一种存储文件的方法 GridFS “GridFS 是一种用于存储和检索超过 BSON 文档大小限制为 16MB 的文件的规范”[2]。

    GridFS 将文件分成 256K 的块,并使用两个集合来存储文件,一个用于元数据,一个用于文件块,这些集合称为 fs.filesfs。块分别。

    存储在这些集合中的文件如下所示:

    >db.fs.files.find()
    {
      "_id": ObjectId("51a0541d03643c8cf4122760"),
      "chunkSize": NumberLong("262144"),
      "length": NumberLong("3145782"),
      "md5": "c5dda7f15156783c53ffa42b422235b2",
      "filename": "test.png",
      "contentType": "image/bmp",
      "uploadDate": ISODate("2013-05-25T06:03:09.611Z"),
      "aliases": null,
      "metadata": {
        "extra1": "anything 1",
        "extra2": "anything 2"
      }
    }
    
    
    >db.fs.chunks.find()
    {
    "_id": ObjectId("51a0541e03643c8cf412276c"),
      "files_id": ObjectId("51a0541d03643c8cf4122760"),
      "n": 11,
      "data": BinData(0, "BINARY_DATA_WILL_BE_STORED_HERE")
    }
    .
    .
    

    注意文件集合中的 ObjectId 如何与块集合中的 files_id 匹配。

    在此澄清之后,您的问题的简短答案是:

    是的,您可以使用 GridFS 将文件直接存储在 mongoDB 中。

    在以下链接中,您可以找到使用 Spring Data 的 GridFS 工作示例:

    http://www.mkyong.com/mongodb/spring-data-mongodb-save-binary-file-gridfs-example/

    [1]http://docs.mongodb.org/manual/reference/glossary/#term-bson

    [2]http://docs.mongodb.org/manual/core/gridfs/

    【讨论】:

    • 感谢您回答问题,我知道我可以使用 GridFS 存储文件,但我想做的是在该对象中存储一个具有多个该人图像的人的对象。我可以这样做吗???我已经成功尝试了你在 mkyong.com 中提到的内容...... :)
    • 我也找到了一种解决方法,我所做的是将图像转换为 byte[] 然后将其存储,然后将 byte[] 转换为图像...但是我可以存储整个对象吗一口气把里面的图片放进去???还是有其他建议???
    • 如果您不想使用 GridFS,那么您可以将文件的二进制表示形式存储在 BSON 文档中,注意 16MB 的大小限制。如果您正在寻找一种只需一步完成的方法,那么您可以使用 CustomConverter 在 DBobjects 和自定义对象之间进行映射,请查看以下链接以获取相应的 Spring 文档:static.springsource.org/spring-data/data-mongodb/docs/current/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-26
    • 2018-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-22
    相关资源
    最近更新 更多