add fdfs upload download test

dev
chengdu 2019-07-13 15:49:36 +08:00
parent 4249f95310
commit 4b8b8397d1
4 changed files with 128 additions and 1 deletions

15
pom.xml
View File

@ -17,6 +17,21 @@
<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>

View File

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

View File

@ -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

View File

@ -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