fastDFS分布式文件系统--文件上传/下载/查询完整代码实现

搭建环境

这里我们使用javaApi测试文件的上传,java版本的fastdfs-client地址在: https://github.com/happyfish100/fastdfs-client-java,参考此工程编写测试用例。

fastDFS分布式文件系统--文件上传/下载/查询完整代码实现_第1张图片

fastDFS分布式文件系统--文件上传/下载/查询完整代码实现_第2张图片

1)创建maven工程

pom.xml


org.springframework.boot
spring‐boot‐starter‐parent
1.5.9.RELEASE

com.xuecheng
test‐fastDSF
1.0‐SNAPSHOT


org.springframework.boot
spring‐boot‐starter‐web



net.oschina.zcx7878
fastdfs‐client‐java
1.27.0.0


org.springframework.boot
spring‐boot‐starter‐test
test


org.apache.commons
commons‐io
1.3.2


2) 配置 文件

在classpath:config下创建fastdfs-client.properties文件

fastdfs.connect_timeout_in_seconds = 5 #http连接超时时间
fastdfs.network_timeout_in_seconds = 30 #tracker与storage网络通信超时时间
fastdfs.charset = UTF‐8 #字符编码
fastdfs.tracker_servers = 192.168.101.64:22122 #tracker服务器地址,多个地址中间用英文逗号分隔

文件上传

//上传文件
    @Test
    public void testUpload() {
        try {
            ClientGlobal.initByProperties("config/fastdfs‐client.properties");
            System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms");
            System.out.println("charset=" + ClientGlobal.g_charset);
            //创建客户端
            TrackerClient tc = new TrackerClient();
            //连接tracker Server
            TrackerServer ts = tc.getConnection();
            if (ts == null) {
                System.out.println("getConnection return null");
                return;
            }
            //获取一个storage server
            StorageServer ss = tc.getStoreStorage(ts);
            if (ss == null) {
                System.out.println("getStoreStorage return null");
            }
            //创建一个storage存储客户端
            StorageClient1 sc1 = new StorageClient1(ts, ss);
            NameValuePair[] meta_list = null; //new NameValuePair[0];
            String item = "C:\\Users\\admin\\Desktop\\1.png";
            String fileid;
            fileid = sc1.upload_file1(item, "png", meta_list);
            System.out.println("Upload local file " + item + " ok, fileid=" + fileid);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

文件查询

//查询文件
@Test
public void testQueryFile() throws IOException, MyException {
    ClientGlobal.initByProperties("config/fastdfs‐client.properties");
    TrackerClient tracker = new TrackerClient();
    TrackerServer trackerServer = tracker.getConnection();
    StorageServer storageServer = null;
    StorageClient storageClient = new StorageClient(trackerServer,storageServer);
    FileInfo fileInfo =     storageClient.query_file_info("group1","M00/00/01/wKhlQFrKBSOAW5AWAALcAg10vf4862.png");
    System.out.println(fileInfo);
}

文件下载

//下载文件
    @Test
    public void testDownloadFile() throws IOException, MyException {
        ClientGlobal.initByProperties("config/fastdfs‐client.properties");
        TrackerClient tracker = new TrackerClient();
        TrackerServer trackerServer = tracker.getConnection();
        StorageServer storageServer = null;
        StorageClient1 storageClient1 = new StorageClient1(trackerServer,
                storageServer);
        byte[] result =
                storageClient1.download_file1("group1/M00/00/01/wKhlQFrKBSOAW5AWAALcAg10vf4862.png");
        File file = new File("d:/1.png");
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        fileOutputStream.write(result);
        fileOutputStream.close();
    }

 

你可能感兴趣的:(后台编程)