1024programmer Java MongoDB combines with Spring to store files (pictures, audio, etc.)

MongoDB combines with Spring to store files (pictures, audio, etc.)

MongoDB has two ways to store images and other files (this article is aimed at users who can already use MONGODB to integrate Spring) Related reading: MongoDB backup and recovery http

MongoDB has two ways to store images and other files

(This article is aimed at users who can already use MONGODB to integrate Spring)

Related reading:

MongoDB backup and recovery

CentOS compile and install MongoDB

CentOS compiles and installs MongoDB and mongoDB’s php extension

CentOS 6 uses yum to install MongoDB and server-side configuration

Installing MongoDB2.4.3 under Ubuntu 13.04

How to create new databases and collections in MongoDB

A must-read for getting started with MongoDB (paying equal attention to concepts and practice)

“MongoDB: The Definitive Guide” English text version [PDF]

1.Use MongoTemplate

/**
* Storage file
* @param collectionName collection name
* @param file file
* @param fileid file id
* @param companyid The company id of the file
* @param filename file name
*/
public void SaveFile(String collectionName, File file, String fileid, String companyid, String filename) {
try {
DB db = mongoTemplate.getDb();
// Store the root node of fs
GridFS gridFS = new GridFS(db, collectionName);
GridFSInputFile gfs = gridFS.createFile( file);
gfs.put(“aliases”, companyid);
gfs.put(“filename”, fileid);
gfs.put(“contentType”, filename.substring(filename .lastIndexOf(“.”)));
gfs.save();
} catch (Exception e) {
e.printStackTrace();
System.out.println (“An error occurred while storing the file!!!”);
}
}

// Retrieve the file
public GridFSDBFile retrieveFileOne(String collectionName, String filename) {
try {
DB db = mongoTemplate.getDb();
// Get the root node of fs
GridFS gridFS = new GridFS(db, collectionName);
GridFSDBFile dbfile = gridFS.findOne(filename);
if (dbfile != null) {
return dbfile;
}
} catch (Exception e) {
// TODO: handle exception
}
return null;
}
//Sorry I can’t give you the project case, but this is the implementation code for accessing files from mongodb. I hope it can help. your turn.
//The inputStream can be obtained from GridFSDBFile, so you will understand.

2.Use GridFsTemplate

The first method found on the Internet is summarized very poorly, and the Spring combination does not feel close enough. I googled and referenced articles online.

Reference articles

Spring pseudo-configuration

<mongo:options connections-per-host="200"
threads-allowed-to-block-for -connection-multiplier=”100″
connect-timeout=”1000″ max-wait-time=”1500″ auto-connect-retry=”true”
socket-keep-alive=”true” socket-timeout=”1500″ slave-ok=”true”
write-number=”1″ write-timeout=”0″ write-fsync=”true” />

<mongo:db-factory dbname="***" username="***"
password=”***” mongo-ref=”mongo” />

<mongo:mapping-converter
db-factory-ref=”mongoDbFactory” />

<bean
abstract=”true”>

Java pseudocode

List files = this.getGridFsTemplate().find(null); //Query all, the query method is the same as MongoTemplate

System.out.println(“—– ————“);

for (GridFSDBFile file: files) {
System.out.println(file);
}

Detailed introduction of MongoDB: Please click here
MongoDB download address: Please click here

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/mongodb-combines-with-spring-to-store-files-pictures-audio-etc/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索