FastDFS(三)Java实现文件上传下载

FastDFS(三)Java实现文件上传下载

<dependencies>
    <!--fastdfs的java客户端-->
    <dependency>
        <groupId>net.oschina.zcx7878</groupId>
        <artifactId>fastdfs-client-java</artifactId>
        <version>1.27.0.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-io</artifactId>
        <version>1.3.2</version>
    </dependency>
</dependencies>

在resources下创建config目录,在config目录下创建 fastdfs-client.properties,内容如下:

##fastdfs-client.properties
fastdfs.connect_timeout_in_seconds = 5
fastdfs.network_timeout_in_seconds = 30
fastdfs.charset = UTF-8
fastdfs.http_anti_steal_token = false
fastdfs.http_secret_key = FastDFS1234567890
fastdfs.http_tracker_http_port = 80
fastdfs.tracker_servers = IP:22122

文件上传

public class TestUpload {

    public static void main(String[] args) {
        try {
            //加载配置文件
            ClientGlobal.initByProperties("config/fastdfs-client.properties");
            //创建tracker客户端
            TrackerClient trackerClient = new TrackerClient();
            //通过客户端获取tracker的连接服务并返回
            TrackerServer trackerServer = trackerClient.getConnection();
            //声明storage服务
            StorageServer storageServer = null;
            //定义文件元信息
            NameValuePair[] list = new NameValuePair[1];
            list[0] = new NameValuePair("fileName","1.png");
            //定义storage客户端
            StorageClient1 client = new StorageClient1(trackerServer, storageServer);
            String fileId = client.upload_appender_file1("E:\\daimashangchuan\\course-notes\\第七阶段\\FastDFS\\1.png", "png", list);
            //   group1/M00/00/00/ChcDu2ALonSEO3NvAAAAAIy3Q2g733.png
            /*
                group1:一台服务器,就是一个组
                M00: store_path0 ----> /home/fastdfs/fdfs_storage/data
                00/00:两级数据目录
            */
            System.out.println(fileId);
            //关闭客户端
            trackerServer.close();

        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }
    }

}

文件查询

public class TestQuery {

    public static void main(String[] args) {
        try {
            //加载配置文件
            ClientGlobal.initByProperties("config/fastdfs-client.properties");
            //创建tracker客户端
            TrackerClient trackerClient = new TrackerClient();
            //通过客户端获取tracker的连接服务并返回
            TrackerServer trackerServer = trackerClient.getConnection();
            //声明storage服务
            StorageServer storageServer = null;
            //定义storage客户端
            StorageClient1 client = new StorageClient1(trackerServer, storageServer);

            String string = "group1/M00/00/00/ChcDu2ALonSEO3NvAAAAAIy3Q2g733.png";
            //查询信息
            FileInfo fileInfo = client.query_file_info1(string);
            System.out.println("fileInfo:" + fileInfo);

            //关闭客户端
            trackerServer.close();

        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }
    }

}

文件下载

public class TestDownload {

    public static void main(String[] args) {
        try {
            //加载配置文件
            ClientGlobal.initByProperties("config/fastdfs-client.properties");
            //创建tracker客户端
            TrackerClient trackerClient = new TrackerClient();
            //通过客户端获取tracker的连接服务并返回
            TrackerServer trackerServer = trackerClient.getConnection();
            //声明storage服务
            StorageServer storageServer = null;
            //定义storage客户端
            StorageClient1 client = new StorageClient1(trackerServer, storageServer);

            String string = "group1/M00/00/00/ChcDu2ALonSEO3NvAAAAAIy3Q2g733.png";
            //下载
            byte[] bytes = client.download_file1(string);
            //将字节数组转换为文件
            FileOutputStream fileOutputStream = new FileOutputStream(new File("E:\\11111.png"));
            fileOutputStream.write(bytes);
            //关闭流
            fileOutputStream.close();

            //关闭客户端
            trackerServer.close();

        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值