SpringBoot 整合 FastDFS

1. 创建SpringBoot 项目

2. 导入FastDFS Java客户端依赖

<!-- fastdfs -->
<dependency>
    <groupId>net.oschina.zcx7878</groupId>
    <artifactId>fastdfs-client-java</artifactId>
    <version>1.27.0.0</version>
</dependency>

3. 配置 FastDFS 

3.1 在 application.properties 文件中配置如下:

损失函数

#fastdfs 配置
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=80
fastdfs.tracker_servers=192.168.31.225:22122
fastdfs.connection_pool.enabled=true
fastdfs.connection_pool.max_count_per_entry=500
fastdfs.connection_pool.max_idle_time=3600
fastdfs.connection_pool.max_wait_time_in_ms=1000
fastdfs.nginx.host=192.168.31.225

 3.2 创建FastDFS配置类

漏洞

新建java文件 FastDFSConfig.java

面经

@Configuration
public class FastDFSConfig {

    @Bean
    public StorageClient storageClient() {
        StorageClient storageClient = null;
        try {
            ClientGlobal.initByProperties("application.properties");
            TrackerClient trackerClient = new TrackerClient();
            TrackerServer trackerServer = trackerClient.getConnection();
            storageClient = new StorageClient(trackerServer, null);
        } catch (IOException | MyException e) {
            e.printStackTrace();
        }
        return storageClient;
    }


}

4. 业务代码中使用

Service 层实现类

springsecurity

@Service
public class UploadServiceImpl implements UploadService {

    private static final Logger logger = LoggerFactory.getLogger(UploadServiceImpl.class);

    @Resource
    private StorageClient storageClient;

    @Value("${fastdfs.nginx.host}")
    private String serverAddr;

    @Override
    public String upload(MultipartFile file) {

        String oldName = file.getOriginalFilename();
        if (oldName != null && !"".equals(oldName)) {
            String ext = oldName.substring(oldName.lastIndexOf(".") + 1);
            try {
                String[] parts = storageClient.upload_file(file.getBytes(), ext, null);
                String path = parts != null ? parts[0] + "/" + parts[1] : null;
                return "http://" + serverAddr + "/" + path;
            } catch (IOException | MyException e) {
                logger.error("文件上传失败");
                e.printStackTrace();
            }

        }

        return null;
    }
}

Controller 层调用

提升帧率

@RestController
@RequestMapping("/fastdfs")
public class UploadController {

    @Resource
    private UploadService uploadService;

    @PostMapping("/upload")
    public String uploadFile(MultipartFile file) {
        System.out.println(">>>>>>>>> 开始上传文件 >>>>>>>>>>>");
        return uploadService.upload(file);
    }
}

5. postman 工具测试

小程序订阅消息

 

assetbundle

返回结果就是上传到服务器的文件的访问地址,浏览器地址栏可以打开。

队列

资源加载

6. 封装工具类

/**
 *  FastDFS 文件服务工具类
 * @author: cs
 * @date: 2023-02-12 19:39:25
 * @since: 1.0
 */
@Component
public class FastDFSUtil {


    private static StorageClient1 storageClient1;

    public static final String FASTDFS_NGINX_HOST_SPREFIX = "http://";

    // 服务器地址(服务器的Nginx已经配置好了,不需要加端口)
    @Value("${fastdfs.nginx.host}")
    private String serverAddr;

    /**
     * 初始化FastDFS 配置
     */
    @PostConstruct
    public void init() {
        try {
            ClientGlobal.initByProperties("application.properties");
            // 获取trackerClient服务
            TrackerClient trackerClient = new TrackerClient();
            TrackerServer trackerServer = trackerClient.getConnection();
            storageClient1 = new StorageClient1(trackerServer, null);

        } catch (IOException | MyException e) {
            e.printStackTrace();
        }
    }
    
    
    /**
     * 上传文件
     * @param file 文件对象
     * @return  文件id
     */
    public String upload(MultipartFile file) {
        String oldName = file.getOriginalFilename();
        if (oldName != null && !"".equals(oldName)) {
            String ext = oldName.substring(oldName.lastIndexOf(".") + 1);
            try {
                return FASTDFS_NGINX_HOST_SPREFIX + serverAddr + "/" + storageClient1.upload_file1(file.getBytes(), ext, null);
            } catch (IOException | MyException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    
}

 使用时就不用再写 FastDFSConfig.java 配置类了,直接在类中使用 @Resource 注入即可。

C语言深度解剖

@Resource
private FastDFSUtil fastDFSUtil;

子系统

7. 服务器中Nginx的配置

目标检测

 在server 节点下配置以上内容。监听的端口是80,注意开防火墙。

计算机网络

/home/fastdfs/storage/data 是fastdfs 服务存储文件的位置

代码封装

软著

在安装Nginx 时不用额外安装类似 fastdfs-nginx-module 这个模块。直接在nginx.conf 中将fastdfs文件的存储路径配置一下即可。

图书商城系统

MMCM

 

jira

 

UUID

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注