springboot 中fastdfs 的配置及使用

maven依赖:


    com.luhuiguo
    fastdfs-spring-boot-starter
    0.2.0

1.配置文件中配置

application-dev.yml

fdfs:

  so-timeout: 3000

  connectTimeout: 2000

  thumbImage:             #缩略图生成参数

    width: 150

    height: 150

  trackerList:            #TrackerList参数,支持多个

    - 111.202.66.46:22122

 

2.读取配置文件

import java.util.ArrayList;

import java.util.List;



import org.springframework.boot.context.properties.ConfigurationProperties;



@ConfigurationProperties(prefix = "fdfs")

public class FdfsProperties {



    /** 读取时间 */

    private int soTimeout = 1000;

    /** 连接超时时间 */

    private int connectTimeout = 1000;



    /** Tracker 地址列表 */

    private List trackerList = new ArrayList<>();



    public int getSoTimeout() {

    return soTimeout;

    }



    public void setSoTimeout(int soTimeout) {

    this.soTimeout = soTimeout;

    }



    public int getConnectTimeout() {

    return connectTimeout;

    }



    public void setConnectTimeout(int connectTimeout) {

    this.connectTimeout = connectTimeout;

    }



    public List getTrackerList() {

    return trackerList;

    }



    public void setTrackerList(List trackerList) {

    this.trackerList = trackerList;

    }

  

}

 

3.通过读取到的配置文件配置fastdfs

import org.springframework.boot.context.properties.ConfigurationProperties;

import org.springframework.boot.context.properties.EnableConfigurationProperties;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;



import com.luhuiguo.fastdfs.conn.ConnectionManager;

import com.luhuiguo.fastdfs.conn.ConnectionPoolConfig;

import com.luhuiguo.fastdfs.conn.FdfsConnectionPool;

import com.luhuiguo.fastdfs.conn.PooledConnectionFactory;

import com.luhuiguo.fastdfs.conn.TrackerConnectionManager;

import com.luhuiguo.fastdfs.service.AppendFileStorageClient;

import com.luhuiguo.fastdfs.service.DefaultAppendFileStorageClient;

import com.luhuiguo.fastdfs.service.DefaultFastFileStorageClient;

import com.luhuiguo.fastdfs.service.DefaultTrackerClient;

import com.luhuiguo.fastdfs.service.FastFileStorageClient;

import com.luhuiguo.fastdfs.service.TrackerClient;



@Configuration

@EnableConfigurationProperties(FdfsProperties.class)

public class FdfsAutoConfiguration {



    private final FdfsProperties properties;



    public FdfsAutoConfiguration(FdfsProperties properties) {

    super();

    this.properties = properties;

    }



    @Bean

    public PooledConnectionFactory pooledConnectionFactory() {

    PooledConnectionFactory pooledConnectionFactory = new PooledConnectionFactory();

    pooledConnectionFactory.setSoTimeout(properties.getSoTimeout());

    pooledConnectionFactory.setConnectTimeout(properties.getConnectTimeout());

    return pooledConnectionFactory;

    }





    @Bean

    @ConfigurationProperties(prefix = "fdfs.pool")

    public ConnectionPoolConfig connectionPoolConfig() {

    ConnectionPoolConfig connectionPoolConfig = new ConnectionPoolConfig();

    return connectionPoolConfig;

    }



    @Bean

    public FdfsConnectionPool fdfsConnectionPool(PooledConnectionFactory pooledConnectionFactory,

      ConnectionPoolConfig connectionPoolConfig) {

    FdfsConnectionPool pool =  new FdfsConnectionPool(pooledConnectionFactory, connectionPoolConfig);

    return pool;

    }



    @Bean

    public TrackerConnectionManager trackerConnectionManager(FdfsConnectionPool fdfsConnectionPool) {

    return new TrackerConnectionManager(fdfsConnectionPool, properties.getTrackerList());

    }



    @Bean

    public TrackerClient trackerClient(TrackerConnectionManager trackerConnectionManager) {

    return new DefaultTrackerClient(trackerConnectionManager);

    }



    @Bean

    public ConnectionManager connectionManager(FdfsConnectionPool fdfsConnectionPool) {

    return new ConnectionManager(fdfsConnectionPool);

    }



    @Bean

    public FastFileStorageClient fastFileStorageClient(TrackerClient trackerClient,

      ConnectionManager connectionManager) {

    return new DefaultFastFileStorageClient(trackerClient, connectionManager);

    }



    @Bean

    public AppendFileStorageClient appendFileStorageClient(TrackerClient trackerClient,

      ConnectionManager connectionManager) {

    return new DefaultAppendFileStorageClient(trackerClient, connectionManager);

    }



}

 

4.通过fastDFS 的接口来进行文件的增删改查

FastFileStorageClient storageClient



//删除文件 只需要传文件id即可,这个文件id是文件上传后,fastdfs 返回给我们的文件存储位置的一个串

storageClient.deleteFile(voiceInfo.get("voiceId").toString());

//上传文件需要的 文件信息:文件流、文件大小、文件扩展名、元数据集合

storePath uploadFile = storageClient.uploadFile(updateFile.getInputStream(), updateFile.getSize(), extension, null);

//fastdfs 下载文件

storageClient.downloadFile("group1", "fileid");

 

你可能感兴趣的:(Springboot)