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

  • 最近做了一个项目,包含文件上传功能,本地运行很正常,打成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

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

你可能感兴趣的:(jar,spring,java)