FASTDFS configuration, deployment, and API usage interpretation (3) client-side code for uploading files in a streaming manner

Source: Internet
Author: User

The APIs that are called are:

String[] Upload_file (

String group_name,//group name, not specified, can be set to NULL

Long file_size,//file size, must be developed

Uploadcallback callback,//Callback

String File_ext_name,

Namevaluepair[] Meta_list

)

1     /** 2 * Upload File to DFS, directly transferring Java.io.InputStream to Java.io.OutStream3 * @author poechant4 * @email [email protected]5 * @param filebuff, file to be uploaded.6 * @param uploadfilename, the name of the file.7 * @param filelength, the length of the file.8 * @return The file ID in DFS.9 * @throws IOExceptionTen      */   One      PublicString[] Uploadfilebystream (InputStream instream, String uploadfilename,Longfilelength) throws IOException { A            -String[] results =NULL;  -String Fileextname ="";  the         if(Uploadfilename.contains (".")) {   -Fileextname = uploadfilename.substring (Uploadfilename.lastindexof (".") +1);  -}Else {   -Logger.warn ("Fail to upload file, because the format of filename is illegal.");  +             returnresults;  -         }   +            ATrackerclient Tracker =Newtrackerclient ();  atTrackerserver Trackerserver =tracker.getconnection ();  -Storageserver Storageserver =NULL;  -StorageClient1 client =NewStorageClient1 (Trackerserver, storageserver);  -               -namevaluepair[] Metalist =Newnamevaluepair[3];  -metalist[0] =NewNamevaluepair ("FileName", Uploadfilename);  inmetalist[1] =NewNamevaluepair ("Fileextname", Fileextname);  -metalist[2] =NewNamevaluepair ("Filelength", String.valueof (filelength));  to               +            Try {   -             //Results[0]: GroupName, results[1]: Remotefilename.  theResults = Client.upload_file (NULL, Filelength,NewUploadfilesender (instream), Fileextname, Metalist);  *}Catch(Exception e) { $Logger.warn ("Upload file \ ""+ Uploadfilename +"\ "Fails"); Panax Notoginseng         }   -               the Trackerserver.close ();  +            A         returnresults;  the}

The Uploadfilesender is a class that implements the Uploadcallback interface:

1     Private Static classUploadfilesender implements Uploadcallback {2           3         PrivateInputStream instream; 4           5          PublicUploadfilesender (InputStream instream) {6              This. instream =instream; 7         }  8           9          Public intSend (OutputStream out) throws IOException {Ten             intreadbytes;  One              while((Readbytes = Instream.read ()) >0) {   A                  out. Write (readbytes);  -             }   -             return 0;  the         }   -}

FASTDFS configuration, deployment, and API usage interpretation (3) client code for uploading files in a stream (go)

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.