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/pom.xml b/pom.xml index 1297337..cfe3f56 100644 --- a/pom.xml +++ b/pom.xml @@ -17,12 +17,27 @@ 1.6 + + + + org.slf4j + slf4j-log4j12 + 1.7.26 + + + junit + junit + 4.12 + test + + + org.apache.maven.plugins maven-compiler-plugin - 3.5.1 + UTF-8 ${jdk.version} diff --git a/src/main/java/org/csource/fastdfs/StorageClient.java b/src/main/java/org/csource/fastdfs/StorageClient.java index dec5b1b..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 */ @@ -1748,6 +1750,34 @@ public class StorageClient { this.storageServer.getSocket().getOutputStream().write(wholePkg); } + public boolean isConnected(){ + return trackerServer.isConnected(); + } + + public boolean isAvaliable(){ + return trackerServer.isAvaliable(); + } + + public void close() throws IOException { + 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/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 new file mode 100644 index 0000000..2893114 --- /dev/null +++ b/src/test/java/org/csource/fastdfs/FdfsTest.java @@ -0,0 +1,119 @@ +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(storageClient != null){ + try { + storageClient.close(); + }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()); + } + + @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()); + } + +} 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