資源簡介
由于MongoDB的版本3.3之后,原來的GridFS的getDB()已經被廢除了,所以自己花費了梁哥小時,寫了GridFSBucket的方法,試下按照文件名和ObjectID的下載方法!希望對你們有用!

代碼片段和文件信息
package?MongoTest;
import?com.mongodb.MongoClient;
import?com.mongodb.MongoCredential;
import?com.mongodb.ServerAddress;
import?com.mongodb.client.MongoCollection;
import?com.mongodb.client.MongoDatabase;
import?com.mongodb.client.gridfs.GridFSBucket;
import?com.mongodb.client.gridfs.GridFSBuckets;
import?com.mongodb.client.gridfs.model.GridFSUploadOptions;
import?org.bson.Document;
import?org.bson.types.objectId;
import?java.io.*;
import?java.util.ArrayList;
import?java.util.List;
/**
?*?Created?by?liu?on?2017-9-8.
?*/
public?class?MogoUp?{
????public?static?void?main(String[]?args)?throws?IOException?{
????????//連接MongoDB
????????//連接數據庫
????????List?list?=?new?ArrayList();
????????//?連接到MongoDB服務?如果是遠程連接可以替換“localhost”為服務器所在IP地址
????????//?ServerAddress()兩個參數分別為?服務器地址?和?端口
????????ServerAddress?serverAddress?=?new?ServerAddress(“169.254.123.90“?27017);
????????List?addrs?=?new?ArrayList();
????????addrs.add(serverAddress);
????????MongoCredential?credential?=?MongoCredential.createScramSha1Credential(“sa“?“admin“?“xush“.toCharArray());
????????List?credentials?=?new?ArrayList();
????????credentials.add(credential);
????????MongoClient?client?=?new?MongoClient(addrs?credentials);
????????//鏈接測試
????????MongoDatabase?db?=?client.getDatabase(“lhc“);
????????MongoCollection?coll?=?db.getCollection(“test“);
????????//上傳文件
????????InputStream?in?=?null;
????????String?FileName?=?“C:\\Users\\liu\\Desktop\\CREC2010.svg“;
????????File?file?=?new?File(FileName);
????????String?fileName?=?file.getName();
????????String?fileType?=?fileName.substring(fileName.lastIndexOf(“.“)?fileName.length());
????????in?=?new?FileInputStream(file);
????????GridFSBucket?bucket?=?GridFSBuckets.create(db);
????????GridFSUploadOptions?options?=?new?GridFSUploadOptions();
????????//設置去除fileName之外的其他信息
????????Document?metadata?=?new?Document();
????????metadata.append(“contentType“?fileType);
????????options.metadata(metadata);
????????objectId?objectId?=?bucket.uploadFromStream(fileName?in?options);
????????System.out.println(objectId);
????????System.out.println(“上傳成功“);
????????//文件下載
????????OutputStream?os?=?null;
????????String?files?=?“C:\\Users\\liu\\Desktop\\CREC209.svg“;
????????File?file2?=?new?File(files);
????????if?(!file2.exists())?{
????????????file2.createNewFile();
????????}
????????os?=?new?FileOutputStream(file2);
????????GridFSBucket?bucket3?=?GridFSBuckets.create(db);
//????????bucket3.downloadToStream(new?objectId(objectId.toString())os);??//根據objectID
????????bucket3.downloadToStream(fileName?os);????//根據文件名
????????System.out.println(“下載成功“);
????}
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2017-09-08?17:34??mongoDB上傳大文件和下載文件\
?????文件????????2947??2017-09-08?17:34??mongoDB上傳大文件和下載文件\MogoUp.java
?????文件?????1814408??2017-09-08?15:25??mongoDB上傳大文件和下載文件\mongo-java-driver-3.5.0.jar
評論
共有 條評論