자바 fastfds 작업 파일
connect_timeout = 2
network_timeout = 30
charset = UTF-8
http.tracker_http_port = 8080
http.anti_steal_token = no
tracker_server = 192.168.42.29:22122
package com.ctl.utils;
import com.ctl.utils.json.JsonDateValueProcessor;
import com.ctl.utils.json.JsonNumberValueProcessor;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.apache.commons.io.IOUtils;
import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
/**
* Title: FastFdsUtil
* Description:
* Copyright: Copyright (c) 2018
* Company: www.ctl.com
* @author guolin
* @version 1.0
* @date 2018-08-14 21:04
*/
public class FastFdsUtil {
static Logger logger = LoggerFactory.getLogger(FastFdsUtil.class);
static JsonConfig jsonConfig = new JsonConfig();
static {
try {
String confPath = FastFdsUtil.class.getClassLoader().getResource("fdfs_client.conf").getPath();
ClientGlobal.init(confPath);
Map map = new HashMap<>();
jsonConfig.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor());
jsonConfig.registerJsonValueProcessor(Timestamp.class, new JsonDateValueProcessor());
jsonConfig.registerJsonValueProcessor(Integer.class, new JsonNumberValueProcessor());
jsonConfig.registerJsonValueProcessor(Long.class, new JsonNumberValueProcessor());
jsonConfig.registerJsonValueProcessor(Byte.class, new JsonNumberValueProcessor());
jsonConfig.registerJsonValueProcessor(Float.class, new JsonNumberValueProcessor());
jsonConfig.registerJsonValueProcessor(Double.class, new JsonNumberValueProcessor());
} catch (IOException e) {
logger.error("FastFds ", e);
} catch (MyException e) {
logger.error("FastFds ", e);
}
}
/**
*
* @param localFilePath
* @return
*/
public String[] fileLocalUpload(String localFilePath) {
return fileLocalUpload(localFilePath, null);
}
/**
*
* @param localFilePath
* @param nvp NameValuePair
* @return
*/
public String[] fileLocalUpload(String localFilePath, NameValuePair[] nvp) {
try {
TrackerClient tracker = new TrackerClient();
TrackerServer trackerServer = tracker.getConnection();
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
String fileIds[] = storageClient.upload_file(localFilePath, "png", nvp);
logger.info("fileIds.length={}", fileIds.length);
logger.info(" ={}", fileIds[0]);
logger.info(" ={} ", fileIds[1]);
return fileIds;
} catch (FileNotFoundException e) {
logger.error("FastFds ", e);
} catch (IOException e) {
logger.error("FastFds ", e);
} catch (MyException e) {
logger.error("FastFds ", e);
}
return null;
}
/**
*
* @param remoteFilename : M00/00/00/wKgqHVty2ZCAHaBvAAE0vHMtwgw608.png
* @return
*/
public boolean fileServerDownload(String remoteFilename) {
return fileServerDownload("group1", remoteFilename, System.getProperty("java.io.tmpdir") + UUID.randomUUID().toString() + ".tmp");
}
/**
*
* @param remoteFilename : M00/00/00/wKgqHVty2ZCAHaBvAAE0vHMtwgw608.png
* @param localDownloadPath
* @return
*/
public boolean fileServerDownload(String remoteFilename, String localDownloadPath) {
return fileServerDownload("group1", remoteFilename, localDownloadPath);
}
/**
*
* @param groupName
* @param remoteFilename : M00/00/00/wKgqHVty2ZCAHaBvAAE0vHMtwgw608.png
* @param localDownloadPath
* @return
*/
public boolean fileServerDownload(String groupName, String remoteFilename, String localDownloadPath) {
try {
TrackerClient tracker = new TrackerClient();
TrackerServer trackerServer = tracker.getConnection();
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
byte[] b = storageClient.download_file(groupName, remoteFilename);
IOUtils.write(b, new FileOutputStream(localDownloadPath));
return true;
} catch (Exception e) {
logger.error("FastFds ", e);
return false;
}
}
/**
*
* @param remoteFilename : M00/00/00/wKgqHVty2ZCAHaBvAAE0vHMtwgw608.png
* @return
*/
public FileInfo getFileInfo(String remoteFilename) {
return getFileInfo("group1", remoteFilename);
}
/**
*
* @param groupName
* @param remoteFilename : M00/00/00/wKgqHVty2ZCAHaBvAAE0vHMtwgw608.png
* @return
*/
public FileInfo getFileInfo(String groupName, String remoteFilename) {
try {
TrackerClient tracker = new TrackerClient();
TrackerServer trackerServer = tracker.getConnection();
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
FileInfo fi = storageClient.get_file_info(groupName, remoteFilename);
System.out.println(fi.getSourceIpAddr());
System.out.println(fi.getFileSize());
System.out.println(fi.getCreateTimestamp());
System.out.println(fi.getCrc32());
return fi;
} catch (Exception e) {
logger.error("FastFds ", e);
return null;
}
}
public NameValuePair[] getFileMate(String remoteFilename) {
return getFileMate("group1", remoteFilename);
}
public NameValuePair[] getFileMate(String groupName, String remoteFilename) {
try {
TrackerClient tracker = new TrackerClient();
TrackerServer trackerServer = tracker.getConnection();
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
NameValuePair nvps[] = storageClient.get_metadata(groupName, remoteFilename);
for (NameValuePair nvp : nvps) {
System.out.println(nvp.getName() + ":" + nvp.getValue());
}
return nvps;
} catch (Exception e) {
logger.error("FastFds ", e);
return null;
}
}
/**
*
* @param remoteFilename : M00/00/00/wKgqHVty2ZCAHaBvAAE0vHMtwgw608.png
* @return
*/
public boolean delete(String remoteFilename) {
return delete("group1", remoteFilename);
}
/**
*
* @param groupName
* @param remoteFilename : M00/00/00/wKgqHVty2ZCAHaBvAAE0vHMtwgw608.png
* @return
*/
public boolean delete(String groupName, String remoteFilename) {
try {
TrackerClient tracker = new TrackerClient();
TrackerServer trackerServer = tracker.getConnection();
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
int i = storageClient.delete_file("group1", remoteFilename);
logger.info(i == 0 ? " " : " :" + i);
return i == 0 ? true : false;
} catch (Exception e) {
logger.error("FastFds ", e);
return false;
}
}
public static void main(String[] args) {
FastFdsUtil fastFdsTest = new FastFdsUtil();
String[] strings = fastFdsTest.fileLocalUpload("E:\\fAPP\\_20180814101019.png");
logger.info(JSONArray.fromObject(strings,jsonConfig).toString());
logger.info(JSONObject.fromObject(fastFdsTest.getFileInfo(strings[1]),jsonConfig).toString());
logger.info("downresult={}", fastFdsTest.fileServerDownload("group1", strings[1], "e:\\"+UUID.randomUUID()+"test.png"));
logger.info("delresult={}", fastFdsTest.delete( strings[1]));
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.