fastdfs02-创建springboot进行连接fastdfs

接上一节fastdfs01-Linux下安装fastdfs:https://blog.csdn.net/xixiyuguang/article/details/105160807

fastdfs相关

1、fastdfs01-Linux下安装fastdfs(tracker和storage安装): https://blog.csdn.net/xixiyuguang/article/details/105160807

2、fastdfs02-创建springboot进行连接fastdfs:https://blog.csdn.net/xixiyuguang/article/details/105161424

fastdfs01-FastDFS-配置token教程及问题:https://blog.csdn.net/xixiyuguang/article/details/104772505

fastdfs02-springboot加入FastDFS工具类: https://blog.csdn.net/xixiyuguang/article/details/105142448

 

1、创建springboot+maven项目

1.1、maven依赖

 
<dependency>
    <groupId>net.oschina.zcx7878</groupId>
    <artifactId>fastdfs-client-java</artifactId>
    <version>1.27.0.0</version>
</dependency>

1.2、配置fastdfs-client.properties

只需要将 fastdfs.tracker_servers = 192.168.116.128:22122。修改为自己的ip和端口号即可

## 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 = 10.0.11.201:22122,10.0.11.202:22122,10.0.11.203:22122
fastdfs.tracker_servers = 192.168.116.128:22122

## Whether to open the connection pool, if not, create a new connection every time
fastdfs.connection_pool.enabled = true

## max_count_per_entry: max connection count per host:port , 0 is not limit
fastdfs.connection_pool.max_count_per_entry = 500

## connections whose the idle time exceeds this time will be closed, unit: second, default value is 3600
fastdfs.connection_pool.max_idle_time = 3600

## Maximum waiting time when the maximum number of connections is reached, unit: millisecond, default value is 1000
fastdfs.connection_pool.max_wait_time_in_ms = 1000

2、上传代码

 

@Test
void upload() throws IOException, MyException {

    ClientGlobal.initByProperties("fastdfs-client.properties");

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

    StorageServer storageServer = null;
    StorageClient1 storageClient1 = new StorageClient1(trackerServer, storageServer);


    NameValuePair pairs[] = null;

    String file1 = storageClient1.upload_file1("D:\\header.jpg", "jpg", pairs);
    System.out.println("打印: " + file1);

}

 

3、下载代码

 

 

@Test
void download() throws IOException, MyException {

    ClientGlobal.initByProperties("fastdfs-client.properties");

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

    StorageServer storageServer = null;
    StorageClient1 storageClient1 = new StorageClient1(trackerServer, storageServer);

    byte[] bytes = storageClient1.download_file1("group1/M00/00/00/wKh0gF5ncAmATxokAACStPtAfGg349.jpg");

    FileOutputStream fos = new FileOutputStream(new File("D:\\header-2.jpg"));
    fos.write(bytes);
    fos.close();
}

如有疑问 欢迎评论

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值