博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java MongoDB : Save image example
阅读量:7025 次
发布时间:2019-06-28

本文共 4214 字,大约阅读时间需要 14 分钟。

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.

1. Save image

Code snippets to save an image file into MongoDB, under “photo” namespace, and assign a new “filename” for the saved image.

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 }

Code snippets to get all the saved files from MongoDB and iterate it with DBCursor.

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.

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.

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

转载于:https://www.cnblogs.com/ghgyj/p/4847474.html

你可能感兴趣的文章
goto语句的升级版,setjmp,longjmp
查看>>
CentOS7使用firewalld打开关闭防火墙与端口[转]
查看>>
Eclipse-Java代码规范和质量检查插件-Checkstyle
查看>>
c语言中各种数据类型的长度
查看>>
TEST mathjax
查看>>
修改web项目的启动页
查看>>
居中显示
查看>>
Java的不定参数(eg:Object...)(转)
查看>>
[编程] C语言循环结构计算π的值
查看>>
C/C++下scanf的%匹配以及过滤字符串问题
查看>>
全内存的redis用习惯了?那能突破内存限制类redis产品ssdb呢?
查看>>
17秋 软件工程 第六次作业 Beta冲刺
查看>>
html5--6-23 CSS3中的文字与字体
查看>>
使用腾讯云无服务器云函数(SCF)分析天气数据
查看>>
Android系统编译错误Note: Some input files use or override a deprecated API. 解决办法【转】...
查看>>
Redis进阶实践之四Redis的基本数据类型
查看>>
006-Spring Boot自动配置-Condition、Conditional、Spring提供的Conditional自动配置
查看>>
URAL Palindromic Contest
查看>>
Spring Cloud启动应用时指定IP或忽略某张网卡配置
查看>>
RequireJS 和 Sea.js
查看>>