原文出自:http://www.mkyong.com/mongodb/java-mongodb-save-image-example/
返回目录:http://ysj5125094.iteye.com/blog/2192754
Java MongoDB : Save image example
In this tutorial, we show you how to save an image file into MongoDB, via GridFS API. The GridFS APIs are able to serve other binary files as well, like video and music files.
译:在本教程中,我们将向你展示如何通过 GridFS API 保存一个图片到MongoDB。GridFS APIs 提供将其他二进制文件的支持,比如视频和音频等。
Note
For detail explanation, read this MongoDB GridFS manual.
译:详细解释,请阅读MongoDB GridFS 文档.
1. Save image
Code snippets to save an image file into MongoDB, under “photo” namespace, and assign a new “filename” for the saved image.
译:下面代码片断为保存一个图片文件到MongoDB,在"photo"命名空间下,将图片文件取一个新名保存。
String newFileName = "mkyong-java-image"; File imageFile = new File("c:\\JavaWebHosting.png"); GridFS gfsPhoto = new GridFS(db, "photo"); GridFSInputFile gfsFile = gfsPhoto.createFile(imageFile); gfsFile.setFilename(newFileName); gfsFile.save();
2. Get image
Code snippets to get the saved image by its “filename”.
译:下面代码片断,根据文件名,获取保存的图片。
String newFileName = "mkyong-java-image"; GridFS gfsPhoto = new GridFS(db, "photo"); GridFSDBFile imageForOutput = gfsPhoto.findOne(newFileName); System.out.println(imageForOutput);
Output, the image is saved as following JSON format.
{ "_id" : { "$oid" : "4dc9511a14a7d017fee35746" } , "chunkSize" : 262144 , "length" : 22672 , "md5" : "1462a6cfa27669af1d8d21c2d7dd1f8b" , "filename" : "mkyong-java-image" , "contentType" : null , "uploadDate" : { "$date" : "2011-05-10T14:52:10Z" } , "aliases" : null }
3. Print all saved images
Code snippets to get all the saved files from MongoDB and iterate it with DBCursor.
译:下面代码片断,从MongoDB中获取所有保存的文件,并用数据库游标迭代输出。
GridFS gfsPhoto = new GridFS(db, "photo"); DBCursor cursor = gfsPhoto.getFileList(); while (cursor.hasNext()) { System.out.println(cursor.next()); }
4. Save into another image
Code snippets to get an image file from MongoDB and output it to another image file.
译:下面代码片断,从MongoDB中获取一个图片文件并输出(生成另一个图片)。
String newFileName = "mkyong-java-image"; GridFS gfsPhoto = new GridFS(db, "photo"); GridFSDBFile imageForOutput = gfsPhoto.findOne(newFileName); imageForOutput.writeTo("c:\\JavaWebHostingNew.png"); //output to new file
5. Delete image
Code snippets to delete an image file.
译:下面代码片断,删除一个图片文件。
String newFileName = "mkyong-java-image"; GridFS gfsPhoto = new GridFS(db, "photo"); gfsPhoto.remove(gfsPhoto.findOne(newFileName));
Full Example
Full example to work with image, via Java MongoDB GridFS API. See comments for explanation.
注:运行程序之前,一定要在C盘创建“c:\\JavaWebHosting.png"图片文件。
package com.mkyong.core; import java.io.File; import java.io.IOException; import java.net.UnknownHostException; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.Mongo; import com.mongodb.MongoException; import com.mongodb.gridfs.GridFS; import com.mongodb.gridfs.GridFSDBFile; import com.mongodb.gridfs.GridFSInputFile; /** * Java MongoDB : Save image example * */ public class SaveImageApp { public static void main(String[] args) { try { Mongo mongo = new Mongo("localhost", 27017); DB db = mongo.getDB("imagedb"); DBCollection collection = db.getCollection("dummyColl"); String newFileName = "mkyong-java-image"; File imageFile = new File("c:\\JavaWebHosting.png"); // create a "photo" namespace GridFS gfsPhoto = new GridFS(db, "photo"); // get image file from local drive GridFSInputFile gfsFile = gfsPhoto.createFile(imageFile); // set a new filename for identify purpose gfsFile.setFilename(newFileName); // save the image file into mongoDB gfsFile.save(); // print the result DBCursor cursor = gfsPhoto.getFileList(); while (cursor.hasNext()) { System.out.println(cursor.next()); } // get image file by it's filename GridFSDBFile imageForOutput = gfsPhoto.findOne(newFileName); // save it into a new image file imageForOutput.writeTo("c:\\JavaWebHostingNew.png"); // remove the image file from mongoDB gfsPhoto.remove(gfsPhoto.findOne(newFileName)); System.out.println("Done"); } catch (UnknownHostException e) { e.printStackTrace(); } catch (MongoException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
At the end of the program, a new image file is created in “c:\\JavaWebHostingNew.png“.
译:执行程序之后,会创建一个新的图片文件“c:\\JavaWebHostingNew.png“。
Reference
相关推荐
const url = 'http://example.com/image.jpg'; http.get(url, (res) => { let data = ''; res.on('data', (chunk) => { data += chunk; }); res.on('end', () => { fs.writeFile('image.jpg', data, (err) => ...
MongoEngine是一个针对Python的文档型数据库ORM(Object-Relational Mapping)库,它允许开发者以面向对象的方式操作MongoDB数据库。本教程通过构建一个简单的微博客应用,介绍MongoEngine的基本用法。 首先,安装...
## 什么是graylog Graylog 是一个简单易用、功能较全面的日志管理工具,相比 ELK 组合, 优点: ...Example: resource:\/posts\/45326 ``` ### 查询条件可以保存下来 使用 save search criteria 按钮
product_image_url = soup.find('img', class_='product-image').get('src') return product_name, product_price, product_image_url # 存储数据到CSV文件 def save_to_csv(data, filename): with open...
contentTypes: ['image/*'] // 只允许上传图片 } } }); ``` 配置完成后,我们需要在客户端实现文件选择和上传功能。在`client`目录下的JavaScript代码中,可以创建一个表单或使用HTML5的`<input type="file">`...
img.save("example.png") ``` 这段代码将`data`中的URL转换为一个二维码并保存为PNG图像。我们可以根据需求调整二维码的版本、错误校正级别、边框大小以及颜色。 接下来,我们需要搭建一个网站,让用户能够通过...