码迷,mamicode.com
首页 > 编程语言 > 详细

fastdfs_java测试代码

时间:2016-07-23 19:33:22      阅读:775      评论:0      收藏:0      [点我收藏+]

标签:

上传:

技术分享
/**
 * <p><b>Title:</b> TestUpload.java </p>
 * @Package fastdfs
 */
package fastdfs;

import java.io.File;
import java.io.FileInputStream;

import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.ServerInfo;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;

/**
 * <p>
 * <b>TestUpload Description:</b> (测试上传)
 * </p>
 * 
 * @author Xu Debiao <b>DATE:</b> 2016年7月23日 下午5:19:00
 */
public class TestUpload {
    public static void main(String[] args) throws Exception {
        String classPath = new File(TestUpload.class.getResource("/").getFile()).getCanonicalPath();
        String configFilePath = classPath + File.separator + "fdfs_client.conf";
        System.out.println("配置文件:" + configFilePath);

        ClientGlobal.init(configFilePath);

        TrackerClient trackerClient = new TrackerClient();
        TrackerServer trackerServer = trackerClient.getConnection();

        StorageServer storageServer = null;
        StorageClient storageClient = new StorageClient(trackerServer, storageServer);

        NameValuePair[] meta_list = new NameValuePair[3];
        meta_list[0] = new NameValuePair("width", "120");
        meta_list[1] = new NameValuePair("heigth", "120");
        meta_list[2] = new NameValuePair("author", "gary");

        // byte[] file_buff = "F:\\pic.jpg".getBytes(ClientGlobal.g_charset);
        File file = new File("D:\\x.jpg");
        FileInputStream fis = new FileInputStream(file);
        byte[] file_buff = null;
        if (fis != null) {
            int len = fis.available();
            file_buff = new byte[len];
            fis.read(file_buff);
        }
        System.out.println("file length: " + file_buff.length);

        String group_name = null;
        StorageServer[] storageServers = trackerClient.getStoreStorages(trackerServer, group_name);
        if (storageServers == null) {
            System.err.println("get store storage servers fail, error code: " + storageClient.getErrorCode());
        } else {
            System.err.println("store storage servers count: " + storageServers.length);
            for (int k = 0; k < storageServers.length; k++) {
                System.err.println(k + 1 + ". " + storageServers[k].getInetSocketAddress().getAddress().getHostAddress()
                        + ":" + storageServers[k].getInetSocketAddress().getPort());
            }
            System.err.println("");
        }

        long startTime = System.currentTimeMillis();
        String[] results = storageClient.upload_file(file_buff, "jpg", meta_list);
        System.out.println("upload_file time used: " + (System.currentTimeMillis() - startTime) + " ms");

        if (results == null) {
            System.err.println("upload file fail, error code: " + storageClient.getErrorCode());
            return;
        }

        group_name = results[0];
        String remote_filename = results[1];
        System.err.println("group_name: " + group_name + ", remote_filename: " + remote_filename);
        System.err.println(storageClient.get_file_info(group_name, remote_filename));

        ServerInfo[] servers = trackerClient.getFetchStorages(trackerServer, group_name, remote_filename);
        if (servers == null) {
            System.err.println("get storage servers fail, error code: " + trackerClient.getErrorCode());
        } else {
            System.err.println("storage servers count: " + servers.length);
            for (int k = 0; k < servers.length; k++) {
                System.err.println(k + 1 + ". " + servers[k].getIpAddr() + ":" + servers[k].getPort());
            }
            System.err.println("");
        }
    }
}
View Code

控制台:

技术分享
配置文件:D:\__work\xudebiao\fastdfs\bin\fdfs_client.conf
file length: 274328
store storage servers count: 1
1. 121.196.215.6:23000

upload_file time used: 2036 ms
group_name: group1, remote_filename: M00/00/00/rBAAAVeTOgqAViirAAQvmOWw9sk233.jpg
source_ip_addr = 172.16.0.1, file_size = 274328, create_timestamp = 2016-07-23 17:34:02, crc32 = -441387319
storage servers count: 1
1. 121.196.215.6:23000
View Code

 

下载:

技术分享
/**
 * <p><b>Title:</b> TestGet.java </p>
 * @Package fastdfs
 */
package fastdfs;

import java.io.File;

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;

/**
 * <p>
 * <b>TestGet Description:</b> (测试下载)
 * </p>
 * 
 * @author Xu Debiao <b>DATE:</b> 2016年7月23日 下午5:24:57
 */
public class TestGet {
    public static void main(String[] args) throws Exception {
        String classPath = new File(TestGet.class.getResource("/").getFile()).getCanonicalPath();
        String configFilePath = classPath + File.separator + "fdfs_client.conf";
        ClientGlobal.init(configFilePath);
        TrackerClient trackerClient = new TrackerClient();
        TrackerServer trackerServer = trackerClient.getConnection();
        StorageServer storageServer = null;
        StorageClient storageClient = new StorageClient(trackerServer, storageServer);

        String group_name = "group1";
        String remote_filename = "M00/00/00/rBAAAVeTOgqAViirAAQvmOWw9sk233.jpg";
        FileInfo fi = storageClient.get_file_info(group_name, remote_filename);
        String sourceIpAddr = fi.getSourceIpAddr();
        long size = fi.getFileSize();
        System.out.println("ip:" + sourceIpAddr + ",size:" + size);
    }
}
View Code

控制台:

技术分享
ip:172.16.0.1,size:274328
View Code

 

fastdfs_java测试代码

标签:

原文地址:http://www.cnblogs.com/debiao/p/5699214.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!