This article is from the poetry merchant Liu Jing Hong poechant csdn blog. For reprinted, please indicate the source address: fastdfs configuration, deployment, and API usage explanation (2) client code for uploading files to the fastdfs Distributed File System
Before reading this article, please read the fastdfs configuration, deployment, and API usage explanation (1) refer to the deployment and testing sections in the blog post in get started with fastdfs to complete the preparations.
1. Download The fastdfs API
Fastdfs provides client APIs in Java, PHP, and other languages. You can download fastdfs from the Google Code project home page http://code.google.com/p/fastdfs/downloads/list. This article takes Java API as an example.
2. Call the upload API
Use servlet to get inputstream, file name, and file length, and then call the Java API provided by fastdfs to upload the file to the fastdfs server. For details about getfilebuffer in the code below, refer to the previous blog post in this blog. (By poechant)
/*** Upload File to DFS. * @ Param filebuff, file to be uploaded. * @ Param uploadfilename, the name of the file. * @ Param filelength, the length of the file. * @ return the file ID in DFS. * @ throws ioexception */Public String uploadfile (inputstream instream, string uploadfilename, long filelength) throws ioexception {byte [] filebuff = getfilebuffer (instream, filelength); string fileid = ""; string fileextname = ""; if (uploadfilename. contains (". ") {fileextname = uploadfilename. substring (uploadfilename. lastindexof (". ") + 1);} else {logger. warn ("fail to upload file, because the format of filename is illegal. "); Return fileid;} // establish a connection trackerclient tracker = new trackerclient (); trackerserver = tracker. getconnection (); storageserver = NULL; storageclient1 client = new storageclient1 (trackerserver, storageserver); // set the metadata namevaluepair [] metalist = new feature [3]; metalist [0] = new namevaluepair ("FILENAME", uploadfilename); metalist [1] = new namevaluepair ("fileextname", fileextname ); metalist [2] = new namevaluepair ("filelength", String. valueof (filelength); // upload the file try {fileid = client. upload_file1 (filebuff, fileextname, metalist);} catch (exception e) {logger. warn ("Upload file \" "+ uploadfilename +" \ "fails");} trackerserver. close (); Return fileid ;}
3. Call Methods
(1) communication between the client and the tracker server
According to the working principle of fastdfs provided in "fastdfs configuration, deployment, and API usage (1) get started with fastdfs", combined with the above Code, first, use the trackerclient constructor to obtain the IP address and port of the tracker servers from the global configuration to initialize a trackerclient object tracker and establish a connection with it. We can see from the source code of the API:
/*** constructor with global tracker group*/public TrackerClient(){this.tracker_group = ClientGlobal.g_tracker_group;}/*** constructor with specified tracker group* @param tracker_group the tracker group object*/public TrackerClient(TrackerGroup tracker_group){this.tracker_group = tracker_group;}
In the preceding code, clientglobal is a class that provides many static members for external reading. A trackerserver object is instantiated through the tracker connection established by the trackerclient to the tracker server.
(2) Communication between the client and the storage server
Use trackerserver to obtain the address of an available storage server and instantiate a storageclient1 object. In this way, the connection is established before the client of fastdfs calls upload, download, delete, and other operations.
(3) Call the file operation API
These operations include upload, download, append, and delete. In the preceding example, the upload instance is provided.
This article is from the poetry dealer Liu Jing Hong poechant csdn blog. For more information, see the source address: fastdfs configuration, deployment, and API usage. (2) Java API: upload files based on inputstream, file name, and file length.
-