Commit b8389a87 by huangfusuper

fastdfs组件封装

parent 4058490d
......@@ -46,6 +46,17 @@
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>net.oschina.zcx7878</groupId>
<artifactId>fastdfs-client-java</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
......
package com.byit.executor.conf;
import com.byit.executor.filesystem.FileSystem;
import org.apache.commons.io.FileUtils;
import org.csource.common.MyException;
import java.io.IOException;
import java.util.Map;
/**
* 文件系统上下文对象
* @author huangfu
*/
public class FileSystemContext {
private FileSystem fileSystem;
public FileSystemContext(FileSystem fileSystem) {
this.fileSystem = fileSystem;
}
public String uploadFile(byte[] fileBuffer, String fileExtName, Map<String, String> mateDaTA) throws IOException, MyException {
return fileSystem.uploadFile(fileBuffer,fileExtName,mateDaTA);
}
public byte[] downloaderFile(String remPath) throws IOException, MyException {
return fileSystem.downloaderFile(remPath);
}
public void fileRemove(String remPath) throws IOException, MyException {
fileSystem.fileRemove(remPath);
}
public void fileAppend(String filePath, byte[] fileBuffer) throws IOException, MyException {
fileSystem.fileAppend(filePath,fileBuffer);
}
public Map<String, String> getFileMate(String filePath) throws IOException, MyException {
return fileSystem.getFileMate(filePath);
}
}
package com.byit.executor.filesystem;
import cn.hutool.core.collection.CollectionUtil;
import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* FastDFS设计模式
* @author huangfu
*/
public class FastDfsFileSystem implements FileSystem {
static {
try {
ClientGlobal.init("fdfs_client.conf");
} catch (IOException | MyException e) {
e.printStackTrace();
}
}
@Override
public String uploadFile(byte[] fileBuffer, String fileExtName, Map<String, String> mateDaTa) throws IOException, MyException {
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
StorageClient1 storageClient1 = new StorageClient1(trackerServer, null);
NameValuePair[] mathList = new NameValuePair[mateDaTa.size()];
if(CollectionUtil.isNotEmpty(mateDaTa)){
Set<Map.Entry<String, String>> mateSet = mateDaTa.entrySet();
int i = 0;
for (Map.Entry<String, String> mate : mateSet) {
mathList[i] = new NameValuePair(mate.getKey(),mate.getValue());
i++;
}
}
String fileId = storageClient1.upload_file1(fileBuffer, fileExtName, mathList);
trackerServer.close();
return fileId;
}
@Override
public byte[] downloaderFile(String remPath) throws IOException, MyException {
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
StorageClient1 storageClient1 = new StorageClient1(trackerServer, null);
byte[] bytes = storageClient1.download_file1(remPath);
trackerServer.close();
return bytes;
}
@Override
public void fileRemove(String remPath) throws IOException, MyException {
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
StorageClient1 storageClient1 = new StorageClient1(trackerServer, null);
storageClient1.delete_file1(remPath);
trackerServer.close();
}
@Override
public void fileAppend(String filePath, byte[] fileBuffer) throws IOException, MyException {
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
StorageClient1 storageClient1 = new StorageClient1(trackerServer, null);
storageClient1.append_file1(filePath,fileBuffer);
trackerServer.close();
}
@Override
public Map<String, String> getFileMate(String filePath) throws IOException, MyException {
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
StorageClient1 storageClient1 = new StorageClient1(trackerServer, null);
NameValuePair[] metadata1 = storageClient1.get_metadata1(filePath);
Map<String,String> map = new HashMap<String,String>(5);
for (int i = 0; i <metadata1.length ; i++) {
map.put(metadata1[i].getName(),metadata1[i].getValue());
}
return map;
}
}
package com.byit.executor.filesystem;
import org.csource.common.MyException;
import java.io.IOException;
import java.util.Map;
/**
* 文件系统 所有类型的文件系统都必须继承这个文件系统接口
* @author huangfu
*/
public interface FileSystem {
/**
* 文件上传接口
* @param fileBuffer 文件的字节流
* @param fileExtName 文件的扩展名称
* @param mateDaTA 文件的元信息
* @return
*/
String uploadFile(byte[] fileBuffer, String fileExtName, Map<String,String> mateDaTA) throws IOException, MyException;
/**
* 文件下载操作
* @param remPath 远程文件地址
* @return 文件流
*/
byte[] downloaderFile(String remPath) throws IOException, MyException;
/**
* 文件删除
* @param remPath 远程地址
*/
void fileRemove(String remPath) throws IOException, MyException;
/**
* 数据追加
* @param filePath
* @param fileBuffer
*/
void fileAppend(String filePath,byte[] fileBuffer) throws IOException, MyException;
/**
* 查询文件原信息
* @param filePath
* @return
*/
Map<String,String> getFileMate(String filePath) throws IOException, MyException;
}
connect_timeout = 2
network_timeout = 30
charset = UTF-8
http.tracker_http_port = 8080
http.anti_steal_token = no
http.secret_key = FastDFS1234567890
tracker_server = 10.0.120.2:22122
connection_pool.enabled = true
connection_pool.max_count_per_entry = 500
connection_pool.max_idle_time = 3600
connection_pool.max_wait_time_in_ms = 1000
\ No newline at end of file
package com.test;
import com.byit.executor.conf.FileSystemContext;
import com.byit.executor.filesystem.FastDfsFileSystem;
import org.apache.commons.io.FileUtils;
import org.csource.common.MyException;
import org.junit.Before;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class FastDFSTest {
private FileSystemContext fileSystemContext;
@Before
public void init(){
fileSystemContext = new FileSystemContext(new FastDfsFileSystem());
}
@Test
public void testUpload() throws IOException, MyException {
byte[] bytes = FileUtils.readFileToByteArray(new File("E:\\image/20180101.log"));
Map<String,String> map = new HashMap<String,String>(10);
map.put("filename","20180101.log");
//ddmp/M00/00/00/CgB4Al5GY5GAMGVaAAAHa9wlKKc755.log
System.out.println(fileSystemContext.uploadFile(bytes, "log", map));
}
@Test
public void downLoad() throws IOException, MyException {
String filePath = "ddmp/M00/00/00/CgB4Al5GY5GAMGVaAAAHa9wlKKc755.log";
Map<String, String> fileMate = fileSystemContext.getFileMate(filePath);
String filename = fileMate.get("filename");
byte[] bytes = fileSystemContext.downloaderFile(filePath);
File file = new File("E:\\image\\down/" + filename);
if(file.exists()){
file.delete();
}
FileUtils.writeByteArrayToFile(file,bytes);
}
@Test
public void appendTest() throws IOException, MyException {
String filePath = "ddmp/M00/00/00/CgB4Al5GY5GAMGVaAAAHa9wlKKc755.log";
byte[] bytes = FileUtils.readFileToByteArray(new File("E:\\image/20180101.log"));
fileSystemContext.fileAppend(filePath,bytes);
}
}
......@@ -67,12 +67,22 @@
<guava.version>21.0</guava.version>
<springfox-swagger-ui.version>2.9.2</springfox-swagger-ui.version>
<org-apache-commons.version>1.3</org-apache-commons.version>
<fastdfs-client-java-version>1.27.0.0</fastdfs-client-java-version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>net.oschina.zcx7878</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>${fastdfs-client-java-version}</version>
</dependency>
<!-- 外部脚本执行器 -->
<dependency>
<groupId>org.apache.commons</groupId>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment