|
@@ -10,18 +10,11 @@ import java.io.*;
|
|
|
|
|
|
public class FastDFSClient {
|
|
|
private static org.slf4j.Logger logger = LoggerFactory.getLogger(FastDFSClient.class);
|
|
|
- private static TrackerClient trackerClient;
|
|
|
- private static TrackerServer trackerServer;
|
|
|
- private static StorageClient storageClient;
|
|
|
- private static StorageServer storageServer;
|
|
|
|
|
|
static {
|
|
|
try {
|
|
|
String filePath = new ClassPathResource("fdfs_client.conf").getFile().getAbsolutePath();;
|
|
|
ClientGlobal.init(filePath);
|
|
|
- trackerClient = new TrackerClient();
|
|
|
- trackerServer = trackerClient.getConnection();
|
|
|
- storageServer = trackerClient.getStoreStorage(trackerServer);
|
|
|
} catch (Exception e) {
|
|
|
logger.error("FastDFS Client Init Fail!",e);
|
|
|
}
|
|
@@ -35,8 +28,9 @@ public class FastDFSClient {
|
|
|
|
|
|
long startTime = System.currentTimeMillis();
|
|
|
String[] uploadResults = null;
|
|
|
+ StorageClient storageClient=null;
|
|
|
try {
|
|
|
- storageClient = new StorageClient(trackerServer, storageServer);
|
|
|
+ storageClient = getTrackerClient();
|
|
|
uploadResults = storageClient.upload_file(file.getContent(), file.getExt(), meta_list);
|
|
|
} catch (IOException e) {
|
|
|
logger.error("IO Exception when uploadind the file:" + file.getName(), e);
|
|
@@ -45,7 +39,7 @@ public class FastDFSClient {
|
|
|
}
|
|
|
logger.info("upload_file time used:" + (System.currentTimeMillis() - startTime) + " ms");
|
|
|
|
|
|
- if (uploadResults == null) {
|
|
|
+ if (uploadResults == null && storageClient!=null) {
|
|
|
logger.error("upload file fail, error code:" + storageClient.getErrorCode());
|
|
|
}
|
|
|
String groupName = uploadResults[0];
|
|
@@ -57,7 +51,7 @@ public class FastDFSClient {
|
|
|
|
|
|
public static FileInfo getFile(String groupName, String remoteFileName) {
|
|
|
try {
|
|
|
- storageClient = new StorageClient(trackerServer, storageServer);
|
|
|
+ StorageClient storageClient = getTrackerClient();
|
|
|
return storageClient.get_file_info(groupName, remoteFileName);
|
|
|
} catch (IOException e) {
|
|
|
logger.error("IO Exception: Get File from Fast DFS failed", e);
|
|
@@ -69,7 +63,7 @@ public class FastDFSClient {
|
|
|
|
|
|
public static InputStream downFile(String groupName, String remoteFileName) {
|
|
|
try {
|
|
|
- storageClient = new StorageClient(trackerServer, storageServer);
|
|
|
+ StorageClient storageClient = getTrackerClient();
|
|
|
byte[] fileByte = storageClient.download_file(groupName, remoteFileName);
|
|
|
InputStream ins = new ByteArrayInputStream(fileByte);
|
|
|
return ins;
|
|
@@ -83,22 +77,38 @@ public class FastDFSClient {
|
|
|
|
|
|
public static void deleteFile(String groupName, String remoteFileName)
|
|
|
throws Exception {
|
|
|
- storageClient = new StorageClient(trackerServer, storageServer);
|
|
|
+ StorageClient storageClient = getTrackerClient();
|
|
|
int i = storageClient.delete_file(groupName, remoteFileName);
|
|
|
logger.info("delete file successfully!!!" + i);
|
|
|
}
|
|
|
|
|
|
public static StorageServer[] getStoreStorages(String groupName)
|
|
|
throws IOException {
|
|
|
+ TrackerClient trackerClient = new TrackerClient();
|
|
|
+ TrackerServer trackerServer = trackerClient.getConnection();
|
|
|
return trackerClient.getStoreStorages(trackerServer, groupName);
|
|
|
}
|
|
|
|
|
|
public static ServerInfo[] getFetchStorages(String groupName,
|
|
|
String remoteFileName) throws IOException {
|
|
|
+ TrackerClient trackerClient = new TrackerClient();
|
|
|
+ TrackerServer trackerServer = trackerClient.getConnection();
|
|
|
return trackerClient.getFetchStorages(trackerServer, groupName, remoteFileName);
|
|
|
}
|
|
|
|
|
|
- public static String getTrackerUrl() {
|
|
|
- return "http://"+trackerServer.getInetSocketAddress().getHostString()+":"+ClientGlobal.getG_tracker_http_port()+"/";
|
|
|
+ public static String getTrackerUrl() throws IOException {
|
|
|
+ return "http://"+getTrackerServer().getInetSocketAddress().getHostString()+":"+ClientGlobal.getG_tracker_http_port()+"/";
|
|
|
+ }
|
|
|
+
|
|
|
+ private static StorageClient getTrackerClient() throws IOException {
|
|
|
+ TrackerServer trackerServer = getTrackerServer();
|
|
|
+ StorageClient storageClient = new StorageClient(trackerServer, null);
|
|
|
+ return storageClient;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static TrackerServer getTrackerServer() throws IOException {
|
|
|
+ TrackerClient trackerClient = new TrackerClient();
|
|
|
+ TrackerServer trackerServer = trackerClient.getConnection();
|
|
|
+ return trackerServer;
|
|
|
}
|
|
|
}
|