如何使用java在mongoDB中插入图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4245787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to insert images in mongoDB using java?
提问by
I want to store image documents in MongoDB. I am using java.
我想在 MongoDB 中存储图像文档。我正在使用Java。
Any links or suggestions would be appreciated.
任何链接或建议将不胜感激。
采纳答案by Edward83
For storing binary data like images you can use GridFSor implement your own realization; Download the driverand look at src/test/com/mongodb/gridfs/GridFSTest.java
;)
为了存储像图像这样的二进制数据,您可以使用GridFS或实现您自己的实现;下载驱动看看src/test/com/mongodb/gridfs/GridFSTest.java
;)
Edit: you are lucky today! I made complete code for you;) Enjoy!
编辑:你今天很幸运!我为你制作了完整的代码;) 享受吧!
package mongodb.testing.java;
import com.mongodb.*;
import com.mongodb.gridfs.*;
import java.io.*;
public class Main {
public static byte[] LoadImage(String filePath) throws Exception {
File file = new File(filePath);
int size = (int)file.length();
byte[] buffer = new byte[size];
FileInputStream in = new FileInputStream(file);
in.read(buffer);
in.close();
return buffer;
}
public static void main(String[] args) throws Exception {
//Load our image
byte[] imageBytes = LoadImage("C:/Temp/bear.bmp");
//Connect to database
Mongo mongo = new Mongo( "127.0.0.1" );
String dbName = "GridFSTestJava";
DB db = mongo.getDB( dbName );
//Create GridFS object
GridFS fs = new GridFS( db );
//Save image into database
GridFSInputFile in = fs.createFile( imageBytes );
in.save();
//Find saved image
GridFSDBFile out = fs.findOne( new BasicDBObject( "_id" , in.getId() ) );
//Save loaded image from database into new image file
FileOutputStream outputImage = new FileOutputStream("C:/Temp/bearCopy.bmp");
out.writeTo( outputImage );
outputImage.close();
}
}
回答by theTuxRacer
Well, AFAIK, you cant store images in mongodb, you can store the links to images. And I am not high as I say this, if the images are small, like 100x100 px, U can try storing the image in binary, and reform and image on the fly. But, it will take some time to render the images, and hence I suggest you to save the link, and just fetch that image and populate it dynamically.
好吧,AFAIK,你不能在 mongodb 中存储图像,你可以存储图像的链接。而且我不是这么说的,如果图片很小,比如100x100像素,你可以尝试将图片存储为二进制,并在飞行中进行图像改造。但是,渲染图像需要一些时间,因此我建议您保存链接,然后获取该图像并动态填充它。
You can use GridFS to store larger binary objects.
您可以使用 GridFS 来存储更大的二进制对象。
Since you are new to mongoDB, take a look at:
由于您是 mongoDB 的新手,请看一下:
You can also always ask on SO, I am also new to mongoDB, and wouldn't mind helping a newbie along :D
您也可以随时询问 SO,我也是 mongoDB 的新手,不介意帮助新手:D
回答by Scott Hernandez
For small (<1mb) images I'd suggest storing them in a binary field (byte[] in java); if you have larger files GridFS will work better.
对于小(<1mb)图像,我建议将它们存储在二进制字段中(java 中的 byte[]);如果您有更大的文件,GridFS 会更好地工作。
I store lots of thumbnails in my documents directly using a simple binary field.
我使用简单的二进制字段直接在我的文档中存储了大量缩略图。
I use Morphia (http://code.google.com/p/morphia) to store my POJOs.
我使用 Morphia (http://code.google.com/p/morphia) 来存储我的 POJO。