Merge pull request #19 from veryben/master

添加.gitignore文件并修复IniFileReader读取参数Bug和支持从文件系统加载配置
dev
YuQing 2017-05-19 10:16:49 +08:00 committed by GitHub
commit cee5b045a9
43 changed files with 933 additions and 877 deletions

14
.gitignore vendored 100644
View File

@ -0,0 +1,14 @@
bin
build
out
target
.settings
.classpath
.project
.gradle
.DS_Store
*.iml
*.ipr
*.iws
*.log
.idea

View File

@ -1,27 +1,27 @@
Copyright (C) 2008 Happy Fish / YuQing
FastDFS Java Client API may be copied only under the terms of
FastDFS Java Client API may be copied only under the terms of
the BSD license.
Please visit the FastDFS Home Page for more detail.
English language: http://english.csource.org/
Chinese language: http://www.csource.org/
The jar file is compiled by JDK1.5, you can download the last version
The jar file is compiled by JDK1.5, you can download the last version
from google code: http://code.google.com/p/fastdfs/downloads/list
run the FastDFS Java Client test:
java -cp <fastdfs_client_jar_filename> org.csource.fastdfs.test.TestClient <config_filename> <upload_filename>
eg.:
java -cp fastdfs_client_v1.25.jar org.csource.fastdfs.test.TestClient fdfs_client.conf c:\windows\system32\notepad.exe
java -cp <fastdfs_client_jar_filename> org.csource.fastdfs.test.TestClient fdfs_client.conf c:\windows\system32\notepad.exe
or:
java -cp fastdfs_client_v1.25.jar org.csource.fastdfs.test.TestClient fdfs_client.conf /usr/include/stdlib.h
java -cp <fastdfs_client_jar_filename> org.csource.fastdfs.test.TestClient fdfs_client.conf /usr/include/stdlib.h
run the FastDFS monitor:
java -cp <fastdfs_client_jar_filename> org.csource.fastdfs.test.Monitor <config_filename>
eg.:
java -cp fastdfs_client_v1.25.jar org.csource.fastdfs.test.Monitor fdfs_client.conf
java -cp <fastdfs_client_jar_filename> org.csource.fastdfs.test.Monitor fdfs_client.conf

37
build.xml 100644
View File

@ -0,0 +1,37 @@
<?xml version="1.0"?>
<project name="fastdfs-client-java" default="package" basedir=".">
<target name="init">
<property name="project.name" value="fastdfs-client-java"/>
<property name="project.version" value="1.27-SNAPSHOT"/>
<property name="project.java" value="${basedir}/src/main/java"/>
<property name="project.resources" value="${basedir}/src/main/resources"/>
<property name="project.build" value="${basedir}/build"/>
<property name="project.classes" value="${project.build}/classes"/>
<property name="target.jar.file" value="${project.build}/${project.name}-${project.version}.jar"/>
</target>
<target name="compile" depends="init">
<mkdir dir="${project.classes}"/>
<javac destdir="${project.classes}" encoding="UTF-8" failonerror="true" debug="on">
<src path="${project.java}"/>
</javac>
<copy todir="${project.classes}">
<fileset dir="${project.resources}" includes="**/*"/>
</copy>
</target>
<target name="package" depends="compile">
<jar jarfile="${target.jar.file}"
basedir="${project.classes}"
includes="**/*">
</jar>
</target>
<target name="clean" depends="init">
<delete dir="${project.build}"/>
</target>
</project>

10
fdfs_client.conf 100644
View File

@ -0,0 +1,10 @@
connect_timeout = 2
network_timeout = 30
charset = UTF-8
http.tracker_http_port = 8080
http.anti_steal_token = no
http.secret_key = FastDFS1234567890
tracker_server = 10.0.11.247:22122
tracker_server = 10.0.11.248:22122
tracker_server = 10.0.11.249:22122

75
pom.xml
View File

