生成FastDFS Java Client API
进入https://github.com/happyfish100/fastdfs-client-java,下载文件。可以看到,有很多种生成jar文件的方式,有ant、maven等。上面两种生成jar包的方式在README里面写得很清楚,在这里使用eclipse生成相应jar文件。
将下载的文件解压,导入到eclipse中。在 fastdfs-client-java-master 项目上点击右键,export,选择JAR file,Next,如图所示。
接下来,只保留 src/main/java 下的内容。如果想让jar包有源码和注释,勾选图片中 Export Java source files and resources 选项,这样在敲代码的时候,看着注释写会舒服一点。
最后,点击Finish,遇到warning点击确定即可。
导入Jar包
在这里使用Maven管理项目。首先将jar包复制到 src/main/resources/lib 目录下(可以修改),然后在pom.xml中加入下面的代码:
<dependency>
<groupId>org.csource</groupId>
<artifactId>fastdfs-client-java</artifactId>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/fastdfs.jar</systemPath>
<version>1.27-SNAPSHOT</version>
</dependency>
Java操作FastDFS
在src/main/resources目录下创建文件client.conf,添加以下内容:
tracker_server=10.10.140.3:22122
IP和端口根据实际的情况修改。该配置文件的其他选项,参照https://github.com/happyfish100/fastdfs-client-java配置。
public class FastDFSHelper {
private static final Logger logger = Logger.getLogger(FastDFSHelper.class);
static {
try {
ClientGlobal.init("src/main/resources/client.conf");
} catch (IOException | MyException e) {
e.printStackTrace();
}
}
/**
* 向FastDFS上传文件
*
* @param localFilename 本地文件名
* @return 上传成功,返回组名和该文件在FastDFS中的名称;上传失败,返回null
*/
public void uploadFile(String localFilename) {
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = null;
try {
trackerServer = trackerClient.getConnection();
} catch (IOException e) {
logger.error(e);
e.printStackTrace();
}
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
try {
String[] arr = storageClient.upload_file(localFilename, null, null);
if (arr == null || arr.length != 2) {
logger.error("向FastDFS上传文件失败");
} else {
logger.info("向FastDFS上传文件成功");
}
} catch (IOException | MyException e) {
logger.error(e);
e.printStackTrace();
} finally {
try {
if (storageServer != null)
storageServer.close();
if (trackerServer != null)
trackerServer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 从FastDFS下载文件
*
* @param localFilename 本地文件名
* @param groupName 文件在FastDFS中的组名
* @param remoteFilename 文件在FastDFS中的名称
*
*/
public void downloadFile(String localFilename, String groupName, String remoteFilename) {
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = null;
try {
trackerServer = trackerClient.getConnection();
} catch (IOException e) {
logger.error(e);
e.printStackTrace();
}
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(localFilename))) {
byte[] content = storageClient.download_file(groupName, remoteFilename);
if (content == null || content.length == 0) {
logger.error("文件大小为空!");
return;
}
bos.write(content);
logger.info("成功下载文件: " + localFilename);
} catch (IOException | MyException e) {
logger.error(e);
e.printStackTrace();
} finally {
try {
if (storageServer != null)
storageServer.close();
if (trackerServer != null)
trackerServer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 从FastDFS删除文件
*
* @param localFilename 本地文件名
* @param groupName 文件在FastDFS中的组名
* @param remoteFilename 文件在FastDFS中的名称
*
*/
public void deleteFile(String localFilename, String groupName, String remoteFilename) {
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = null;
try {
trackerServer = trackerClient.getConnection();
} catch (IOException e) {
logger.error(e);
e.printStackTrace();
}
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
try {
int i = storageClient.delete_file(groupName, remoteFilename);
if (i == 0) {
logger.info("FastDFS删除文件成功");
} else {
logger.info("FastDFS删除文件失败");
}
} catch (IOException | MyException e) {
logger.error(e);
e.printStackTrace();
} finally {
try {
if (storageServer != null)
storageServer.close();
if (trackerServer != null)
trackerServer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
在实际应用中,应该记录下本地文件名和FastDFS中组名与文件名称的映射关系,在此省略。