Mongodb--java operation MongoDB Implementation file upload download

Source: Internet
Author: User
Tags findone mongodb

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.