栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

linux 安装fastDFS springboot文件上传

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

linux 安装fastDFS springboot文件上传

我这里以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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/937198.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号