Fastdfs Tool class implements file upload _02

Source: Internet
Author: User

One, jar package 

Jar Package Download: https://pan.baidu.com/s/1nwkAHU5 Password: tlv6

Or

Download project, install to Maven local repository

Project Download: https://pan.baidu.com/s/1i6SIm3b Password: JKJT

Second, the tool class code

 PackageCom.common.fastFDS;ImportOrg.csource.common.NameValuePair;ImportOrg.csource.fastdfs.ClientGlobal;Importorg.csource.fastdfs.StorageClient1;ImportOrg.csource.fastdfs.StorageServer;Importorg.csource.fastdfs.TrackerClient;ImportOrg.csource.fastdfs.TrackerServer; Public classfastdfsclient {PrivateTrackerclient trackerclient =NULL; PrivateTrackerserver Trackerserver =NULL; PrivateStorageserver Storageserver =NULL; PrivateStorageClient1 storageclient =NULL;  PublicFastdfsclient (String conf)throwsException {if(Conf.contains ("Classpath:") ) {conf= Conf.replace ("Classpath:", This. GetClass (). GetResource ("/"). GetPath ());        } clientglobal.init (conf); Trackerclient=Newtrackerclient (); Trackerserver=trackerclient.getconnection (); Storageserver=NULL; Storageclient=NewStorageClient1 (Trackerserver, storageserver); }        /*** Upload File Method * <p>Title:uploadFile</p> * <p>description: </p> *@paramFileName File Full path *@paramextname file extension, not including (.) *@parammetas File extension information *@return     * @throwsException*/     PublicString UploadFile (String fileName, String extname, namevaluepair[] metas)throwsException {String result=Storageclient.upload_file1 (FileName, Extname, Metas); returnresult; }         PublicString UploadFile (String fileName)throwsException {returnUploadFile (FileName,NULL,NULL); }         PublicString UploadFile (String fileName, String extname)throwsException {returnUploadFile (FileName, Extname,NULL); }        /*** Upload File Method * <p>Title:uploadFile</p> * <p>description: </p> *@paramcontents of the Filecontent file, byte array *@paramextname File extension *@parammetas File extension information *@return     * @throwsException*/     PublicString UploadFile (byte[] filecontent, String extname, namevaluepair[] metas)throwsException {String result=Storageclient.upload_file1 (filecontent, Extname, Metas); returnresult; }         PublicString UploadFile (byte[] filecontent)throwsException {returnUploadFile (Filecontent,NULL,NULL); }         PublicString UploadFile (byte[] filecontent, String extname)throwsException {returnUploadFile (Filecontent, Extname,NULL); }}

Third, the test code

  @Test  public  void  testfastfdsclient () throws   exception{ // 1. Create a profile client.conf (this file name is arbitrary)  //  Configuration file Contents: tracker_server=192.168.**.**:22122 (your own FASTDFS server ip+ port number)  // 
   
    2. Creating an Fastdfsclient instance  fastdfsclient fastdfsclient = 
    new<        /span> fastdfsclient ("e:/workspaces/conf/client.conf"  // 3. Perform a picture upload and return the picture path  string string =    Fastdfsclient.uploadfile ("c:/waiguoren.jpg"  
   

Four

Can draw on: http://www.cnblogs.com/fangwu/p/8367293.html

Fastdfs Tool class implements file upload _02

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.