FastDFS Java Api Operations

FastDFS Java Api Operations

1. Use eclipse to type the source code into a jar file: (source code download address: https://github.com/happyfish100/fastdfs-client-java )


2. Add fastdfs_client.jar to the Maven repository

#Execute the maven command to add fastdfs_client.jar to the maven repository
mvn install:install-file -DgroupId=org.csource -DartifactId=fastdfs-client-java -Dversion=5.0.4 -Dpackaging=jar -Dfile=/Users/shenwei/Desktop/fastdfs_client.jar

3. View  fastdfs_client.jar. You can see the jar file


4. Create a maven project and add the  fastdfs_client dependency to the pom.

#add 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>

5. Create the fdfs_client.conf configuration file under src/main/resource

#fdfs_client.conf Configuration
connect_timeout = 10
network_timeout = 30
charset = UTF-8
http.tracker_http_port = 8888
http.anti_steal_token = no
http.secret_key = FastDFS1234567890

tracker_server = 100.98.22.253:22122
#If there are multiple services, specify the IP of the cluster
#tracker_server = 192.168.10.250:22122

6. Test fastDFS upload, download, delete, get file information

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.FileInfo;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.junit.Test;

public class TestFastDfs {
    
	//fdfs_client core configuration file
    public String conf_filename = "src/main/resources/fdfs_client.conf";

    @Test
    public void testUpload() { //Upload file
    	TrackerServer trackerServer =null;
    	StorageServer storageServer = null;
    	
        try {
            ClientGlobal.init(conf_filename);
            TrackerClient tracker = new TrackerClient();
            trackerServer = tracker.getConnection();
            StorageClient1 client = new StorageClient1(trackerServer, storageServer);
            
            //path of file to upload
            String local_filename = "/Users/shenwei/Desktop/1.png";
// This parameter can be specified or not specified, if specified, you can get the value here according to the testGetFileMate() method
//            NameValuePair nvp [] = new NameValuePair[]{
//                    new NameValuePair("age", "18"),
//                    new NameValuePair("sex", "male")
//            };
            
            StorageClient storageClient = new StorageClient(trackerServer, storageServer);
//          String fileIds[] = storageClient.upload_file(local_filename, "png", nvp);
            String fileIds[] = storageClient.upload_file(local_filename, "png", null);
            
            System.out.println(fileIds.length);
            System.out.println("组名:" + fileIds[0]);
            System.out.println("路径: " + fileIds[1]);
        } catch (Exception e) {
            e.printStackTrace ();
        } finally{
			try {
				if(null!=storageServer) storageServer.close();
				if(null!=trackerServer) trackerServer.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace ();
			}
        }
    }

    @Test
    public void testDownload() { //Download file
    	TrackerServer trackerServer =null;
    	StorageServer storageServer = null;
    	
        try {
        	String groupName = "group1";
        	String filePath = "M00/00/00/ZGIW_lpujW-ADvpRAAblmT4ACuo125.png";
            ClientGlobal.init(conf_filename);

            TrackerClient tracker = new TrackerClient();
            trackerServer = tracker.getConnection();

            StorageClient storageClient = new StorageClient(trackerServer, storageServer);
            byte[] bytes = storageClient.download_file(groupName, filePath);
            
            String storePath = "/Users/shenwei/Desktop/download.png";
            OutputStream out = new FileOutputStream(storePath);
            out.write(bytes);
        } catch (Exception e) {
            e.printStackTrace ();
        } finally{
			try {
				if(null!=storageServer) storageServer.close();
				if(null!=trackerServer) trackerServer.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace ();
			}
        }
    }
    
    @Test
    public void testGetFileInfo(){ //Get file information
    	TrackerServer trackerServer =null;
    	StorageServer storageServer = null;
    
        try {
        	String groupName = "group1";
        	String filePath = "M00/00/00/ZGIW_lpujW-ADvpRAAblmT4ACuo125.png";
            ClientGlobal.init(conf_filename);

            TrackerClient tracker = new TrackerClient();
            trackerServer = tracker.getConnection();

            StorageClient storageClient = new StorageClient(trackerServer, storageServer);
            FileInfo file = storageClient.get_file_info(groupName, filePath);
            System.out.println("ip--->"+file.getSourceIpAddr());
            System.out.println("File size--->"+file.getFileSize());
            System.out.println("File upload time--->"+file.getCreateTimestamp());
            System.out.println(file.getCrc32());
        } catch (Exception e) {
            e.printStackTrace ();
        } finally{
			try {
				if(null!=storageServer) storageServer.close();
				if(null!=trackerServer) trackerServer.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace ();
			}
       }
    }
    
    @Test
    public void testGetFileMate(){ //Get the original data type of the file
    	TrackerServer trackerServer =null;
    	StorageServer storageServer = null;
    	
    	try {
    		String groupName = "group1";
        	String filePath = "M00/00/00/ZGIW_lpujW-ADvpRAAblmT4ACuo125.png";
    		ClientGlobal.init(conf_filename);

            TrackerClient tracker = new TrackerClient();
            trackerServer = tracker.getConnection();

            StorageClient storageClient = new StorageClient(trackerServer,
                    storageServer);
            
            //This value is the NameValuePair specified when uploading
            NameValuePair nvps [] = storageClient.get_metadata(groupName, filePath);
            if(null!=nvps && nvps.length>0){
            	for (NameValuePair nvp: nvps) {
            		System.out.println(nvp.getName() + ":" + nvp.getValue());
            	}
            }
        } catch (Exception e) {
            e.printStackTrace ();
        } finally{
			try {
				if(null!=storageServer) storageServer.close();
				if(null!=trackerServer) trackerServer.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace ();
			}
       }
    }
    
    @Test
    public void testDelete(){ //delete the file
    	TrackerServer trackerServer =null;
    	StorageServer storageServer = null;
    	
    	try {
    		String groupName = "group1";
        	String filePath = "M00/00/00/ZGIW_lpujW-ADvpRAAblmT4ACuo125.png";
    		ClientGlobal.init(conf_filename);

            TrackerClient tracker = new TrackerClient();
            trackerServer = tracker.getConnection();

            StorageClient storageClient = new StorageClient(trackerServer,
                    storageServer);
            int i = storageClient.delete_file(groupName, filePath);
            System.out.println( i==0 ? "Deletion succeeded" : "Deletion failed:"+i);
        } catch (Exception e) {
            e.printStackTrace ();
        } finally{
			try {
				if(null!=storageServer) storageServer.close();
				if(null!=trackerServer) trackerServer.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace ();
			}
       }
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325922474&siteId=291194637