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)