简介:FastDFS(Fast Distributed File System)是一款开源轻量级分布式文件系统,本文不讲解原理和架构,只是在个人使用部署过程中耗费了好长时间和精力,遇到了很多的坑,于是总结成了一篇详细的部署文档分享给大家。
一、步骤如下:
1、下载fastdfs-client-java工具jar包:
下载地址:https://github.com/happyfish100/fastdfs-client-java
2、导入程序到eclipse并编译生成:fastdfs-client-java-1.29-SNAPSHOT.jar
3、新建maven项目fastdfs_demo
4、项目根目录下创建libs目录,将fastdfs-client-java-1.29-SNAPSHOT.jar复制进去;
5、在pom的中添加:
和如下:
如上是为了导入依赖的jar文件和将外部的libs下的jar文件在执行maven打包的时候把system为system的打进去。
6、创建FastDFSClientUtils.java类:
package com.exception500.fastdfs_demo;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerGroup;
import org.csource.fastdfs.TrackerServer;
/**
* FastDFS客户端工具类
* @author Exception500
*
*/
public class FastDFSClientUtils {
/**配置文件**/
private static final String CONF_FILENAME = "/Users/apple/sts/fastdfs_demo/config/fdfs_client.conf";
private static Logger logger = Logger.getLogger(FastDFSClientUtils.class);
private static TrackerClient trackerClient;
/**加载文件**/
static {
try {
ClientGlobal.init(CONF_FILENAME);
TrackerGroup trackerGroup = ClientGlobal.g_tracker_group;
trackerClient = new TrackerClient(trackerGroup);
} catch (Exception e) {
logger.error(e);
}
}
/**
* 文件上传
* @param file 文件
* @param path 路径
* @return 上传成功返回id,失败返回null
*/
public static String upload(File file, String path) {
TrackerServer trackerServer = null;
StorageServer storageServer = null;
StorageClient1 storageClient1 = null;
FileInputStream fis = null;
try {
NameValuePair[] meta_list = null;
fis = new FileInputStream(file);
byte[] file_buff = null;
if (fis != null) {
int len = fis.available();
file_buff = new byte[len];
fis.read(file_buff);
}
trackerServer = trackerClient.getTrackerServer();
if (trackerServer == null) {
logger.error("getConnection return null");
}
storageServer = trackerClient.getStoreStorage(trackerServer);
storageClient1 = new StorageClient1(trackerServer, storageServer);
String fileid = storageClient1.upload_file1(file_buff, getFileExt(path), meta_list);
return fileid;
} catch (Exception ex) {
logger.error(ex);
return null;
}finally{
if (fis != null){
try {
fis.close();
} catch (IOException e) {
logger.error(e);
}
}
if (storageServer != null){
try {
storageServer.getConnection().close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (trackerServer != null){
try {
trackerServer.getConnection().close();
} catch (Exception e) {
e.printStackTrace();
}
}
storageClient1 = null;
}
}
/**
* 文件上传
* @param data 数据
* @param path 路径
* @return 上传成功返回id,失败返回null
*/
public static String upload(byte[] data, String extName) {
TrackerServer trackerServer = null;
StorageServer storageServer = null;
StorageClient1 storageClient1 = null;
try {
NameValuePair[] meta_list = null;
trackerServer = trackerClient.getTrackerServer();
if (trackerServer == null) {
logger.error("getConnection return null");
}
storageServer = trackerClient.getStoreStorage(trackerServer);
storageClient1 = new StorageClient1(trackerServer, storageServer);
String fileid = storageClient1.upload_file1(data, extName, meta_list);
return fileid;
} catch (Exception ex) {
logger.error(ex);
return null;
}finally{
if (storageServer != null){
try {
storageServer.getConnection().close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (trackerServer != null){
try {
trackerServer.getConnection().close();
} catch (Exception e) {
e.printStackTrace();
}
}
storageClient1 = null;
}
}
/**
* 文件下载
* 说明:通过文件id进行下载
* @param fileId 文件id
* @return 返回InputStream
*/
public static InputStream download(String groupName, String fileId) {
TrackerServer trackerServer = null;
StorageServer storageServer = null;
StorageClient1 storageClient1 = null;
try {
trackerServer = trackerClient.getTrackerServer();
if (trackerServer == null) {
logger.error("getConnection return null");
}
storageServer = trackerClient.getStoreStorage(trackerServer, groupName);
storageClient1 = new StorageClient1(trackerServer, storageServer);
byte[] bytes = storageClient1.download_file1(fileId);
InputStream inputStream = new ByteArrayInputStream(bytes);
return inputStream;
} catch (Exception ex) {
logger.error(ex);
return null;
} finally {
if (storageServer != null){
try {
storageServer.getConnection().close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (trackerServer != null){
try {
trackerServer.getConnection().close();
} catch (Exception e) {
e.printStackTrace();
}
}
storageClient1 = null;
}
}
/**
* 文件删除
* 说明:根据id来删除一个文件
* @param fileId 文件id
* @return 删除成功返回0,非0则操作失败,返回错误代码
*/
public static int delete(String groupName, String fileId) {
TrackerServer trackerServer = null;
StorageServer storageServer = null;
StorageClient1 storageClient1 = null;
try {
trackerServer = trackerClient.getTrackerServer();
if (trackerServer == null) {
logger.error("getConnection return null");
}
storageServer = trackerClient.getStoreStorage(trackerServer, groupName);
storageClient1 = new StorageClient1(trackerServer, storageServer);
int result = storageClient1.delete_file1(fileId);
return result;
} catch (Exception ex) {
logger.error(ex);
return 0;
} finally {
if (storageServer != null){
try {
storageServer.getConnection().close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (trackerServer != null){
try {
trackerServer.getConnection().close();
} catch (Exception e) {
e.printStackTrace();
}
}
storageClient1 = null;
}
}
/**
* 文件修改
* @param oldFileId 旧文件id
* @param file 新文件
* @param path 新文件路径
* @return 上传成功返回id,失败返回null
*/
public static String modify(String oldGroupName, String oldFileId, File file, String path) {
String fileid = null;
try {
/**先上传**/
fileid = upload(file, path);
if (fileid == null) {
return null;
}
/**再删除**/
int delResult = delete(oldGroupName, oldFileId);
if (delResult != 0) {
return null;
}
} catch (Exception ex) {
logger.error(ex);
return null;
}
return fileid;
}
/**
* 获取文件后缀名
* @param fileName
* @return 如:"jpg"、"txt"、"zip" 等
*/
private static String getFileExt(String fileName) {
if (StringUtils.isBlank(fileName) || !fileName.contains(".")) {
return "";
} else {
return fileName.substring(fileName.lastIndexOf(".") + 1);
}
}
}
7、创建FastDFSTest.java类:
package com.exception500.fastdfs_demo;
import java.io.File;
import java.io.InputStream;
/**
* 文件管理
* @author Exception500
*
*/
public class FastDFSTest {
/**
* 上传
*/
public static void upload() throws Exception {
String path = "/Users/apple/Desktop/11.png";
File file = new File(path);
String fileId = FastDFSClientUtils.upload(file, path);
System.out.println("本地文件:" + path + ",上传成功! 文件ID为:" + fileId);
}
/**
* 下载
*/
public static void download() throws Exception {
String fileId = "group1/M00/00/00/wKgAaV5Ju_GAZTB8AABjkxQx4S4722.png";
InputStream inputStream = FastDFSClientUtils.download("group1", fileId);
System.out.println(inputStream.available());
}
/**
* 删除
*/
public static void delete() throws Exception {
String fileId = "group1/M00/00/00/wKgAaV5Ju_GAZTB8AABjkxQx4S4722.png";
int result = FastDFSClientUtils.delete("group1", fileId);
System.out.println(result == 0 ? "删除成功" : "删除失败:" + result);
}
/**
* 测试
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
//upload();
//download();
//delete();
}
}
8、测试上传,现在桌面上准备一张11.png图片:
上传:
下载:
删除测试:
9、代码下载:
附:CentOS6.10上FastDFS+Nginx单机版的安装配置及测试教程(https://www.exception500.com/softwareinstall/32.html)