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()); + } + }