之前给大家简单的介绍了fastdfs是什么,那么下面就再次的和小编一起来了解一下fastdfs的使用方面相关的内容吧。
fastdfs使用Demo
需求:
将图片上传到服务器上面,在控制台打印服务器返回的信息。
1、创建Maven工程fastDFSdemo
因为,fastdfs客户端jar包并不在中央仓库当中,所以,就要使用下面的命令来手动的安装jar包到Maven本地仓库当中。
将jar包放到d盘setup目录。
mvn install: install - file - DgroupId = org.csource.fastdfs - DartifactId = fastdfs - Dversion = 1.2 - Dpackaging = jar - Dfile = d: setup fastdfs_client_v1 .20.jar
pom.xml当中引入:fastdfs和fileupload的jar包。
<dependency> <groupId>org.csource.fastdfs</groupId> <artifactId>fastdfs</artifactId> <version>1.2</version> </dependency>< !--用于前端上传文件-- > <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency>
2、添加配置文件fast_client.conf
将这个当中的服务器的地址,设置为自己的fastdfs地址
# connect timeout in seconds # default value is 30s connect_timeout=30 # network timeout in seconds # default value is 30s network_timeout=60 # the base path to store log files base_path=/home/fastdfs # tracker_server can ocur more than once, and tracker_server format is # "host:port", host can be hostname or ip address tracker_server=192.168.25.133:22122 #standard log level as syslog, case insensitive, value list: ### emerg for emergency ### alert ### crit for critical ### error ### warn for warning ### notice ### info ### debug log_level=info # if use connection pool # default value is false # since V4.05 use_connection_pool = false # connections whose the idle time exceeds this time will be closed # unit: second # default value is 3600 # since V4.05 connection_pool_max_idle_time = 3600 # if load FastDFS parameters from tracker server # since V4.05 # default value is false load_fdfs_parameters_from_tracker=false # if use storage ID instead of IP address # same as tracker.conf # valid only when load_fdfs_parameters_from_tracker is false # default value is false # since V4.05 use_storage_id = false # specify storage ids filename, can use relative or absolute path # same as tracker.conf # valid only when load_fdfs_parameters_from_tracker is false # since V4.05 storage_ids_filename = storage_ids.conf #HTTP settings http.tracker_server_port=80 #use "#include" directive to include HTTP other settiongs ##include http.conf
3、创建main方法
主要是要设置配置文件的路径和本地上传图片的路径
package com.pinyougou.fastDFS; import java.io.FileNotFoundException; import java.io.IOException; import org.csource.fastdfs.ClientGlobal; import org.csource.fastdfs.StorageClient; import org.csource.fastdfs.StorageServer; import org.csource.fastdfs.TrackerClient; import org.csource.fastdfs.TrackerServer; public class fastDemo { public static void main(String[] args) throws FileNotFoundException, IOException, Exception { //加载配置文件 ClientGlobal.init("E:\learnWorkspaces\demo\fastDFS\src\main\resources\fdfs_client.conf"); //构建一个管理者客户端 TrackerClient trackerClient = new TrackerClient(); //连接管理者服务端 TrackerServer trackerServer = trackerClient.getConnection(); //声明存储服务器 StorageServer storageServer = null; //获取村粗服务器客户端对象 StorageClient storageClient = new StorageClient(trackerServer, storageServer); //上传文件 String[] upload_file = storageClient.upload_file("D:\tools\FastDFS\aa.jpg", "jpg", null); //显示上传结果 for (String file: upload_file) { System.out.println(file); } } }
控制台输出结果格式:
group1 M00/00/00/wKgZhVkMP4KAZEy-AAA-tCf93Fo973.jpg
在浏览器汇当中输入url就能够访问图片。
关于fastdfs使用方面的内容就给你介绍到这里了,更多关于fastdfs方面的知识,可以继续关注本站的java架构师栏目来进行了解哦。