commit
fb86f94ad8
|
@ -12,3 +12,6 @@ target
|
|||
*.iws
|
||||
*.log
|
||||
.idea
|
||||
|
||||
*.conf
|
||||
*.PNG
|
||||
|
|
17
pom.xml
17
pom.xml
|
@ -17,12 +17,27 @@
|
|||
<jdk.version>1.6</jdk.version>
|
||||
</properties>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
<version>1.7.26</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.5.1</version>
|
||||
<!--<version>3.5.1</version>-->
|
||||
<configuration>
|
||||
<encoding>UTF-8</encoding>
|
||||
<source>${jdk.version}</source>
|
||||
|
|
|
@ -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
|
||||
*
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
|
@ -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
|
Loading…
Reference in New Issue