public class FastDFSClient {
/** 日志 */
private final static Logger log = LoggerFactory.getLogger(FastDFSClient.class);
/** 文件配置 */
private static final String CONFIG_FILENAME = System.getProperty("webapp.root") + File.separator + "WEB-INF" + File.separator
+ "fdfs_client.conf";
/** storageClient1 */
private static StorageClient1 storageClient1 = null;
// 初始化FastDFS Client
static {
try {
ClientGlobal.init(CONFIG_FILENAME);
TrackerClient trackerClient = new TrackerClient(ClientGlobal.g_tracker_group);
TrackerServer trackerServer = trackerClient.getConnection();
if (trackerServer == null) {
throw new IllegalStateException("getConnection return null");
}
StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
if (storageServer == null) {
throw new IllegalStateException("getStoreStorage return null");
}
storageClient1 = new StorageClient1(trackerServer, storageServer);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 上传文件
*
* @param file 文件对象
* @param fileName 文件名
* @return fileId 文件ID
*/
public static String uploadFile(File file, String fileName) {
return uploadFile(file, fileName, null);
}
/**
* 上传文件
*
* @param file 文件对象
* @param fileName 文件名
* @param metaList 文件元数据
* @return fileId 文件ID
*/
public static String uploadFile(File file, String fileName, Map<String, String> metaList) {
try {
byte[] buff = IOUtil.toByteArray(new FileInputStream(file));
NameValuePair[] nameValuePairs = null;
if (metaList != null) {
nameValuePairs = new NameValuePair[metaList.size()];
int index = 0;
for (Iterator<Map.Entry<String, String>> iterator = metaList.entrySet().iterator(); iterator.hasNext();) {
Map.Entry<String, String> entry = iterator.next();
String name = entry.getKey();
String value = entry.getValue();
nameValuePairs[index++] = new NameValuePair(name, value);
}
}
return storageClient1.upload_file1(buff, fileName, nameValuePairs);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 上传文件
*
* @param fileStream 文件输入流
* @param fileName 文件名
* @return fileId 文件ID
*/
public static String uploadFile(String fileStream, String fileName) {
return uploadFile(fileStream, fileName, null);
}
/**
* 上传文件
*
* @param fileStream 文件输入流
* @param fileName 文件名
* @param metaList 文件元数据
* @return fileId 文件ID
*/
public static String uploadFile(String fileStream, String fileName, Map<String, String> metaList) {
try {
byte[] buff = Base64.decodeBase64(fileStream.getBytes());
NameValuePair[] nameValuePairs = null;
if (metaList != null) {
nameValuePairs = new NameValuePair[metaList.size()];
int index = 0;
for (Iterator<Map.Entry<String, String>> iterator = metaList.entrySet().iterator(); iterator.hasNext();) {
Map.Entry<String, String> entry = iterator.next();
String name = entry.getKey();
String value = entry.getValue();
nameValuePairs[index++] = new NameValuePair(name, value);
}
}
String extNameStr = "";
if (StringUtil.isNotEmpty(fileName)) {
extNameStr = fileName.substring(fileName.indexOf(".") + 1);
}
return storageClient1.upload_file1(buff, extNameStr, nameValuePairs);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 获取文件元数据
*
* @param fileId 文件ID
* @return 元数据
*/
public static Map<String, String> getFileMetadata(String fileId) {
try {
NameValuePair[] metaList = storageClient1.get_metadata1(fileId);
if (metaList != null) {
HashMap<String, String> map = new HashMap<String, String>();
for (NameValuePair metaItem : metaList) {
map.put(metaItem.getName(), metaItem.getValue());
}
return map;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 删除文件
*
* @param fileId 文件ID
* @return 删除失败返回-1,否则返回0
*/
public static int deleteFile(String fileId) {
try {
if (fileId != null && !"".equals(fileId)) {
return storageClient1.delete_file1(fileId);
}
} catch (Exception e) {
e.printStackTrace();
}
return -1;
}
/**
* 下载文件
*
* @param fileId 文件ID(上传文件成功后返回的ID)
* @param outFile 文件下载保存位置
* @return 成功标志
*/
public static int downloadFile(String fileId, File outFile) {
FileOutputStream fos = null;
try {
byte[] content = storageClient1.download_file1(fileId);
fos = new FileOutputStream(outFile);
IOUtil.copy(content, fos);
return 0;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return -1;
}
/**
* 下载文件
*
* @param fileId 文件ID(上传文件成功后返回的ID)
* @return fileString 文件字符流
*/
public static String downloadFile(String fileId) {
log.info("开始下载fastdfs上的文件fileId:["+fileId+"]");
String fileString = "";
try {
byte[] content = storageClient1.download_file1(fileId);
// log.info("下载附件成功,附件byte流:\n"+content);
// 生成文件字符流
byte[] bytes = Base64.encodeBase64(content);
fileString = new String(bytes);
// log.info("下载附件成功,encodeBase64后的附件String流:\n"+fileString);
} catch (Exception e) {
log.error("fastdfs下载文件报错:\n"+e.getMessage());
e.printStackTrace();
}
return fileString;
}
}
FastDFS文件上传下载工具类
于 2020-05-30 09:32:39 首次发布