MongoDB 存放图片
生活随笔
收集整理的這篇文章主要介紹了
MongoDB 存放图片
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
/**
? ? ?* 存儲文件?
? ? ?* @param collectionName 集合名?
? ? ?* @param file 文件?
? ? ?* @param fileid 文件id?
? ? ?* @param companyid 文件的公司id?
? ? ?* @param filename 文件名稱
? ? ?*/
? ? public void SaveFile(String collectionName, File file, String fileid, String companyid, String filename) {
? ? ? ? try {
? ? ? ? ? ? DB db = mongoTemplate.getDb();
? ? ? ? ? ? // 存儲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("存儲文件時發生錯誤!!!");
? ? ? ? }
? ? }
?
? ? // 取出文件
? ? public GridFSDBFile retrieveFileOne(String collectionName, String filename) {
? ? ? ? try {
? ? ? ? ? ? DB db = mongoTemplate.getDb();
? ? ? ? ? ? // 獲取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;
? ? }
//抱歉項目案例不能給你,不過這個是向mongodb存取文件的實現代碼,希望能幫助到你。
//由GridFSDBFile 可以得到inputStream,這樣你就明白了吧。
? ? ?* 存儲文件?
? ? ?* @param collectionName 集合名?
? ? ?* @param file 文件?
? ? ?* @param fileid 文件id?
? ? ?* @param companyid 文件的公司id?
? ? ?* @param filename 文件名稱
? ? ?*/
? ? public void SaveFile(String collectionName, File file, String fileid, String companyid, String filename) {
? ? ? ? try {
? ? ? ? ? ? DB db = mongoTemplate.getDb();
? ? ? ? ? ? // 存儲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("存儲文件時發生錯誤!!!");
? ? ? ? }
? ? }
?
? ? // 取出文件
? ? public GridFSDBFile retrieveFileOne(String collectionName, String filename) {
? ? ? ? try {
? ? ? ? ? ? DB db = mongoTemplate.getDb();
? ? ? ? ? ? // 獲取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;
? ? }
//抱歉項目案例不能給你,不過這個是向mongodb存取文件的實現代碼,希望能幫助到你。
//由GridFSDBFile 可以得到inputStream,這樣你就明白了吧。
總結
以上是生活随笔為你收集整理的MongoDB 存放图片的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 拟合算法模型
- 下一篇: java实现一个整数分解为两个质数乘积