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