FastDFS 简单使用教程
安装
你问我怎么装,那我就只有问此软件开发者了,要不你也去问他,链接在这https://github.com/happyfish100/fastdfs/wiki
安装遇到的问题
首先声明,这些问题你不一定会遇到,遇到了也不一定能用我这里提供的方法解决,条条大路通罗马,看缘分了!
1、不要用git下载,自己老老实实下载压缩包,然后在服务器上解压(你问我为什么,因为亲测前者失败,后者成功)
2、修改 fastdfs-nginx-module/src/config,参照这个链接 https://blog.youkuaiyun.com/zzzgd_666/article/details/81911892
3、编译 nginx 的时候,如果报错说 xxx.h 文件在 fastdfs 目录下找不到,你懂得,从 fastcommon 中复制过去,虽然这是我突发奇想用到的土方法,但是真的有效哦
项目引入
<dependency>
<groupId>fastdfs</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27</version>
</dependency>
配置文件 fastdfs-client.properties
## fastdfs-client.properties
fastdfs.connect_timeout_in_seconds=5
fastdfs.network_timeout_in_seconds=30
fastdfs.charset=UTF-8
fastdfs.http_anti_steal_token=false
fastdfs.http_secret_key=FastDFS1234567890 #默认密码
fastdfs.http_tracker_http_port=【tracker端口】
fastdfs.tracker_servers=10.0.11.201:22121,10.0.11.202:22122,10.0.11.203:22123
文件上传服务类 FDFSService
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
/**
* created by 程序员阿哲 2018/10/25
*/
@Service
public class FDFSService {
//以下配置都用默认的就可以了,需要的话也可以单独配
@Value("${fast.config.file.name:fastdfs-client.properties}")
private String fastConfName;
@Value("${nginx.ip:10.0.0.11:10086}")
private String nginxIp;
private boolean isInit = false;
private void init() {
try {
ClientGlobal.initByProperties(fastConfName);
isInit = true;
} catch (Exception e) {
}
}
/*
saveFile方法返回的是文件的绝对路径
*/
public String saveFile(File file, String dir) throws Exception {
if (file == null || file.length() < 1) {
return null;
}
return saveFile(new FileInputStream(file), dir, getFileExtension(file.getName()));
}
public String saveFile(MultipartFile file, String dir) throws Exception {
if (file == null || file.getSize() < 1) {
return null;
}
return saveFile(file.getInputStream(), dir, getFileExtension(file.getOriginalFilename()));
}
public boolean deleteFile(String fileUrl) {
TrackerServer trackerServer = null;
try {
if (fileUrl == null || fileUrl.length() < nginxIp.length() + 16) {
return false;
}
String filePathName = fileUrl.substring(fileUrl.indexOf(nginxIp + "/") + (nginxIp + "/").length());
String groupName = filePathName.substring(0, filePathName.indexOf("/"));
String remoteFileName = filePathName.substring(filePathName.indexOf("/") + 1);
trackerServer = getTrackerServer();
StorageClient storageClient = new StorageClient(trackerServer,
null);
int resultCode = storageClient.delete_file(groupName, remoteFileName);
if (resultCode == 0) {
return true;
}
} catch (Exception e) {
LOGGER.error("删除文件【" + fileUrl + "】失败:" + e.getMessage());
} finally {
closeTrackerServer(trackerServer);
}
return false;
}
public String saveFile(InputStream is, String dir, String fileExtension) throws Exception {
TrackerServer trackerServer = null;
try {
byte fileBuf[] = null;
if (is != null) {
fileBuf = new byte[is.available()];
is.read(fileBuf);
}
trackerServer = getTrackerServer();
StorageClient storageClient = new StorageClient(trackerServer, null);
String[] fileIds = storageClient.upload_file(fileBuf, fileExtension, new NameValuePair[]{new NameValuePair("type", dir)});
if (fileIds.length == 2) {
return "http://" + nginxIp + "/" + fileIds[0] + "/" + fileIds[1];
}
} catch (Exception e) {
} finally {
closeTrackerServer(trackerServer);
}
return null;
}
private String getFileExtension(String fileName) {
String extension = fileName.substring(fileName.lastIndexOf(".") + 1);
return extension;
}
private TrackerServer getTrackerServer() throws Exception {
if (!isInit) {
init();
}
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
return trackerServer;
}
private void closeTrackerServer(TrackerServer trackerServer) {
if (trackerServer != null) {
try {
trackerServer.close();
} catch (Exception e) {
}
}
}
public String getFileType(String fileName) {
String extension = fileName.substring(fileName.lastIndexOf(".") + 1);
return extension;
}
}