试着将FastDFS的javaClient整合进电商平台之中
FastDFS客户端初始化方式有两种一种是ClientGlobal.init(String)传入配置文件的路径,另外一种是通过ClientGlobal.set的方式,不过实在是不爽这种set方式啊,附上源码就知道了。
- public static int g_connect_timeout; //millisecond
- public static int g_network_timeout; //millisecond
- public static String g_charset;
- public static int g_tracker_http_port;
- public static boolean g_anti_steal_token; //if anti-steal token
- public static String g_secret_key; //generage token secret key
- public static TrackerGroup g_tracker_group;
- //set
- public static int getG_connect_timeout()
- {
- return g_connect_timeout;
- }
- public static void setG_connect_timeout(int connect_timeout)
- {
- ClientGlobal.g_connect_timeout = connect_timeout;
- }
- public static int getG_network_timeout()
- {
- return g_network_timeout;
- }
- public static void setG_network_timeout(int network_timeout)
- {
- ClientGlobal.g_network_timeout = network_timeout;
- }
- public static String getG_charset()
- {
- return g_charset;
- }
完全就是C程序的风格啊。作为一枚称职的java程序猿,实在是受不了这种方式。那么就用配置文件的方式了吧,来吧,试一试
- //先来试试相对路径
- ClientGlobal.init("/fastdfs.conf");
- //运行,配置文件未找到
- //那么在来试试classpath
- ClientGlobal.init("classpath:fastdfs.conf");
- //运行配置文件未找到。
- //看来要使用绝招了,直接给系统的绝对路径
- ClientGlobal.init("D:/seafood_project/seafood-core/src/main/resources/fastdfs.conf");
- //这下子总算是可以了。
- 但是又来问题了,项目部署的时候不可能这样子配置啊,好的选择肯定还是的从classpath下面找才是。没办法又看源码吧,直接上读取配置文件的源码,你就明白了为啥读取不到了
- public class IniFileReader
- {
- //居然在用hashTable。。。。。
- private Hashtable paramTable;
- private String conf_filename;
- /**
- * @param conf_filename config filename
- */
- public IniFileReader(String conf_filename) throws FileNotFoundException, IOException
- {
- this.conf_filename = conf_filename;
- //看来这里是我们需要的
- loadFromFile(conf_filename);
- }
- //看到loadFromFile的源码的时候,根本忍不住要改源码啊
- private void loadFromFile(String conf_filename) throws FileNotFoundException, IOException
- {
- FileReader fReader;
- BufferedReader buffReader;
- String line;
- String[] parts;
- String name;
- String value;
- Object obj;
- ArrayList valueList;
- fReader = new FileReader(conf_filename);
- buffReader = new BufferedReader(fReader);
- this.paramTable = new Hashtable();
- try
- {
- while ((line=buffReader.readLine()) != null)
- {
- line = line.trim();
- if (line.length() == 0 || line.charAt(0) == '#')
- {
- continue;
- }
- parts = line.split("=", 2);
- if (parts.length != 2)
- {
- continue;
- }
- name = parts[0].trim();
- value = parts[1].trim();
- obj = this.paramTable.get(name);
- if (obj == null)
- {
- this.paramTable.put(name, value);
- }
- else if (obj instanceof String)
- {
- valueList = new ArrayList();
- valueList.add(obj);
- valueList.add(value);
- this.paramTable.put(name, valueList);
- }
- else
- {
- valueList = (ArrayList)obj;
- valueList.add(value);
- }
- }
- }
- finally
- {
- fReader.close();
- }
- }
看到这儿应该明白为啥读不了了吧!泪奔啊!附上改写的源代码
- public class IniFileReader {
- private PropertiesLoader loader;
- private String conf_filename;
- /**
- *
- * <p>Title: </p>
- * <p>Description: </p>
- * @param conf_filename
- * @throws FileNotFoundException
- * @throws IOException
- */
- public IniFileReader(String conf_filename) throws FileNotFoundException, IOException {
- this.conf_filename = conf_filename;
- loadFromFile(conf_filename);
- }
- /**
- *
- * @Description: TODO(这里用一句话描述这个方法的作用)
- * @author LiuYi
- * @date 2014年6月5日 上午10:11:14
- * @return String
- */
- public String getConfFilename() {
- return this.conf_filename;
- }
- /**
- *
- * @Description: TODO(这里用一句话描述这个方法的作用)
- * @author LiuYi
- * @date 2014年6月6日 上午10:11:11
- * @param name
- * @return String
- */
- public String getStrValue(String name) {
- return this.loader.getProperty(name);
- }
- /**
- *
- * @Description: TODO(这里用一句话描述这个方法的作用)
- * @author LiuYi
- * @date 2014年6月5日 上午10:11:01
- * @param name
- * @param default_value
- * @return int
- */
- public int getIntValue(String name, int default_value) {
- String szValue = this.loader.getProperty(name);
- if (szValue == null || "".equals(szValue)) {
- return default_value;
- }
- return Integer.parseInt(szValue);
- }
- /**
- *
- * @Description: TODO(这里用一句话描述这个方法的作用)
- * @author LiuYi
- * @date 2014年6月5日 上午10:10:53
- * @param name
- * @param default_value
- * @return boolean
- */
- public boolean getBoolValue(String name, boolean default_value) {
- String szValue = this.loader.getProperty(name);
- if (szValue == null) {
- return default_value;
- }
- return szValue.equalsIgnoreCase("yes") || szValue.equalsIgnoreCase("on")
- || szValue.equalsIgnoreCase("true") || szValue.equals("1");
- }
- /**
- *
- * @Description: TODO()
- * @author LiuYi
- * @date 2014年6月5日 上午10:10:35
- * @param name
- * @return String[]
- */
- public String[] getValues(String name) {
- List<String> values = new ArrayList<String>();
- String val = this.loader.getProperty(name);
- if (val.contains(",")) {
- for (String v : val.split(",")) {
- values.add(v);
- }
- } else {
- values.add(val);
- }
- return values.toArray(new String[values.size()]);
- }
- /**
- *
- * @Description: TODO(这里用一句话描述这个方法的作用)
- * @author LiuYi
- * @date 2014年6月5日 上午10:11:54
- * @param resourcesPaths
- * @throws FileNotFoundException
- * @throws IOException void
- */
- private void loadFromFile(String... resourcesPaths) throws FileNotFoundException, IOException {
- this.loader = new PropertiesLoader(resourcesPaths);
- }
- }
PropertiesLoader依赖了Spring的io包附上源码
- /**
- * Properties文件载入工具类. 可载入多个properties文件, 相同的属性在最后载入的文件中的值将会覆盖之前的值,但以System的Property优先.
- * @author LiuYi
- * @version 2014-05-21
- */
- public class PropertiesLoader {
- private static ResourceLoader resourceLoader = new DefaultResourceLoader();
- private final Properties properties;
- public PropertiesLoader(String... resourcesPaths) {
- properties = loadProperties(resourcesPaths);
- }
- public Properties getProperties() {
- return properties;
- }
- /**
- * 取出Property,但以System的Property优先,取不到返回空字符串.
- */
- private String getValue(String key) {
- String systemProperty = System.getProperty(key);
- if (systemProperty != null) {
- return systemProperty;
- }
- if (properties.containsKey(key)) {
- return properties.getProperty(key);
- }
- return "";
- }
- /**
- * 取出String类型的Property,但以System的Property优先,如果都为Null则抛出异常.
- */
- public String getProperty(String key) {
- String value = getValue(key);
- if (value == null) {
- throw new NoSuchElementException();
- }
- return value;
- }
- /**
- * 取出String类型的Property,但以System的Property优先.如果都为Null则返回Default值.
- */
- public String getProperty(String key, String defaultValue) {
- String value = getValue(key);
- return value != null ? value : defaultValue;
- }
- /**
- * 取出Integer类型的Property,但以System的Property优先.如果都为Null或内容错误则抛出异常.
- */
- public Integer getInteger(String key) {
- String value = getValue(key);
- if (value == null) {
- return null;
- }
- return Integer.valueOf(value);
- }
- /**
- * 取出Integer类型的Property,但以System的Property优先.如果都为Null则返回Default值,如果内容错误则抛出异常
- */
- public Integer getInteger(String key, Integer defaultValue) {
- String value = getValue(key);
- return value != null ? Integer.valueOf(value) : defaultValue;
- }
- /**
- * 取出Double类型的Property,但以System的Property优先.如果都为Null或内容错误则抛出异常.
- */
- public Double getDouble(String key) {
- String value = getValue(key);
- if (value == null) {
- throw new NoSuchElementException();
- }
- return Double.valueOf(value);
- }
- /**
- * 取出Double类型的Property,但以System的Property优先.如果都为Null则返回Default值,如果内容错误则抛出异常
- */
- public Double getDouble(String key, Integer defaultValue) {
- String value = getValue(key);
- return value != null ? Double.valueOf(value) : defaultValue;
- }
- /**
- * 取出Boolean类型的Property,但以System的Property优先.如果都为Null抛出异常,如果内容不是true/false则返回false.
- */
- public Boolean getBoolean(String key) {
- String value = getValue(key);
- if (value == null) {
- throw new NoSuchElementException();
- }
- return Boolean.valueOf(value);
- }
- /**
- * 取出Boolean类型的Property,但以System的Property优先.如果都为Null则返回Default值,如果内容不为true/false则返回false.
- */
- public Boolean getBoolean(String key, boolean defaultValue) {
- String value = getValue(key);
- return value != null ? Boolean.valueOf(value) : defaultValue;
- }
- /**
- * 载入多个文件, 文件路径使用Spring Resource格式.
- */
- private Properties loadProperties(String... resourcesPaths) {
- Properties props = new Properties();
- for (String location : resourcesPaths) {
- // logger.debug("Loading properties file from:" + location);
- InputStream is = null;
- try {
- Resource resource = resourceLoader.getResource(location);
- is = resource.getInputStream();
- props.load(is);
- } catch (IOException ex) {
- } finally {
- try {
- if(is!=null)
- is.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- return props;
- }
- }