mongo数据库对图片的存储

2024-10-13 17:59:23

1、先建立一个数据库连接public MongoDB(String dbName) throws UnknownHostException, MongoException { connection = new Mongo("192.168.19.208:27017"); db = connection.getDB(dbName);}

2、向数据库中存入图片public vo坡纠课柩id inputPage() try { String newFileNam髫潋啜缅e =“phone”; File imageFile =new File("c:\\JavaWebHosting.png") GridFS gfsPhoto =new GridFS(db); GridFSInputFile gfsFile = gfsPhoto.createFile(imageFile) gfsFile.setFilename(newFileName) gfsFile.save(); } catch (IOException e) { e.printStackTrace(); }}

3、存入文件的名叫做phone.

猜你喜欢