接上一节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();
}
如有疑问 欢迎评论