我这里以libfastcommon-1.0.57、fastdfs-6.08、nginx-1.20.2、fastdfs-nginx-module-1.22为例进行搭建
《官网下载libfastcommon-1.0.57.tar.gz》
《官网下载fastdfs-6.08.tar.gz》
《官网下载nginx-1.20.2.tar.gz》
《官网下载fastdfs-nginx-module-1.22.tar.gz》
#文件存放根目录 mkdir -p /fastdfs_log #client.conf mkdir -p /fastdfs_log/fastdfs/client #storage.conf、mod_fastdfs.conf mkdir -p /fastdfs_log/fastdfs/file #storage.conf mkdir -p /fastdfs_log/fastdfs/storage #tracker.conf mkdir -p /fastdfs_log/fastdfs/tracker安装依赖
gcc
yum install gcc-c++
PCRE pcre-devel
yum install -y pcre pcre-devel
zlib
yum install -y zlib zlib-devel
OpenSSL
yum install -y openssl openssl-devel
把他们上传到linx服务器的 /usr/local/ 目录下进行解压
cd /usr/local
分别进行tar解压
tar -zxvf libfastcommon-1.0.57.tar.gz tar -zxvf fastdfs-6.08.tar.gz tar -zxvf nginx-1.20.2.tar.gz tar -zxvf fastdfs-nginx-module-1.22.tar.gz安装libfastcommon-1.0.57
cd /usr/local/libfastcommon-1.0.57
./make.sh ./make.sh install
软链接
ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so ln -s /usr/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.so ln -s /usr/lib64/libfdfsclient.so /usr/lib/libfdfsclient.so安装fastdfs-6.08
cd /usr/local/fastdfs-6.08
./make.sh ./make.sh install
软链接
ln -s /usr/bin/fdfs_trackerd /usr/local/bin ln -s /usr/bin/fdfs_storaged /usr/local/bin ln -s /usr/bin/stop.sh /usr/local/bin ln -s /usr/bin/restart.sh /usr/local/bin
复制 FastDFS安装包下conf文件夹所有文件到/etc/fdfs文件夹下
cp /opt/fastdfs-6.08/conf
@Component
public class FastDFSUtils {
private static final Logger log = LoggerFactory.getLogger(FastDFSUtils.class);
@Resource
private FastFileStorageClient fastFileStorageClient;
@Resource
private FdfsWebServer fdfsWebServer;
public String uploadFile(MultipartFile file) throws IOException {
StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()), null);
String fullPath = storePath.getFullPath();
// return getResAccessUrl(fullPath);
return fullPath;
}
public String uploadFile(File file) {
try {
FileInputStream inputStream = new FileInputStream(file);
StorePath storePath = fastFileStorageClient.uploadFile(inputStream, file.length(), FilenameUtils.getExtension(file.getName()), null);
return storePath.getFullPath();
} catch (Exception e) {
log.error("上传文件失败!", e);
return null;
}
}
public byte[] downloadFile(String filePath) throws IOException{
StorePath storePath = StorePath.parseFromUrl(filePath);
byte[] bytes = fastFileStorageClient.downloadFile(storePath.getGroup(), storePath.getPath(), new DownloadByteArray());
return bytes;
}
public Boolean deleteFile(String filePath) {
if (StringUtils.isEmpty(filePath)) {
return false;
}
try {
StorePath storePath = StorePath.parseFromUrl(filePath);
fastFileStorageClient.deleteFile(storePath.getGroup(), storePath.getPath());
} catch (Exception e) {
log.error("上传文件失败!", e);
return false;
}
return true;
}
public String getResAccessUrl(String path) {
String url = fdfsWebServer.getWebServerUrl() + path;
log.debug("上传文件地址为:n" + url);
return url;
}
}
FastDFSController.java测试
import com.fu.demo.util.FastDFSUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.IOException;
@RestController
public class FastDFSController {
@Resource
private FastDFSUtils fastDFSUtils;
@PostMapping("upload")
public String upload(@RequestParam(name = "file") MultipartFile file) throws IOException {
return fastDFSUtils.uploadFile(file);
}
}
访问:http://192.168.18.111/group1/M00/00/00/wKgSb2KUmlmAbZJcAACB5haWMTc195.png



