From 4249f953101de474a50f8c52f7f789b12e101520 Mon Sep 17 00:00:00 2001
From: chengdu <3281328128@qq.com>
Date: Sat, 13 Jul 2019 15:37:45 +0800
Subject: [PATCH 1/6] add gitignore
---
.gitignore | 3 +++
src/test/resources/fdfs_client.conf | 3 +--
2 files changed, 4 insertions(+), 2 deletions(-)
diff --git a/.gitignore b/.gitignore
index 789c91d..fc1e3bd 100644
--- a/.gitignore
+++ b/.gitignore
@@ -12,3 +12,6 @@ target
*.iws
*.log
.idea
+
+*.conf
+*.PNG
diff --git a/src/test/resources/fdfs_client.conf b/src/test/resources/fdfs_client.conf
index 2f3b351..deb3ff5 100644
--- a/src/test/resources/fdfs_client.conf
+++ b/src/test/resources/fdfs_client.conf
@@ -5,5 +5,4 @@ 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
+tracker_server = 192.168.1.101:22122
From 4b8b8397d1e5ec0ee9465d3f92019c53095adb78 Mon Sep 17 00:00:00 2001
From: chengdu <3281328128@qq.com>
Date: Sat, 13 Jul 2019 15:49:36 +0800
Subject: [PATCH 2/6] add fdfs upload download test
---
pom.xml | 15 +++
.../java/org/csource/fastdfs/FdfsTest.java | 99 +++++++++++++++++++
src/test/resources/fdfs_client.conf | 3 +-
src/test/resources/log4j.properties | 12 +++
4 files changed, 128 insertions(+), 1 deletion(-)
create mode 100644 src/test/java/org/csource/fastdfs/FdfsTest.java
create mode 100644 src/test/resources/log4j.properties
diff --git a/pom.xml b/pom.xml
index 1297337..7485dc4 100644
--- a/pom.xml
+++ b/pom.xml
@@ -17,6 +17,21 @@
1.6
+
+
+
+ org.slf4j
+ slf4j-log4j12
+ 1.7.26
+
+
+ junit
+ junit
+ 4.12
+ test
+
+
+
diff --git a/src/test/java/org/csource/fastdfs/FdfsTest.java b/src/test/java/org/csource/fastdfs/FdfsTest.java
new file mode 100644
index 0000000..fc3eea8
--- /dev/null
+++ b/src/test/java/org/csource/fastdfs/FdfsTest.java
@@ -0,0 +1,99 @@
+package org.csource.fastdfs;
+
+import org.csource.common.NameValuePair;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.*;
+import java.util.Arrays;
+
+/**
+ * @author chengdu
+ * @date 2019/7/13.
+ */
+public class FdfsTest {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(FdfsTest.class);
+
+ private static final String CONF_NAME = "fdfstest.conf";
+
+ private StorageClient storageClient;
+
+ private TrackerServer trackerServer;
+
+ @Before
+ public void initStorageClient() throws Exception {
+ ClientGlobal.init(CONF_NAME);
+ LOGGER.info("network_timeout=" + ClientGlobal.g_network_timeout + "ms");
+ LOGGER.info("charset=" + ClientGlobal.g_charset);
+ TrackerClient tracker = new TrackerClient();
+ trackerServer = tracker.getConnection();
+ StorageServer storageServer = null;
+ storageClient = new StorageClient(trackerServer, storageServer);
+ }
+
+ @After
+ public void closeClient() {
+ LOGGER.info("close connection");
+ if(trackerServer != null){
+ try {
+ trackerServer.close();
+ trackerServer.finalize();
+ }catch (Exception e){
+ e.printStackTrace();
+ }catch (Throwable e){
+ e.printStackTrace();
+ }
+ }
+ }
+
+ public void writeByteToFile(byte[] fbyte, String fileName) throws IOException {
+ BufferedOutputStream bos = null;
+ FileOutputStream fos = null;
+ File file = new File(fileName);
+ try {
+ fos = new FileOutputStream(file);
+ bos = new BufferedOutputStream(fos);
+ bos.write(fbyte);
+ } catch (Exception e) {
+ e.printStackTrace();
+ } finally {
+ if (bos != null) {
+ bos.close();
+ }
+ if (fos != null) {
+ fos.close();
+ }
+ }
+ }
+
+ @Test
+ public void upload() throws Exception{
+ NameValuePair[] metaList = new NameValuePair[1];
+ String local_filename = "build.PNG";
+ metaList[0] = new NameValuePair("fileName", local_filename);
+ File file = new File("C:/Users/chengdu/Desktop/build.PNG");
+ InputStream inputStream = new FileInputStream(file);
+ int length = inputStream.available();
+ byte[] bytes = new byte[length];
+ inputStream.read(bytes);
+ String[] result = storageClient.upload_file(bytes, null, metaList);
+ LOGGER.info("result {}", Arrays.asList(result));
+ Assert.assertEquals(2, result.length);
+ }
+
+ @Test
+ public void download() throws Exception {
+ String[] uploadresult = {"group1", "M00/00/00/wKgBZV0phl2ASV1nAACk1tFxwrM3814331"};
+ byte[] result = storageClient.download_file(uploadresult[0], uploadresult[1]);
+ String local_filename = "build.PNG";
+ writeByteToFile(result, local_filename);
+ File file = new File(local_filename);
+ Assert.assertTrue(file.isFile());
+ }
+
+}
diff --git a/src/test/resources/fdfs_client.conf b/src/test/resources/fdfs_client.conf
index deb3ff5..2f3b351 100644
--- a/src/test/resources/fdfs_client.conf
+++ b/src/test/resources/fdfs_client.conf
@@ -5,4 +5,5 @@ http.tracker_http_port = 8080
http.anti_steal_token = no
http.secret_key = FastDFS1234567890
-tracker_server = 192.168.1.101:22122
+tracker_server = 10.0.11.245:22122
+tracker_server = 10.0.11.246:22122
diff --git a/src/test/resources/log4j.properties b/src/test/resources/log4j.properties
new file mode 100644
index 0000000..dbcafc8
--- /dev/null
+++ b/src/test/resources/log4j.properties
@@ -0,0 +1,12 @@
+log4j.rootLogger=INFO, Console ,File
+
+#Console
+log4j.appender.Console=org.apache.log4j.ConsoleAppender
+log4j.appender.Console.layout=org.apache.log4j.PatternLayout
+log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
+
+#File
+log4j.appender.File = org.apache.log4j.FileAppender
+log4j.appender.File.File = log4j2.log
+log4j.appender.File.layout = org.apache.log4j.PatternLayout
+log4j.appender.File.layout.ConversionPattern =%d [%t] %-5p [%c] - %m%n
\ No newline at end of file
From 6311c744143ce01be474fe857b3806f1b7cb83a3 Mon Sep 17 00:00:00 2001
From: chengdu <3281328128@qq.com>
Date: Sat, 13 Jul 2019 18:10:57 +0800
Subject: [PATCH 3/6] =?UTF-8?q?=E5=88=A4=E6=96=AD=20socket=20=E7=8A=B6?=
=?UTF-8?q?=E6=80=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../org/csource/fastdfs/StorageClient.java | 8 +++
.../org/csource/fastdfs/TrackerServer.java | 32 ++++++++++++
.../java/org/csource/fastdfs/FdfsTest.java | 49 +++++++++++++------
3 files changed, 75 insertions(+), 14 deletions(-)
diff --git a/src/main/java/org/csource/fastdfs/StorageClient.java b/src/main/java/org/csource/fastdfs/StorageClient.java
index dec5b1b..0d3fae8 100644
--- a/src/main/java/org/csource/fastdfs/StorageClient.java
+++ b/src/main/java/org/csource/fastdfs/StorageClient.java
@@ -1748,6 +1748,14 @@ public class StorageClient {
this.storageServer.getSocket().getOutputStream().write(wholePkg);
}
+ public boolean isConnected(){
+ return trackerServer.isConnected();
+ }
+
+ public boolean isAvaliable(){
+ return trackerServer.isAvaliable();
+ }
+
/**
* Upload file by file buff
*
diff --git a/src/main/java/org/csource/fastdfs/TrackerServer.java b/src/main/java/org/csource/fastdfs/TrackerServer.java
index dffdb33..4b7dfd3 100644
--- a/src/main/java/org/csource/fastdfs/TrackerServer.java
+++ b/src/main/java/org/csource/fastdfs/TrackerServer.java
@@ -78,4 +78,36 @@ public class TrackerServer {
protected void finalize() throws Throwable {
this.close();
}
+
+ public boolean isConnected(){
+ boolean isConnected = false;
+ if (sock != null){
+ if (sock.isConnected()){
+ isConnected = true;
+ }
+ }
+ return isConnected;
+ }
+
+ public boolean isAvaliable(){
+ if(isConnected()){
+ if(sock.getPort() == 0){
+ return false;
+ }
+ if(sock.getInetAddress() == null){
+ return false;
+ }
+ if(sock.getRemoteSocketAddress() == null){
+ return false;
+ }
+ if(sock.isInputShutdown()){
+ return false;
+ }
+ if(sock.isOutputShutdown()){
+ return false;
+ }
+ return true;
+ }
+ return false;
+ }
}
diff --git a/src/test/java/org/csource/fastdfs/FdfsTest.java b/src/test/java/org/csource/fastdfs/FdfsTest.java
index fc3eea8..76645e9 100644
--- a/src/test/java/org/csource/fastdfs/FdfsTest.java
+++ b/src/test/java/org/csource/fastdfs/FdfsTest.java
@@ -36,20 +36,20 @@ public class FdfsTest {
storageClient = new StorageClient(trackerServer, storageServer);
}
- @After
- public void closeClient() {
- LOGGER.info("close connection");
- if(trackerServer != null){
- try {
- trackerServer.close();
- trackerServer.finalize();
- }catch (Exception e){
- e.printStackTrace();
- }catch (Throwable e){
- e.printStackTrace();
- }
- }
- }
+// @After
+// public void closeClient() {
+// LOGGER.info("close connection");
+// if(trackerServer != null){
+// try {
+// trackerServer.close();
+// trackerServer.finalize();
+// }catch (Exception e){
+// e.printStackTrace();
+// }catch (Throwable e){
+// e.printStackTrace();
+// }
+// }
+// }
public void writeByteToFile(byte[] fbyte, String fileName) throws IOException {
BufferedOutputStream bos = null;
@@ -96,4 +96,25 @@ public class FdfsTest {
Assert.assertTrue(file.isFile());
}
+ @Test
+ public void testUploadDownload() throws Exception {
+ NameValuePair[] metaList = new NameValuePair[1];
+ String local_filename = "build.PNG";
+ metaList[0] = new NameValuePair("fileName", local_filename);
+ File file = new File("C:/Users/chengdu/Desktop/build.PNG");
+ InputStream inputStream = new FileInputStream(file);
+ int length = inputStream.available();
+ byte[] bytes = new byte[length];
+ inputStream.read(bytes);
+ String[] result = storageClient.upload_file(bytes, null, metaList);
+ Assert.assertTrue(storageClient.isConnected());
+ // pool testOnborrow isAvaliable
+ Assert.assertTrue(storageClient.isAvaliable());
+ LOGGER.info("result {}", Arrays.asList(result));
+ byte[] resultbytes = storageClient.download_file(result[0], result[1]);
+ writeByteToFile(resultbytes, local_filename);
+ File downfile = new File(local_filename);
+ Assert.assertTrue(downfile.isFile());
+ }
+
}
From d73133267b6a819d9f06de82e9302180d95b6f5e Mon Sep 17 00:00:00 2001
From: chengdu <3281328128@qq.com>
Date: Sat, 13 Jul 2019 18:13:30 +0800
Subject: [PATCH 4/6] =?UTF-8?q?=E5=85=B3=E9=97=AD=20socket?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../java/org/csource/fastdfs/FdfsTest.java | 27 +++++++++----------
1 file changed, 13 insertions(+), 14 deletions(-)
diff --git a/src/test/java/org/csource/fastdfs/FdfsTest.java b/src/test/java/org/csource/fastdfs/FdfsTest.java
index 76645e9..5dae203 100644
--- a/src/test/java/org/csource/fastdfs/FdfsTest.java
+++ b/src/test/java/org/csource/fastdfs/FdfsTest.java
@@ -36,20 +36,19 @@ public class FdfsTest {
storageClient = new StorageClient(trackerServer, storageServer);
}
-// @After
-// public void closeClient() {
-// LOGGER.info("close connection");
-// if(trackerServer != null){
-// try {
-// trackerServer.close();
-// trackerServer.finalize();
-// }catch (Exception e){
-// e.printStackTrace();
-// }catch (Throwable e){
-// e.printStackTrace();
-// }
-// }
-// }
+ @After
+ public void closeClient() {
+ LOGGER.info("close connection");
+ if(trackerServer != null){
+ try {
+ trackerServer.finalize();
+ }catch (Exception e){
+ e.printStackTrace();
+ }catch (Throwable e){
+ e.printStackTrace();
+ }
+ }
+ }
public void writeByteToFile(byte[] fbyte, String fileName) throws IOException {
BufferedOutputStream bos = null;
From 9ee6e693fe52779f8d4831cd02adb5deccf4a874 Mon Sep 17 00:00:00 2001
From: chengdu <3281328128@qq.com>
Date: Sat, 13 Jul 2019 18:24:11 +0800
Subject: [PATCH 5/6] client close socket
---
src/main/java/org/csource/fastdfs/StorageClient.java | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/src/main/java/org/csource/fastdfs/StorageClient.java b/src/main/java/org/csource/fastdfs/StorageClient.java
index 0d3fae8..ec4cb7b 100644
--- a/src/main/java/org/csource/fastdfs/StorageClient.java
+++ b/src/main/java/org/csource/fastdfs/StorageClient.java
@@ -1756,6 +1756,10 @@ public class StorageClient {
return trackerServer.isAvaliable();
}
+ public void close() throws IOException {
+ trackerServer.close();
+ }
+
/**
* Upload file by file buff
*
From 086f4efc0fc32df9beb8a77e34e743c9a27959d4 Mon Sep 17 00:00:00 2001
From: chengdu <3281328128@qq.com>
Date: Sun, 14 Jul 2019 20:30:10 +0800
Subject: [PATCH 6/6] add getTrackerServer
---
pom.xml | 2 +-
.../org/csource/fastdfs/StorageClient.java | 18 ++++++++++++++++++
.../java/org/csource/fastdfs/FdfsTest.java | 4 ++--
3 files changed, 21 insertions(+), 3 deletions(-)
diff --git a/pom.xml b/pom.xml
index 7485dc4..cfe3f56 100644
--- a/pom.xml
+++ b/pom.xml
@@ -37,7 +37,7 @@
org.apache.maven.plugins
maven-compiler-plugin
- 3.5.1
+
UTF-8
diff --git a/src/main/java/org/csource/fastdfs/StorageClient.java b/src/main/java/org/csource/fastdfs/StorageClient.java
index ec4cb7b..9d5c3fa 100644
--- a/src/main/java/org/csource/fastdfs/StorageClient.java
+++ b/src/main/java/org/csource/fastdfs/StorageClient.java
@@ -28,6 +28,8 @@ public class StorageClient {
protected StorageServer storageServer;
protected byte errno;
+
+
/**
* constructor using global settings in class ClientGlobal
*/
@@ -1760,6 +1762,22 @@ public class StorageClient {
trackerServer.close();
}
+ public TrackerServer getTrackerServer() {
+ return trackerServer;
+ }
+
+ public void setTrackerServer(TrackerServer trackerServer) {
+ this.trackerServer = trackerServer;
+ }
+
+ public StorageServer getStorageServer() {
+ return storageServer;
+ }
+
+ public void setStorageServer(StorageServer storageServer) {
+ this.storageServer = storageServer;
+ }
+
/**
* Upload file by file buff
*
diff --git a/src/test/java/org/csource/fastdfs/FdfsTest.java b/src/test/java/org/csource/fastdfs/FdfsTest.java
index 5dae203..2893114 100644
--- a/src/test/java/org/csource/fastdfs/FdfsTest.java
+++ b/src/test/java/org/csource/fastdfs/FdfsTest.java
@@ -39,9 +39,9 @@ public class FdfsTest {
@After
public void closeClient() {
LOGGER.info("close connection");
- if(trackerServer != null){
+ if(storageClient != null){
try {
- trackerServer.finalize();
+ storageClient.close();
}catch (Exception e){
e.printStackTrace();
}catch (Throwable e){