java读取xxx.properties文件

在项目中会有很多地方用到同一个东西,类似图片服务器,如果每个人都单独写一个方法来调用图片服务器的话,不但代码后期不好维护,而且如果图片服务器修改了的话会修改很多。那就要写一个公共方法,并且把图片服务器地址放到配置文件里

1、配置xxx.properties文件

#服务器ip地址
file_ip=http://127.0.0.1
#服务器端口号
file_port=8080

2、配置工具类

	/**
     * 创建Properties类
     */
    private static Properties properties = new Properties();

    /**
     * 获取配置文件
     */
    static {
        try {
            // 1.加载xxx.properties配置文件
            properties.load(FileUtil.class.getClassLoader()
                    .getResourceAsStream("xxx.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 返回图片服务器地址
     * file_ip: 对应配置文件中IP地址名称
     * file_port: 对应配置文件中端口号名称
     * @return String
     */
    @ResponseBody
    public String fielUtil() {
    	//定义StringBuffer用来拼接url并返回
        StringBuffer stringBuffer = new StringBuffer ();
        //获取图片服务器ip地址
        String ip = properties.getProperty("file_ip");
        //获取图片服务器端口号
        String port = properties.getProperty("file_port");
        //拼接图片服务器ip地址
        stringBuffer.append(ip).append(":").append(port);
        //http://127.0.0.1:8080
        String filePath = stringBuffer.toString();
        return filePath ;
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值