@ -1,52 +1,35 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<modelVersion>4.0.0</modelVersion>
<groupId>org.csource</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.26</version>
<name>fastdfs-client-java</name>
<description>fastdfs client with java</description>
<packaging>jar</packaging>
<groupId>org.csource</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27-SNAPSHOT</version>
<name>fastdfs-client-java</name>
<description>fastdfs client for java</description>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
</properties>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.test.failure.ignore>true</maven.test.failure.ignore>
<maven.test.skip>true</maven.test.skip>
<jdk.version>1.5</jdk.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<encoding>UTF-8</encoding>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
</plugins>
</build>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<encoding>UTF-8</encoding>
<skip>true</skip>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<excludes>
<exclude>**/test/*.class</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -1,55 +0,0 @@
<?xml version="1.0"?>
<!-- obfuscate -->
<project name="fastdfs_client" default="jar" basedir=".">
<target name="init">
<!-- The root directory of the workspace -->
<property name="fastdfs_client.home" value="."/>
<!-- The destination directory for the build -->
<property name="fastdfs_client.build" value="${fastdfs_client.home}/build"/>
<!-- The source directory for the build -->
<property name="fastdfs_client.src" value="${fastdfs_client.home}"/>
<!-- The destination directory for all the compiled classes. -->
<property name="fastdfs_client.bin_dir" value="${fastdfs_client.build}/classes"/>
<!-- The classpath used for building the workspace. -->
<property name="jar" value="${fastdfs_client.build}/fastdfs_client.jar"/>
</target>
<target name="compile" depends="init">
<mkdir dir="${fastdfs_client.bin_dir}"/>
<javac srcdir="${fastdfs_client.src}"
destdir="${fastdfs_client.bin_dir}"
includes="org/**/*.java"
debug="on"
encoding="UTF-8"
failonerror="true"/>
</target>
<target name="components" depends="init">
</target>
<!-- create .jar -->
<target name="jar" depends="compile">
<jar jarfile="${jar}"
basedir="${fastdfs_client.bin_dir}"
includes="org/csource/**">
<fileset dir="${fastdfs_client.src}">
<include name="*.conf"/>
</fileset>
</jar>
</target>
<target name="clean" depends="init">
<delete dir="${fastdfs_client.build}"/>
</target>
<!--<target name="core" depends="compile, components"/>-->
<target name="core" depends="compile"/>
</project>

View File

