项目采用springcloud框架,将fastDfs服务打成jar部署到服务器上,进行文件上传时,系统报错找不到fastdfs的配置文件。
所以我们可以换一种方式来读取配置文件,ClientGlobal里有个方法initByProperties,我们可以将配置文件已propertie文件的形式注入,所以这里我们可以采用类加载器将配置文件转换后才能Properties。
Properties properties = new Properties();
// 使用ClassLoader加载properties配置文件生成对应的输入流
InputStream in = FastdfsUtil.class.getClassLoader().getResourceAsStream("fdfs_client.properties");
properties.load(in);
ClientGlobal.initByProperties(properties);
这里需要注意的是,由于之前我是采用的ClientGlobal.init(String conf_filename),直接注入fastdfs.conf文件,现在是ClientGlobal.initByProperties(Propertie pro)注入fastdfs.propertie文件,两种格式的文件的key是不一样的,所以我们需要修改conf里的key.
fastdfs.connect_timeout_in_seconds = 5
fastdfs.network_timeout_in_seconds = 30
fastdfs.charset = UTF-8
fastdfs.http_tracker_http_port = 80
fastdfs.tracker_servers = 192.168.1.201:22122
#注1:properties 配置文件中属性名跟 conf 配置文件不尽相同,并且统一加前缀"fastdfs.",便于整合到用户项目配置文件
#注2:fastdfs.tracker_servers 配置项不能重复属性名,多个 tracker_server 用逗号","隔开
#注3:除了fastdfs.tracker_servers,其它配置项都是可选的
参考: