FastDFS 的maven依赖
<dependency>
<groupId>net.oschina.zcx7878</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27.0.0</version>
</dependency>
FastDFS 的jar包下载
链接:https://pan.baidu.com/s/1CbePpVRhL0LyZ5pQiDCxyA?pwd=mrlm
提取码:mrlm
FastDFS附件上传下载工具类
public class FastDFSUtil {
private static final String CONF_FILENAME = Thread.currentThread().getContextClassLoader().getResource("").getPath() + "fastdfs.conf";
private static StorageClient storageClient = null;
/**
* 只加载一次.
*/
static {
try {
ClientGlobal.init(CONF_FILENAME);
TrackerClient trackerClient = new TrackerClient(ClientGlobal.g_tracker_group);
TrackerServer trackerServer = trackerClient.getConnection();
StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
storageClient = new StorageClient(trackerServer, storageServer);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 文件上传
* @return
*/
public String uploadFile(byte[] buffFile,String fileExtname) {
TrackerServer ts = null;
StorageServer ss = null;
String retPath = null;
try {
/**
* 参数1:为需要上传的文件字节数组
* 参数2:为需要上传的文件扩展名
* 参数3:为文件的属性文件通常不上传
* 返回值:数组中第一个元素为文件所在组名
* 数组中第二个元素为文件所在远程路径名
*/
String[] result = storageClient.upload_file(buffFile, fileExtname, null);
retPath = result[0]+"/"+result[1];
} catch (Exception e) {
e.printStackTrace();
}
return retPath;
}
/**
* 文件下载
* @return
*/
public InputStream downloadFile(String groupName, String remoteFileName) {
try {
byte[] bytes = storageClient.download_file(groupName, remoteFileName);
InputStream inputStream = new ByteArrayInputStream(bytes);
return inputStream;
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
/**
* 文件删除
* @return
*/
public int deleteFile(String groupName, String remoteFileName) {
try {
int result = storageClient.delete_file(groupName == null ? "group1" : groupName, remoteFileName);
return result;
} catch (Exception ex) {
return 0;
}
}
}
FastDFS配置文件 (fastdfs.conf)
#连接tracker服务器超时时长
connect_timeout = 2
#socket连接超时时长
network_timeout = 30
#文件内容编码
charset = UTF-8
#22122是跟踪器地址 7777是nginx反向代理下载地址
#tracker服务器IP和端口(可以写多个逗号隔开)
#自行配置
tracker_server = 192.168.5.22:22122
网友评论