@ -0,0 +1,213 @@
/**
* Copyright (C) 2008 Happy Fish / YuQing
*
* FastDFS Java Client may be copied only under the terms of the GNU Lesser
* General Public License (LGPL).
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
**/
package org.csource.common;
import java.io.*;
import java.util.*;
import org.csource.common.*;
/**
* ini file reader / parser
* @author Happy Fish / YuQing
* @version Version 1.0
*/
public class IniFileReader
{
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);
}
/**
* get the config filename
* @return config filename
*/
public String getConfFilename()
{
return this.conf_filename;
}
/**
* get string value from config file
* @param name item name in config file
* @return string value
*/
public String getStrValue(String name)
{
Object obj;
obj = this.paramTable.get(name);
if (obj == null)
{
return null;
}
if (obj instanceof String)
{
return (String)obj;
}
return (String)((ArrayList)obj).get(0);
}
/**
* get int value from config file
* @param name item name in config file
* @param default_value the default value
* @return int value
*/
public int getIntValue(String name, int default_value)
{
String szValue = this.getStrValue(name);
if (szValue == null)
{
return default_value;
}
return Integer.parseInt(szValue);
}
/**
* get boolean value from config file
* @param name item name in config file
* @param default_value the default value
* @return boolean value
*/
public boolean getBoolValue(String name, boolean default_value)
{
String szValue = this.getStrValue(name);
if (szValue == null)
{
return default_value;
}
return szValue.equalsIgnoreCase("yes") || szValue.equalsIgnoreCase("on") ||
szValue.equalsIgnoreCase("true") || szValue.equals("1");
}
/**
* get all values from config file
* @param name item name in config file
* @return string values (array)
*/
public String[] getValues(String name)
{
Object obj;
String[] values;
obj = this.paramTable.get(name);
if (obj == null)
{
return null;
}
if (obj instanceof String)
{
values = new String[1];
values[0] = (String)obj;
return values;
}
Object[] objs = ((ArrayList)obj).toArray();
values = new String[objs.length];
System.arraycopy(objs, 0, values, 0, objs.length);
return values;
}
private void loadFromFile(String confFilePath) throws IOException {
InputStream in = null;
try {
// 优先从文件系统路径加载
if (new File(confFilePath).exists()) {
in = new FileInputStream(confFilePath);
//System.out.println("loadFrom...file path done");
}
// 从类路径加载
else {
in = classLoader().getResourceAsStream(confFilePath);
//System.out.println("loadFrom...class path done");
}
readToParamTable(in);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
if(in != null) in.close();
//System.out.println("loadFrom...finally...in.close(); done");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
private void readToParamTable(InputStream in) throws IOException {
this.paramTable = new Hashtable();
String line;
String[] parts;
String name;
String value;
Object obj;
ArrayList valueList;
InputStreamReader inReader = null;
BufferedReader bufferedReader = null;
try {
inReader = new InputStreamReader(in);
bufferedReader = new BufferedReader(inReader);
while ((line = bufferedReader.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);
}
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
if(bufferedReader != null) bufferedReader.close();
if(inReader != null) inReader.close();
//System.out.println("readToParamTable...finally...bufferedReader.close();inReader.close(); done");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
private static ClassLoader classLoader() {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
if (loader == null) {
loader = ClassLoader.getSystemClassLoader();
}
return loader;
}
}

View File

@ -1,50 +1,50 @@
package org.csource.fastdfs;
import java.io.IOException;
import java.io.OutputStream;
import org.csource.fastdfs.DownloadCallback;
/**
* Download file by stream (download callback class)
* @author zhouzezhong & Happy Fish / YuQing
* @version Version 1.11
*/
public class DownloadStream implements DownloadCallback
{
private OutputStream out;
private long currentBytes = 0;
public DownloadStream(OutputStream out)
{
super();
this.out = out;
}
/**
* recv file content callback function, may be called more than once when the file downloaded
* @param fileSize file size
* @param data data buff
* @param bytes data bytes
* @return 0 success, return none zero(errno) if fail
*/
public int recv(long fileSize, byte[] data, int bytes)
{
try
{
out.write(data, 0, bytes);
}
catch(IOException ex)
{
ex.printStackTrace();
return -1;
}
currentBytes += bytes;
if (this.currentBytes == fileSize)
{
this.currentBytes = 0;
}
return 0;
}
}
package org.csource.fastdfs;
import java.io.IOException;
import java.io.OutputStream;
import org.csource.fastdfs.DownloadCallback;
/**
* Download file by stream (download callback class)
* @author zhouzezhong & Happy Fish / YuQing
* @version Version 1.11
*/
public class DownloadStream implements DownloadCallback
{
private OutputStream out;
private long currentBytes = 0;
public DownloadStream(OutputStream out)
{
super();
this.out = out;
}
/**
* recv file content callback function, may be called more than once when the file downloaded
* @param fileSize file size
* @param data data buff
* @param bytes data bytes
* @return 0 success, return none zero(errno) if fail
*/
public int recv(long fileSize, byte[] data, int bytes)
{
try
{
out.write(data, 0, bytes);
}
catch(IOException ex)
{
ex.printStackTrace();
return -1;
}
currentBytes += bytes;
if (this.currentBytes == fileSize)
{
this.currentBytes = 0;
}
return 0;
}
}

View File

@ -1,182 +0,0 @@
/**
* Copyright (C) 2008 Happy Fish / YuQing
*
* FastDFS Java Client may be copied only under the terms of the GNU Lesser
* General Public License (LGPL).
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
**/
package org.csource.common;
import java.io.*;
import java.util.*;
import org.csource.common.*;
/**
* ini file reader / parser
* @author Happy Fish / YuQing
* @version Version 1.0
*/
public class IniFileReader
{
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);
}
/**
* get the config filename
* @return config filename
*/
public String getConfFilename()
{
return this.conf_filename;
}
/**
* get string value from config file
* @param name item name in config file
* @return string value
*/
public String getStrValue(String name)
{
Object obj;
obj = this.paramTable.get(name);
if (obj == null)
{
return null;
}
if (obj instanceof String)
{
return (String)obj;
}
return (String)((ArrayList)obj).get(0);
}
/**
* get int value from config file
* @param name item name in config file
* @param default_value the default value
* @return int value
*/
public int getIntValue(String name, int default_value)
{
String szValue = this.getStrValue(name);
if (szValue == null)
{
return default_value;
}
return Integer.parseInt(szValue);
}
/**
* get boolean value from config file
* @param name item name in config file
* @param default_value the default value
* @return boolean value
*/
public boolean getBoolValue(String name, boolean default_value)
{
String szValue = this.getStrValue(name);
if (szValue == null)
{
return default_value;
}
return szValue.equalsIgnoreCase("yes") || szValue.equalsIgnoreCase("on") ||
szValue.equalsIgnoreCase("true") || szValue.equals("1");
}
/**
* get all values from config file
* @param name item name in config file
* @return string values (array)
*/
public String[] getValues(String name)
{
Object obj;
String[] values;
obj = this.paramTable.get(name);
if (obj == null)
{
return null;
}
if (obj instanceof String)
{
values = new String[1];
values[0] = (String)obj;
return values;
}
Object[] objs = ((ArrayList)obj).toArray();
values = new String[objs.length];
System.arraycopy(objs, 0, values, 0, objs.length);
return values;
}
private void loadFromFile(String conf_filename) throws FileNotFoundException, IOException
{
//问题说明 使用中发现原来客户端打jar包后在另一个项目中引用另一个项目打jar包后运行时找不到客户端配置文件
String name;
String value;
Object obj;
ArrayList valueList;
InputStream is=null;
this.paramTable = new Hashtable();
try
{
Properties props;
try {
is = Thread.currentThread().getContextClassLoader().getResourceAsStream(conf_filename);
props = new Properties();
props.load(is);
} catch (Exception ex) {
is = new FileInputStream(conf_filename);
props = new Properties();
props.load(is);
}
Iterator<Map.Entry<Object, Object>> it = props.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Object, Object> entry = it.next();
name= entry.getKey().toString();
value = entry.getValue().toString();
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
{
if (is != null) {
is.close();
}
}
}
}

View File

@ -0,0 +1,27 @@
package org.csource.common;
/**
* Created by James on 2017/5/16.
*/
public class IniFileReaderTests {
public static void main(String[] args) throws Exception {
String conf_filename = "fdfs_client.conf";
IniFileReader iniFileReader = new IniFileReader(conf_filename);
System.out.println("getConfFilename: " + iniFileReader.getConfFilename());
System.out.println("connect_timeout: " + iniFileReader.getIntValue("connect_timeout", 3));
System.out.println("network_timeout: " + iniFileReader.getIntValue("network_timeout", 45));
System.out.println("charset: " + iniFileReader.getStrValue("charset"));
System.out.println("http.tracker_http_port: " + iniFileReader.getIntValue("http.tracker_http_port", 8080));
System.out.println("http.anti_steal_token: " + iniFileReader.getBoolValue("http.anti_steal_token", false));
System.out.println("http.secret_key: " + iniFileReader.getStrValue("http.secret_key"));
String[] tracker_servers = iniFileReader.getValues("tracker_server");
if (tracker_servers != null) {
System.out.println("tracker_servers.length: " + tracker_servers.length);
for (int i = 0; i < tracker_servers.length; i++) {
System.out.println(String.format("tracker_servers[%s]: %s", i, tracker_servers[i]));
}
}
}
}

View File

@ -0,0 +1,9 @@
connect_timeout = 2
network_timeout = 30
charset = UTF-8
http.tracker_http_port = 8080
http.anti_steal_token = no
http.secret_key = FastDFS1234567890
tracker_server = 10.0.11.245:22122
tracker_server = 10.0.11.246:22122