FASTDFS configuration, deployment, and API usage interpretation (2) client-side code for uploading files in byte mode

Source: Internet
Author: User

This article from the poem Merchants • Liu Yong poechant csdn Blog, reproduced please specify the source address: Fastdfs configuration, deployment and API use Interpretation (2) upload files to the Fastdfs Distributed File system client code


Before reading this article, you should first use the Fastdfs configuration, deployment, and API usage interpretation (1) Get Started with Fastdfs article for the reference blog post of the deployment and test to complete the preparatory work.


1, download the Fastdfs API
FASTDFS provides client APIs for languages such as Java and PHP. You can go to Fastdfs on Google Code's Project home page http://code.google.com/p/fastdfs/downloads/list download. This article takes the Java API as an example.


2. Invoking API's upload interface
Get the InputStream, file name, and file length through the servlet, and then upload the file to the FASTDFS server by calling the Java API provided by Fastdfs. The Getfilebuffer in the next section of the code can refer to the previous blog post. (by Poechant)

1     /** 2 * Upload File to DFS.3 * @param filebuff, file to be uploaded.4 * @param uploadfilename, the name of the file.5 * @param filelength, the length of the file.6 * @return The file ID in DFS.7 * @throws IOException8      */  9      PublicString UploadFile (InputStream instream, String uploadfilename,Longfilelength) throws IOException {Ten         byte[] Filebuff =Getfilebuffer (instream, filelength);  OneString fileId ="";  AString Fileextname ="";  -         if(Uploadfilename.contains (".")) {   -Fileextname = uploadfilename.substring (Uploadfilename.lastindexof (".") +1);  the}Else {   -Logger.warn ("Fail to upload file, because the format of filename is illegal.");  -             returnfileId;  -         }   +        -        +         //Establish a connection ATrackerclient Tracker =Newtrackerclient ();  atTrackerserver Trackerserver =tracker.getconnection ();  -Storageserver Storageserver =NULL;  -StorageClient1 client =NewStorageClient1 (Trackerserver, storageserver);  -        -        -         //Set META information innamevaluepair[] Metalist =Newnamevaluepair[3];  -metalist[0] =NewNamevaluepair ("FileName", Uploadfilename);  tometalist[1] =NewNamevaluepair ("Fileextname", Fileextname);  +metalist[2] =NewNamevaluepair ("Filelength", String.valueof (filelength));  -        the        *         //Uploading Files $         Try {  Panax NotoginsengFileId =Client.upload_file1 (Filebuff, Fileextname, Metalist);  -}Catch(Exception e) { theLogger.warn ("Upload file \ ""+ Uploadfilename +"\ "Fails");  +         }   A Trackerserver.close ();  the         returnfileId;  +}

3, the method of invocation detailed
(1) Client communicates with tracker server
According to the "Fastdfs configuration, Deployment and API usage interpretation (1) Get Started with Fastdfs" article provides the principle of Fastdfs, combined with the above code, We can see from the source of the API the tracker servers IP and port initialized by the Trackerclient constructor from the global configuration to initialize a Trackerclient object tracker and establish a connection with it:

1     /** 2 * Constructor with global tracker group3     */  4      Publictrackerclient ()5     {  6          This. Tracker_group =Clientglobal.g_tracker_group; 7     }     8     /** 9 * constructor with specified tracker groupTen * @param tracker_group the Tracker group object One     */   A      Publictrackerclient (trackergroup tracker_group) -     {   -          This. Tracker_group =Tracker_group;  the}

In the code above, Clientglobal is a class that provides many static members for external reading. A Trackerserver object was instantiated by tracker the connection to the tracker server established by this trackerclient.


(2) client communicates with Storage server

Use Trackerserver to obtain the address of an available storage server and instantiate a StorageClient1 object with it. This completes the Fastdfs client call upload, download, delete, and so on all operations of the pre-established connection work.


(3) Invoking the file Operations API
These operations include upload, download, append, delete, and so on. The example above provides an instance of an upload.



This article from the poem Merchants • Liu Yong poechant csdn Blog, reprint please specify the source address: Fastdfs configuration, Deployment and API usage interpretation (2) Java API: Upload files According to InputStream, file name, file length

FASTDFS configuration, deployment, and API usage interpretation (2) client-side code to upload a file in bytes (go)

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.