在线文字转语音网站:无界智能 aiwjzn.com

Java如何使用GridFSBucket存储图片、音频、视频等文件到MongoDB

Java如何使用GridFSBucket存储图片、音频、视频等文件到MongoDB

在Java中使用GridFSBucket来存储文件到MongoDB中,需要先导入MongoDB的Java驱动程序(如mongodb-driver)和GridFSBucket的类(org.mongodb:gridfs)。 下面是一个使用GridFSBucket存储图片文件到MongoDB的完整样例代码: 1. 首先,确保你已经在Maven项目中添加了以下依赖: <dependency> <groupId>org.mongodb</groupId> <artifactId>mongodb-driver-sync</artifactId> <version>4.4.1</version> </dependency> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongodb-driver-core</artifactId> <version>4.4.1</version> </dependency> <dependency> <groupId>org.mongodb</groupId> <artifactId>bson</artifactId> <version>4.4.1</version> </dependency> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongodb-driver</artifactId> <version>4.4.1</version> </dependency> 2. 编写Java代码: import com.mongodb.MongoClientSettings; import com.mongodb.MongoCredential; import com.mongodb.ServerAddress; import com.mongodb.client.MongoClient; import com.mongodb.client.MongoClients; import com.mongodb.client.gridfs.GridFSBucket; import com.mongodb.client.gridfs.GridFSBuckets; import com.mongodb.client.gridfs.GridFSFindIterable; import com.mongodb.client.gridfs.model.GridFSFile; import com.mongodb.client.gridfs.model.GridFSUploadOptions; import org.bson.Document; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; public class MongoGridFSExample { public static void main(String[] args) { // MongoDB连接参数 String host = "localhost"; int port = 27017; String databaseName = "test"; String username = "username"; String password = "password"; // 创建MongoDB客户端 MongoCredential credential = MongoCredential.createCredential(username, databaseName, password.toCharArray()); ServerAddress serverAddress = new ServerAddress(host, port); MongoClientSettings settings = MongoClientSettings.builder() .credential(credential) .applyToSslSettings(builder -> builder.enabled(false)) .applyToClusterSettings(builder -> builder.hosts(Arrays.asList(serverAddress))) .build(); MongoClient mongoClient = MongoClients.create(settings); // 获取GridFSBucket GridFSBucket gridFSBucket = GridFSBuckets.create(mongoClient.getDatabase(databaseName)); // 上传文件 String filePath = "path_to_file/image.jpg"; String fileName = "image.jpg"; try (InputStream streamToUploadFrom = new FileInputStream(filePath)) { GridFSUploadOptions options = new GridFSUploadOptions() .chunkSizeBytes(1024) .metadata(new Document("type", "image")); String fileId = gridFSBucket.uploadFromStream(fileName, streamToUploadFrom, options); System.out.println("File uploaded with id: " + fileId); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // 根据文件名查询文件 GridFSFindIterable files = gridFSBucket.find(new Document("filename", fileName)); GridFSFile file = files.first(); if (file != null) { System.out.println("File name: " + file.getFilename()); System.out.println("File size: " + file.getLength()); System.out.println("File type: " + file.getMetadata().getString("type")); // 可以通过file.getObjectId()获取文件ID,通过gridFSBucket.openDownloadStream(file.getObjectId())下载文件 } else { System.out.println("File not found"); } // 关闭MongoDB客户端 mongoClient.close(); } } 这个样例代码演示了如何使用GridFSBucket将图片文件上传到MongoDB,并根据文件名查询文件并获取一些基本信息。请根据你的具体需求进行修改和扩展。 需要注意的是,上面的代码假设你已经在本地运行MongoDB服务器,并且设置了用户名、密码等认证信息。请根据你的实际情况进行相应的更改。 参考文档: - MongoDB官方Java驱动程序文档:https://mongodb.github.io/mongo-java-driver/ - GridFSBucket类文档和示例:https://mongodb.github.io/mongo-java-driver/4.4/driver/tutorials/gridfs/ - MongoDB官方文档:https://docs.mongodb.com/