springboot打包成jar包部署,无法访问到fastDFS配置文件【已解决】

本文介绍了作者在将项目打包为jar部署服务器时遇到的fastDFS配置问题,通过转换为.properties文件并使用不同方式读取,成功解决了无法访问配置文件的问题。重点在于配置文件迁移和读取策略的调整。
摘要由CSDN通过智能技术生成
  • 最近做了一个项目,包含文件上传功能,本地运行很正常,打成jar包部署到服务器上时,结果就报错了,花了几个小时的时间才发现是因为无法访问到fastDFS的配置文件dfds_client.conf,导致报错,以下是我之前的读取文件的代码:
filePath = new ClassPathResource("fdfs_client.conf").getFile().getAbsolutePath();

由于打包成的是 jar 包,不是war包,无法读取到.conf 文件,因此可以换一种配置文件:.properties配置文件,因为 ClientGlobal 类中有 initByProperties() 方法,可以读取 properties文件;修改如下:

  • .conf 配置文件
connect_timeout = 2
network_timeout = 30
charset = UTF-8
http.tracker_http_port = 443
http.anti_steal_token = no
http.secret_key = FastDFS1234567890abc
tracker_server = 82.157.172.13:22122

connection_pool.enabled = true
connection_pool.max_count_per_entry = 500
connection_pool.max_idle_time = 3600
connection_pool.max_wait_time_in_ms = 1000
  • .properties配置文件
fastdfs.connect_timeout_in_seconds = 2
fastdfs.network_timeout_in_seconds = 30
fastdfs.charset = UTF-8
fastdfs.http.tracker_http_port = 443
fastdfs.http.anti_steal_token = false
fastdfs.http.secret_key = FastDFS1234567890
fastdfs.tracker_servers = 82.157.172.13:22122

fastdfs.connection_pool.enabled = true
fastdfs.connection_pool.max_count_per_entry = 500
fastdfs.connection_pool.max_idle_time = 3600
fastdfs.connection_pool.max_wait_time_in_ms = 1000
#注意:
#1. properties文件中都需要添加 fastdfs 前缀;
#2. conf 文件和 properties文件中有几项名称有变动:
# 		connect_timeout  ------>  connect_timeout_in_seconds 
#		network_timeout ------->  network_timeout_in_seconds 
#		tracker_server     -------->  tracker_servers 
#3. fastdfs.tracker_servers 中,多个 tracker_server 用逗号","隔开
  • 读取properties文件
//方式1:
	String properties = "fdfs_client.properties";
    ClientGlobal.initByProperties(properties);
 
//方式2:
	Properties properties = new Properties();
    InputStream resourceAsStream = FastDFSUtils.class.getClassLoader().getResourceAsStream("fdfs_client.properties");
    properties.load(resourceAsStream);
    ClientGlobal.initByProperties(properties);

//以上两个方式本人都测试过,都可以正常运行,无报错;

参考博客:https://blog.csdn.net/qq_41917197/article/details/103912746

非常感谢这位博主解决了这个问题!!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值