Original link: http://blog.csdn.net/xyang81/article/details/52847311
Java Client source code and JAR: Link: http://pan.baidu.com/s/1jHIwtsq Password: n757
I have encapsulated the official SDK with a tool class that will be used to import the tool class into the project, as follows:
PackageCom.digi_zones.fdfs;Importorg.apache.commons.io.FileUtils;ImportOrg.apache.commons.io.IOUtil;ImportOrg.csource.common.NameValuePair;Importorg.csource.fastdfs.*;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.util.HashMap;ImportJava.util.Iterator;ImportJava.util.Map;/*** <p>description:fastdfs File upload download tool class </p> * <p>copyright:copyright (c) 2016</p> * *@authorYangxin *@version1.0 * @date 2016/10/19*/ Public classfastdfsclient {Private Static FinalString config_filename = "src/main/resources/fdfs/fdfs_client.conf"; Private StaticStorageClient1 storageClient1 =NULL; //initializing the Fastdfs Client Static { Try{clientglobal.init (config_filename); Trackerclient trackerclient=Newtrackerclient (Clientglobal.g_tracker_group); Trackerserver Trackerserver=trackerclient.getconnection (); if(Trackerserver = =NULL) { Throw NewIllegalStateException ("Getconnection return null"); } storageserver storageserver=trackerclient.getstorestorage (Trackerserver); if(Storageserver = =NULL) { Throw NewIllegalStateException ("Getstorestorage return null"); } storageClient1=NewStorageClient1 (Trackerserver,storageserver); } Catch(Exception e) {e.printstacktrace (); } } /*** Upload Files *@paramFile Object *@paramFileName *@return */ Public Staticstring uploadfile (file file, string fileName) {returnUploadFile (File,filename,NULL); } /*** Upload Files *@paramFile Object *@paramFileName *@parammetalist file meta data *@return */ Public StaticString uploadfile (file file, string fileName, map<string,string>Metalist) { Try { byte[] buff = Ioutil.tobytearray (Newfileinputstream (file)); Namevaluepair[] Namevaluepairs=NULL; if(Metalist! =NULL) {Namevaluepairs=Newnamevaluepair[metalist.size ()]; intindex = 0; for(Iterator<map.entry<string,string>> Iterator =Metalist.entryset (). iterator (); Iterator.hasnext ();) {Map.entry<String,String> entry =Iterator.next (); String name=Entry.getkey (); String value=Entry.getvalue (); Namevaluepairs[index++] =NewNamevaluepair (Name,value); } } returnStorageclient1.upload_file1 (Buff,fileutils.getextension (fileName), namevaluepairs); } Catch(Exception e) {e.printstacktrace (); } return NULL; } /*** Get file meta data *@paramfileId File ID *@return */ Public StaticMap<string,string>Getfilemetadata (String fileId) {Try{namevaluepair[] metalist=storageclient1.get_metadata1 (fileId); if(Metalist! =NULL) {HashMap<String,String> map =NewHashmap<string, string>(); for(Namevaluepair metaitem:metalist) {map.put (Metaitem.getname (), Metaitem.getvalue ()); } returnmap; } } Catch(Exception e) {e.printstacktrace (); } return NULL; } /*** Delete Files *@paramfileId File ID *@returnDelete Failed returns-1, otherwise returns 0*/ Public Static intDeleteFile (String fileId) {Try { returnStorageclient1.delete_file1 (fileId); } Catch(Exception e) {e.printstacktrace (); } return-1; } /*** Download File *@paramfileId file ID (ID returned when the file was uploaded successfully) *@paramoutFile File Download save location *@return */ Public Static intDownloadFile (String fileId, File outFile) {FileOutputStream fos=NULL; Try { byte[] content =Storageclient1.download_file1 (fileId); FOS=NewFileOutputStream (OutFile); Ioutil.copy (Content,fos); return0; } Catch(Exception e) {e.printstacktrace (); } finally { if(Fos! =NULL) { Try{fos.close (); } Catch(IOException e) {e.printstacktrace (); } } } return-1; }}
Java Client configuration file (fdfs_client.conf):
Connect_timeout = Ten # connection Tracker Server timeout Length network_timeout = # Socket Connection Timeout Length charset = UTF-8 # File content Encoding Http.tracker_http_port = 8888 # Tracker Server Port Http.anti_steal_token = Nohttp.secret_key = FastDFS1234567890 tracker_server = 192.168.0.200:22122 # Tracker Server IP and port (can write multiple) #tracker_server = xxxx:xxx
Java client file Upload, download, delete, and meta data acquisition testing :
PackageCom.digi_zones.fdfs;Importorg.junit.Test;ImportJava.io.File;ImportJava.util.HashMap;ImportJava.util.Iterator;ImportJava.util.Map;/*** <p>description: </p> * <p>copyright:copyright (c) 2016</p> * *@authorYangxin *@version1.0 * @date 2016/10/19*/ Public classFastdfsclienttest {/*** File Upload test*/@Test Public voidtestupload () {File file=NewFile ("C:\\users\\yangfang\\pictures\\angularjs_share.jpg"); Map<String,String> Metalist =NewHashmap<string, string>(); Metalist.put ("width", "1024"); Metalist.put ("Height", "768"); Metalist.put ("Author", "Yang Xin"); Metalist.put ("Date", "20161018"); String FID=Fastdfsclient.uploadfile (File,file.getname (), metalist); System.out.println ("Upload local file" + File.getpath () + "OK, fileid=" +FID); //upload the file that was successfully returned Id:group1/m00/00/00/wkgayvgfk9aab8hwaa-8q6_7thw351.jpg } /*** File Download Test*/@Test Public voidtestdownload () {intr = Fastdfsclient.downloadfile ("Group1/m00/00/00/wkgayvgfk9aab8hwaa-8q6_7thw351.jpg",NewFile ("Downloadfile_fid.jpg")); System.out.println (R= = 0? "Download succeeded": "Download Failed"); } /*** Get file meta-data test*/@Test Public voidTestgetfilemetadata () {Map<String,String> Metalist = Fastdfsclient.getfilemetadata ("group1/m00/00/00/wkgayvgfk9aab8hwaa-8q6_ 7thw351.jpg "); for(Iterator<map.entry<string,string>> Iterator =Metalist.entryset (). iterator (); Iterator.hasnext ();) {Map.entry<String,String> entry =Iterator.next (); String name=Entry.getkey (); String value=Entry.getvalue (); SYSTEM.OUT.PRINTLN (Name+ " = " +value); } } /*** File Delete test*/@Test Public voidTestdelete () {intr = Fastdfsclient.deletefile ("Group1/m00/00/00/wkgayvgfk9aab8hwaa-8q6_7thw351.jpg"); System.out.println (R= = 0? "Delete succeeded": "Delete Failed"); }}
Fastdfs Distributed File System Java client use