試着將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;
- }
- }