SpringBoot集成FastDFS

集成步骤

  1. 生成jar包
  2. 引入maven
  3. 添加配置文件
  4. 写Utile类

1、生成jar包

FastDFS开源地址:https://github.com/happyfish100
选择javasdk下载代码生成jar包,如图所示,这里是我生成的jar包fastdfs-client-java-1.29-SNAPSHOT.jar
jar包地址:https://download.csdn.net/download/yylhm1125/21790071


jar包位置
在这里插入图片描述

2、引入maven

<dependency>
        <groupId>org.csource</groupId>
        <artifactId>fastdfs-client-java</artifactId>
        <version>1.29-SNAPSHOT</version>
</dependency>

因为是jar包引入所以需要配置一下

<build>
        <plugins>
                <plugin>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
                <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-install-plugin</artifactId>
                        <version>2.5</version>
                        <executions>
                                <execution>
                                        <id>install-fastdfs-client-java-jar</id>
                                        <phase>clean</phase>
                                        <configuration>
                                                <repositoryLayout>default</repositoryLayout>
                                                <groupId>org.csource</groupId>
                                                <artifactId>fastdfs-client-java</artifactId>
                                                <version>1.29-SNAPSHOT</version>
                                                <file>${project.basedir}/lib/fastdfs-client-java-1.29-SNAPSHOT.jar</file>
                                                <packaging>jar</packaging>
                                                <generatePom>true</generatePom>
                                                <packaging>jar</packaging>
                                        </configuration>
                                        <goals>
                                                <goal>install-file</goal>
                                        </goals>
                                </execution>
                        </executions>
                </plugin>
        </plugins>
</build>

3、添加配置文件

文件名:fdfs_client.conf

connect_timeout = 2
network_timeout = 30
charset = UTF-8
http.tracker_http_port = 8888
http.anti_steal_token = no
http.secret_key = FastDFS1234567890

tracker_server = 192.168.100.51:22122
#tracker_server = 10.0.11.244:22122

connection_pool.enabled = true
connection_pool.max_count_per_entry = 500
connection_pool.max_idle_time = 3600
connection_pool.max_wait_time_in_ms = 1000

文件位置
在这里插入图片描述

4、写Util类

package com.practice.demo.base.util;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

import java.io.*;

/**
 * fastDFS文件上传工具类
 */
@Slf4j
@Data
@Component
public class FastDFSUtil {
    private static TrackerClient trackerClient;
    private static TrackerServer trackerServer;
    private static StorageServer storageServer;
    private static StorageClient1 storageClient1;
    static {
        try {
            String filePath = new ClassPathResource("fdfs_client.conf").getFile().getAbsolutePath();;
            ClientGlobal.init(filePath);
            trackerClient = new TrackerClient();
            trackerServer = trackerClient.getTrackerServer();
            storageServer = trackerClient.getStoreStorage(trackerServer);
            storageClient1 = new StorageClient1(trackerServer, storageServer);
        } catch (Exception e) {
            log.error("FastDFS Client Init Fail!",e);
        }
    }
    /**
     * 上传文件
     * @param fileContent 文件内容
     * @param extName 后缀名
     * @return
     * @throws Exception
     */
    public static String[] uploadFile(byte[] fileContent, String extName) {

        try {
            //upload_file()三个参数:@param fileContent ①:文件的内容,字节数组 ②:文件扩展名 ③文件扩展信息 数组
            return storageClient1.upload_file(fileContent, extName, null);
        } catch (Exception e) {
            log.error(e.toString(), e);
            return null;
        }
    }
    /**
     * 下载文件
     * @param fileId 文件id(包括groupId)
     * @return
     */
    public static byte[] downloadFile(String fileId) {
        try {
            byte[] fileByte = storageClient1.download_file1(fileId);
            return fileByte;
        } catch (Exception e) {
            log.error(e.toString(), e);
            return null;
        }
    }
    /**
     * 删除文件
     * @param fileId 带组名的路径名称 如:group1/M00/00/00/wKgRsVjtwpSAXGwkAAAweEAzRjw471.jpg
     * @return -1失败,0成功 ,2找不到文件
     */
    public static int deleteFile(String fileId) {

        try {
            int i = storageClient1.delete_file1(fileId);
            log.info("delete file successfully!!!" + i);
            return 1;
        } catch (Exception e) {
            log.error(e.toString(), e);
            return 0;
        }
    }
    /**
     * @Description  :获取文件信息
     * @author       : lihuimin
     * @param        : fileId
     * @return       : 
     * @date         : 2021-07-27 17:10
     */
    public static FileInfo getFile(String fileId) {
        try {
            return storageClient1.get_file_info1(fileId);
        } catch (IOException e) {
            log.error("IO Exception: Get File from Fast DFS failed", e);
        } catch (Exception e) {
            log.error("Non IO Exception: Get File from Fast DFS failed", e);
        }
        return null;
    }
    /**
     * @Description  :获取文件上传之前保存的信息,
     * @author       : lihuimin
     * @param        : fileId
     * @return       : 
     * @date         : 2021-07-27 17:22
     */
    public static void testGetFileMatedata(String fileId) {
        try {
            NameValuePair nvps[] = storageClient1.get_metadata1(fileId);
            if (null != nvps && nvps.length > 0) {
                for (NameValuePair nvp : nvps) {
                    System.out.println(nvp.getName() + ":" + nvp.getValue());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) throws IOException {
        //group1/M00/00/00/wKhkM2D_gQWAS2g_AA-itrfn0m4.tar.gz
        //group1/M00/00/00/wKhkM2D_ykmALQd9AADOwrv3BxE949.png
        //开始上传
//        File f = new File("D:\\折线图堆叠.png");
//        FileInputStream fin = new FileInputStream(f);
//        byte[] ib = new byte[(int)f.length()];
//        fin.read(ib);
//        fin.close();
//        String[] fileid = FastDFSUtil.uploadFile(ib,"png");
//        System.out.println(fileid.length);
//        if(fileid!=null&&fileid.length>0){
//            for(String str:fileid){
//                System.out.println(str);
//            }
//        }
        //上传结束
        //开始下载
//        byte[] ib = FastDFSUtil.downloadFile("group1/M00/00/00/wKhkM2D_ykmALQd9AADOwrv3BxE949.png");
//        if(ib != null){
//            String filepath ="D:\\" + "1232.png";
//            File file  = new File(filepath);
//            if(file.exists()){
//                file.delete();
//            }
//            FileOutputStream fos = new FileOutputStream(file);
//            fos.write(ib,0,ib.length);
//            fos.flush();
//            fos.close();
//        }
        //下载结束
        //开始获取文件信息
        FileInfo fileInfo = FastDFSUtil.getFile("group1/M00/00/00/wKhkM2D_ykmALQd9AADOwrv3BxE949.png");
        System.out.println(fileInfo.toString());
        //结束获取文件信息
        //开始删除
//        int i = FastDFSUtil.deleteFile("group1/M00/00/00/wKhkM2D_gQWAS2g_AA-itrfn0m4.tar.gz");
//        System.out.println(i);
        //删除结束
        //获取文件上传之前信息
//        FastDFSUtil.testGetFileMatedata("group1/data/M00/00/00/wKhkM2D_ykmALQd9AADOwrv3BxE949.png");
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值