Pom.xml file Add MongoDB jar Package
<dependency> <groupId>org.mongodb</groupId> <artifactid>mongo-java-driver</ artifactid> <version>3.4.0</version></dependency><dependency> <groupid >org.springframework.data</groupId> <artifactId>spring-data-mongodb</artifactId> <version>1.10.3.RELEASE</version></dependency>
Add Mongodb.properties under Src/main/resources
Mongoip=127.0.0.1mongoport=27017mongodatabase=vulnsubmitconnections_per_host=10max_wait_time=120000connect_ timeout=0mongo_user=sysadminmongo_pass=simple123456
MongoDB Tools Class
Package Com.vulnverify.core.utils;import Java.io.file;import Java.io.fileinputstream;import Java.io.fileoutputstream;import Java.io.ioexception;import Java.io.inputstream;import Java.io.OutputStream;import Java.net.url;import java.util.arraylist;import java.util.list;import Java.util.properties;import Java.util.UUID; Import Javax.validation.constraints.null;import Org.bson.types.objectid;import Org.slf4j.logger;import Org.slf4j.loggerfactory;import Com.mongodb.mongoclient;import Com.mongodb.mongoclientoptions;import Com.mongodb.mongocredential;import Com.mongodb.serveraddress;import Com.mongodb.client.mongodatabase;import Com.mongodb.client.gridfs.gridfsbucket;import Com.mongodb.client.gridfs.gridfsbuckets;import com.mongodb.client.model.filters;/** * MongoDB Tool class * Easy to upload and download files * Total upload, download, delete Files Tool class * * @author Linan * @since 20170517 */public class Generalmongodbutil {Private Final static properties Properties = New properties ();p rivate final static Logg ER logger = Loggerfactory.getloggeR (generalmongodbutil.class); static {try {URL url = GeneralMongoDbUtil.class.getClassLoader (). GetResource (" Mongodb.properties "); if (URL! = null) {logger.info (" Found ' mongodb.properties ' file in local classpath "); InputStream InputStream = Url.openstream (); try {properties.load (inputstream);} catch (Exception e) {e.printstacktrace ();} finally { Inputstream.close ();}}} catch (IOException e) {e.printstacktrace (); Logger.info ("Could not load ' mongo_db.properties ' file from local classpath:" + e);}} Public Generalmongodbutil () {}private static class Config {/** * MongoDB connection Properties */public static String IP = null;public static int port = 27017;public static String database = null;/** * MongoDB Connection Pool Properties */publi c static int connectionsperhost = 10;public static int maxwaittime = 120000;public static int connecttimeout = 0;public St atic Boolean socketkeepalive = true;public static int sockettimeout = 150000;public static Mongoclientoptions Mongocliento ptions = null;/** * User and voucher */public static list<mongocredential> credentiallist = new arraylist<mongocredential> (); static {IP = Properties.getproperty ("Mongoip");p ort = Integer.parseint (Properties.getproperty ("Mongoport"));d atabase = Properties.getproperty ("Mongodatabase"); connectionsperhost = Integer.parseint (Properties.getproperty (" Connections_per_host ")); maxwaittime = Integer.parseint (Properties.getproperty (" Max_wait_time ")); connectTimeout = Integer.parseint (Properties.getproperty ("connect_timeout")); socketkeepalive = True;sockettimeout = 150000; Mongoclientoptions = Mongoclientoptions.builder (). ConnectTimeout (ConnectTimeout). Maxwaittime (MaxWaitTime). Connectionsperhost (Connectionsperhost). Socketkeepalive (socketkeepalive). Sockettimeout (SocketTimeout). build (); * * Certification and Voucher * */mongocredential credential = mongocredential.createcredential (Properties.getproperty ("Mongo_user"), Database,properties.getproperty ("Mongo_pass"). ToCharArray ()); Credentiallist.add (credential);}} private static Final ClasS mongoinstance {public final static mongoclient client;static {//client = new Mongoclient (New ServerAddress (config.ip,// Config.port), config.credentiallist, config.mongoclientoptions); client = new Mongoclient (New ServerAddress (Config.ip , Config.port), config.mongoclientoptions);}} /** * Destroy pool */public static final void Destroy () {MongoInstance.client.close ();} /** * Get a mongodatabase * * @return */public static mongodatabase getdatabase () {return MongoInstance.client.getDatabas E (config.database);} public static Mongodatabase Getgridfsdatabase () {return MongoInstance.client.getDatabase ("Gridfs");} /** * Get a mongodatabase by Name * * @param databaseName * @return */public static mongodatabase getdatabase (String data BaseName) {return MongoInstance.client.getDatabase (databaseName);} Below is the upload content///////////////////////////////** * Upload file to MongoDB * * @param destinationname * @param inputS Tream * @return */public static string Uploadfiletogridfs (String destinationnAme, InputStream InputStream, String bucketname) {/** * default bucket name is FS */gridfsbucket bucket = gridfsbuckets.create (Getdatabase ( ), bucketname); ObjectId fileId = Bucket.uploadfromstream (Destinationname, InputStream); return fileid.tohexstring ();} /** * Upload files to MongoDB, you can choose to close the stream * * @param destinationname * @param inputstream * @param close * @return */public static STR ing Uploadfiletogridfs (string destinationname, InputStream InputStream, String Bucketname, Boolean close) {string fileId = null;try {fileId = Uploadfiletogridfs (Destinationname, InputStream, bucketname);} finally {if (close) {try {InputStream . Close ();} catch (IOException e) {logger.info ("Close InputStream fail:" + E);}}} return fileId;} /** * Upload file to MongoDB, parameter is file * * @param destinationname * @param file * @return */public static String UPLOADFILETOGRIDF S (string destinationname, File file, string bucketname) {InputStream InputStream = null;try {inputstream = new fileinputst Ream (file); String fileId = Uploadfiletogridfs (DestinationnaMe, InputStream, Bucketname, true); return fileId;} catch (IOException e) {logger.info ("Upload fail:" + e);} return null;} /** * Upload file to MongoDB, file name unchanged upload * * @param file * @return */public static string Uploadfiletogridfs (file file, string bucket Name) {return Uploadfiletogridfs (File.getname (), file, bucketname);} /** * Upload file to MongoDB, file name add UUID * * @param file * @return */public static String uploadfiletogridfsbyuuid (file file, Strin G Bucketname) {return Uploadfiletogridfs (Uuid.randomuuid (). toString (), file, bucketname);} Below is the download///////////////////////////////** * download file from MongoDB via filename * * @param sourceName * @param outpu TStream */public static void Downloadfilebyname (String sourceName, OutputStream outputstream, string bucketname) { Gridfsbucket bucket = gridfsbuckets.create (Getdatabase (), bucketname); Bucket.downloadtostream (SourceName, OutputStream);} /** * Download files from MongoDB via Objectid * * @param objectid * @param outputstream */public static void DownloadFile (String objecTId, OutputStream OutputStream, String bucketname) {/** * default bucket name is FS */gridfsbucket bucket = gridfsbuckets.create (Getdatabas E (), bucketname); Bucket.downloadtostream (new ObjectId (ObjectId), outputstream);} /** * Get file name from MongoDB * @param objectId * @param outputstream * @param file name */public static string GetFileName (String objec TId, String bucketname) {Gridfsbucket bucket = gridfsbuckets.create (Getdatabase (), bucketname); String fileName = Bucket.find (Filters.eq ("_id", New ObjectId (ObjectId))). First (). GetFileName (); return fileName;} /** * Download files from MongoDB via Objectid * * @param objectid * @param outputstream */public static String downloadtaskresultfile (S Tring ObjectId, OutputStream outputstream) {/** * default bucket name is FS */gridfsbucket bucket = gridfsbuckets.create (getgridfsdatabase (), "Bucketme"); String fileName = Bucket.find (Filters.eq ("_id", New ObjectId (ObjectId))). First (). GetFileName (); Bucket.downloadtostream (New ObjectId (ObjectId), outputstream); return fileName;} /** * Download files from MongoDB via Objectid * * @Param objectId * @param destinationfile */public static void DownloadFile (String objectId, File destinationfile, String bu Cketname) {OutputStream OutputStream = null;try {outputstream = new FileOutputStream (destinationfile);d Ownloadfile ( ObjectId, OutputStream, bucketname);} catch (IOException e) {logger.info ("Download fail:" + e);} finally {if (outputstream! = null) {try {outputstream.close ()} catch (IOException e) {logger.info ("Close OutputStream FAI L: "+ e);}}}} /** * Download files from MongoDB via Objectid * * @param objectid * @param destinationname */public static void DownloadFile (String obj Ectid, String destinationname, String bucketname) {File Destinationfile = new File (destinationname);d Ownloadfile ( ObjectId, Destinationfile, bucketname);} /** * Delete files from MongoDB via objectId * * @param objectId */public static void Deletebyobjectid (String objectId, String bucketn AME) {Gridfsbucket bucket = gridfsbuckets.create (Getdatabase (), bucketname); Bucket.delete (new ObjectId (ObjectId));}}
MongoDB Tool Class