赞
踩
一.引入官方客户端依赖
- <!--FastDFS-->
- <dependency>
- <groupId>net.oschina.zcx7878</groupId>
- <artifactId>fastdfs-client-java</artifactId>
- <version>1.27.0.0</version>
- </dependency>
- <!--pool-->
- <dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-pool2</artifactId>
- <version>2.4.2</version>
- </dependency>
-- 以上只列举相关的依赖,其它依赖自行添加
二 . 配置文件
#######################FastDFS####################### #文件服务器地址 file_server_addr=192.168.17.128:80 # 最大连接数 并发量较大的话可加大该连接数 max_storage_connection=10 #超时配置 fastdfs.connect_timeout_in_seconds=100000 fastdfs.network_timeout_in_seconds=300000 fastdfs.charset=UTF-8 #token 防盗链功能 fastdfs.http_anti_steal_token=true #密钥,默认为FASTDFS1234567890 fastdfs.http_secret_key=FASTDFS1234567890 # TrackerServer port fastdfs.http_tracker_http_port=6666 ## Tracker Server, if more than one, separate with "," fastdfs.tracker_servers=192.168.17.128:22122 #######################FastDFS#######################
-- FastDFS的配置文件只支持properties类型的,可以整合放入application.properties里面,但是不能放入yml类型的里面
三.服务端代码编写
0.启动类配置
SpringbootFastdfsApplication.class
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class }) @EnableAutoConfiguration @MapperScan(value = "com.vesus.springbootfastdfs.mapper") @Configuration @PropertySource(value = "classpath:fastdfs.properties",encoding = "utf-8") public class SpringbootFastdfsApplication extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(SpringbootFastdfsApplication.class, args); } /** * war 包部署 * @param application * @return */ @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(SpringbootFastdfsApplication.class); } }
--
1.编写调度tracker池
- TrackerServerPool.class
- public class TrackerServerPool {
-
- private static Logger logger= LoggerFactory.getLogger(TrackerServerPool.class);
- /***
- * 配置文件路径
- */
- private static final String FASTDFS_CONFIG_PATH = "fastdfs.properties";
- /***
- * 最大连接数
- */
- @Value("${max_storage_connection}")
- private static int maxStorageConnection ;
- /***
- * TrackerServer 对象池
- */
- private static GenericObjectPool<TrackerServer> pool ;
- /***
- * 无参构造函数
- */
- private TrackerServerPool(){}
- /***
- * 获取TrackerServer连接池
- * @return
- * @throws Exception
- */
- public static synchronized GenericObjectPool<TrackerServer> getObjectPool() throws Exception{
- if (pool ==null){
- //加载配置文件
- ClientGlobal.initByProperties(FASTDFS_CONFIG_PATH);
- //pool配置,设置最大值和最小值
- GenericObjectPoolConfig config = new GenericObjectPoolConfig();
- config.setMinIdle(2);
- if (maxStorageConnection>0){
- config.setMaxIdle(maxStorageConnection);
- }
-
- pool = new GenericObjectPool<>(new TrackerServerFactory(),config) ;
- }
- return pool ;
- }
- /***
- * 获取TrackerServer
- * @return
- * @throws Exception
- */
- public static TrackerServer borrowObject()throws Exception{
- TrackerServer trackerServer = null;
- trackerServer = getObjectPool().borrowObject() ;
- return trackerServer ;
- }
- /***
- * 回收 TrackerServer
- * @param server
- * @throws Exception
- */
- public static void recycleObject(TrackerServer server) throws Exception{
- getObjectPool().returnObject(server);
- }
- }
2.fastdfs调度Tracker工厂
TrackerServerFactory.class
- public class TrackerServerFactory extends BasePooledObjectFactory<TrackerServer> {
-
- public TrackerServer create() throws Exception {
- //实例化TrackerClient
- TrackerClient client = new TrackerClient();
- //获取TrackerServer
- TrackerServer trackerServer = client.getConnection();
- return trackerServer;
- }
- public PooledObject<TrackerServer> wrap(TrackerServer server) {
- return new DefaultPooledObject<TrackerServer>(server);
- }
- }
3.编写fastdfs操作客户端
FastDFSClient.class
- @Component
- public class FastDFSClient {
-
- /**
- * 路径分隔符
- */
- public static final String SEPARATOR = "/";
- /**
- * Point
- */
- public static final String POINT = ".";
- /**
- *
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。