Fastdfs-client-java Operation fastdfs5.0.4

Source: Internet
Author: User

First, download the client in Https://github.com/happyfish100/fastdfs-client-java, unzip and execute the Ant command, in E:\tools\libs\fastdfs\ Fastdfs-client-java-master\src\build will generate Fastdfs_client.jar

Second, mvn install Fastdfs_client.jar, execute command in cmd

MVN Install:install-file-dgroupid=org.csource-dartifactid=fastdfs-client-java-dversion=5.0.4-dpackaging=jar- Dfile=d:\fastdfs_client.jar

III. Create a new MAVEN project in Eclipse Fastdfs-demo, adding dependencies in Pom.xml as follows

<project xmlns= "http://maven.apache.org/POM/4.0.0"  xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "  xsi:schemalocation=" http://maven.apache.org/POM/4.0.0 http:// Maven.apache.org/xsd/maven-4.0.0.xsd ">  <modelVersion>4.0.0</modelVersion>   <groupId>com.leech</groupId>  <artifactId>fastdfs-demo</artifactId>   <version>0.0.1-SNAPSHOT</version>  <packaging>jar</packaging>   <name>fastdfs-demo</name>  <url>http://maven.apache.org</url>   <properties>    <project.build.sourceencoding>utf-8</ project.build.sourceencoding>  </properties>  <dependencies>     <dependency>      <groupId>junit</groupId>       <artifactid>junit</artifactid>      <version>4.11</version>       <scope>test</scope>    </dependency>         <dependency>      <groupid>org.csource</groupid >      <artifactId>fastdfs-client-java</artifactId>       <version>5.0.4</version>    </dependency>         <dependency><groupId>commons-io</groupId>< artifactid>commons-io</artifactid><version>2.4</version></dependency>       </dependencies></project>

Create a new fdfs_client.conf file in src/main/resources with the following content

Connect_timeout = 2

Network_timeout = 30

CharSet = UTF-8

Http.tracker_http_port = 80

Http.anti_steal_token = No

Http.secret_key = FastDFS1234567890

Tracker_server = 192.168.17.112:22122

#tracker_server = 192.168.0.119:22122

Create the Testfastdfs.java in Src/main/java as follows

package com.leech.fastdfs.demo;import java.io.filenotfoundexception;import  java.io.fileoutputstream;import java.io.ioexception;import java.util.uuid;import  org.apache.commons.io.ioutils;import org.csource.common.myexception;import  org.csource.common.namevaluepair;import org.csource.fastdfs.clientglobal;import  org.csource.fastdfs.fileinfo;import org.csource.fastdfs.storageclient;import  org.csource.fastdfs.storageserver;import org.csource.fastdfs.trackerclient;import  org.csource.fastdfs.trackerserver;import org.junit.after;import org.junit.before;import  org.junit.test;public class testfastdfs {public string conf_filename =  "D:\ \stsworkspace\\fastdfs-demo\\src\\main\\resources\\fdfs_client.conf ";      //public  String local_filename =  "D:\\stsworkspace\\fastdfs-demo\\src\\main\\resources\\fdfs_ Client.conf ";     public string local_filename =  "d:\\stsworkspace\\fastdfs-demo\\src\\main\\resources\\ Sprites.png "; @Beforepublic  void setup ()  throws exception {} @Afterpublic  void  teardown ()  throws exception {} @Testpublic  void testupload ()  {         try {              clientglobal.init (Conf_filename);             trackerclient tracker = new trackerclient ();              TrackerServer trackerServer =  Tracker.getconnection ();              storageserver storageserver = null;             storageclient storageclient  = new storageclient (Trackerserver, storageserver); //           namevaluepair nvp = new namevaluepair ("Age",  "; ")             namevaluepair nvp [] =  new NameValuePair[]{                      new namevaluepair ("Age",  ",   ")                    New namevaluepair ("Sex",  "male")               };             String  Fileids[] = storageclient.upload_file (local_filename,  "PNG", &NBSP;NVP);                          system.out.println ( Fileids.length);              SYSTEM.OUT.PRINTLN ("group name:"  + fileids[0]);              system.out.println ("Path: "  + fileids[1]);         } catch  (filenotfoundexception e)  {              e.printstacktrace ();          } catch  (ioexception e)  {              e.printstacktrace ();         } catch  (myexception e)  {              E.printstacktrace ();    &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;}&NBSP;&NBSP;&NBSP;&NBSP;&NBSP,} @Test      public  void testdownload ()  {        try {             clientglobal.init (Conf_filename);             TrackerClient tracker = new  Trackerclient ();              trackerserver  trackerserver = tracker.getconnection ();              StorageServer storageServer = null;             storageclient storageclient = new storageclient ( Trackerserver, storageserver);              byte[] b = stOrageclient.download_file ("group1",  "m00/00/00/wkgrcfv_08oak_kcaaaa5fm_sy874.conf");              system.out.println (b);              ioutils.write (B, new fileoutputstream ("D:/" + Uuid.randomuuid (). toString () + ". conf");        } catch  ( Exception e)  {              E.printstacktrace ();          }     }      @Test      public void testgetfileinfo () {          try {              clientglobal.init (Conf_filename);             TrackerClient Tracker = new trackerclient ();              trackerserver trackerserver = tracker.getconnection ();              StorageServer storageServer = null;             StorageClient storageClient  = new storageclient (Trackerserver, storageserver);              fileinfo fi = storageclient.get_file_info ("group1",  " M00/00/00/wkgrcfv_08oak_kcaaaa5fm_sy874.conf ");              system.out.println (Fi.getsourceipaddr ());              system.out.println (Fi.getfilesize ());             &nbsp System.out.println (Fi.getcreatetimestamp ());              system.out.println (FI.GETCRC32 ());         }  catch  (exception e)  {              e.printstacktrace ();         }      }          @Test      public  Void testgetfilemate () {         try {              clientglobal.init (Conf_filename);             TrackerClient tracker = new  Trackerclient ();              trackerserver  trackerserver =&Nbsp;tracker.getconnection ();              storageserver storageserver = null;             storageclient storageclient = new storageclient (trackerServer,                       storageserver);              Namevaluepair nvps [] = storageclient.get_metadata ("group1",  "M00/00/00/wKgRcFV_08OAK _kcaaaa5fm_sy874.conf ");              for ( NAMEVALUEPAIR&NBSP;NVP&NBSP;:&NBSP;NVPS) {                  system.out.println (Nvp.getname ()  +  ":"  + nvp.getvalue ( ));              }         }  catch  (exception e)  {              e.printstacktrace ();         }      }          @Test      public  Void testdelete () {         try {              clientglobal.init (Conf_filename);             TrackerClient tracker = new  Trackerclient ();              trackerserver  trackerserver = tracker.getconnection ();              storageserver storageserver = null;             Storageclient storageclient = new storageclient (trackerserver,                       Storageserver);              int i =  storageclient.delete_file ("group1",  "m00/00/00/wkgrcfv_08oak_kcaaaa5fm_sy874.conf");              system.out.println ( i==0 ?  " Delete succeeded " : " Delete failed: "+i";         } catch  ( Exception e)  {              E.printstacktrace ();          }     }}

Execute the Testupload method as follows

In the browser input http://192.168.17.112/group1/M00/00/00/wKgRcFV_7W6Aa56BAAInmQ3ZUtg584.png as follows

Fastdfs-client-java Operation fastdfs5.0.4

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.