On the blog about MongoDB processing large file ideas and processes, let's look at how the Java driver to implement file operations. The Java driver provided by MongoDB encapsulates specific implementation details, and we are very simple to operate.
Add, delete, and read files by using a few components:
Gridfs: A core class that provides creation, discovery, and deletion of files.
Gridfsdbfile: A file read from a database that provides the output of the file, the deletion operation.
Gridfsfile: To save the file, provide the file to save, verify, get the basic information of the file and other operations.
Two examples of dbcollection:
_filescollection: A collection of files, basic information about the file.
_chunkcollection: Block collection, file data
[Java] View plain copy <span style= "font-size:18px" > public static Void main (String[]args) throws Exception{ mongo mongo = new mongo (); db db = mongo.getdb ("Mongodbfile"); //Create a database connection &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;GRIDFS&NBSP;MYFS&NBSP;=&NBSP;NEW&NBSP;GRIDFS (db, "User1"); //Create GridFS operate the User1 in the Mongodbfile database so that the read and delete files are user1 files //save files gridfsfile file = myfs.createfile (New File ("D:/ Image1.jpg "); file.save (); //output File /*gridfsdbfile file =myfs.findone ("image1.jpg"); file.writeto (New file ("d:/image1.jpg"));*/ // Delete file /*gridfsdbfile file =myfs.findone (" Image1.jpg "); myfs.remove ((ObjectId) file2.getId ()); */ }</span >
MongoDB Java driver provided to our use is not complex, and the number of classes involved is very small, we in the project according to the specific needs to find the appropriate way to do.
--------------------------------------------------------------------------------------------------------------- ---
The following is a file with Java to upload, preview, download, bulk packaged download function, often used in the project, first look at the operation effect:
First page
Upload
Show List of all files:
Single File Download:
Bulk Package Download
Files in a compressed package:
SOURCE Download: http://download.csdn.net/detail/liusong0605/6276141
http://blog.csdn.net/liusong0605/article/details/11125277