判断 socket 状态
parent
4b8b8397d1
commit
6311c74414
|
@ -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
|
||||
*
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue