拉取镜像并启动

docker run -d --restart=always --privileged=true --net=host --name=fastdfs -e IP=106.54.238.xxx -e WEB_PORT=8000 -v ${HOME}/fastdfs:/var/local/fdfs registry.cn-beijing.aliyuncs.com/tianzuo/fastdfs

 需要开放3个端口, fastdfs-tracker(22122)  fastdfs-storage(23000)  fastdfs-nginx(8000)

firewall-cmd --zone=public --add-port=8000/tcp --permanent

firewall-cmd --reload

$HOME是用户的主目录,登录后缺省进入的目录, cd ~进入

其中-v ${HOME}/fastdfs:/var/local/fdfs是指:将${HOME}/fastdfs这个目录挂载到容器里的/var/local/fdfs这个目录里。

所以上传的文件将被持久化到${HOME}/fastdfs/storage/data里,IP 后面是自己的服务器公网ip或者虚拟机ip,

-e WEB_PORT=8000 指定nginx端口

如果有问题可以看一下是否占用8000端口或是否开启22122 23000端口

  • 引入依赖
  <dependency>
            <groupId>com.github.tobato</groupId>
            <artifactId>fastdfs-client</artifactId>
        </dependency>
  •  配置
fdfs:
  so-timeout: 1501  #超时时间
  connect-timeout: 601  #连接超时时间
  thumb-image:  #上传原图和缩略图,可返回缩略图url给前端
    width: 60
    height: 60
  tracker-list:  #tracker地址
    - 106.54.238.xxx:22122   #多个 trackerServer中间以逗号分隔
  • Test
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.domain.ThumbImageConfig;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

@RunWith(SpringRunner.class)
@SpringBootTest
public class FdfsTest {

    @Autowired
    private FastFileStorageClient storageClient;

    @Autowired
    private ThumbImageConfig thumbImageConfig;

    //普通上传
    @Test
    public void testUpload() throws FileNotFoundException {
        File file = new File("C:\\Users\\JavaDev\\Desktop\\she.jpg");
        // 上传
        StorePath storePath = this.storageClient.uploadFile(
                new FileInputStream(file), file.length(), "jpg", null);
        // 带分组的路径
        System.out.println(storePath.getFullPath());
        // 不带分组的路径
        System.out.println(storePath.getPath());
    }

    //上传并创建缩略图
    @Test
    public void testUploadAndCreateThumb() throws FileNotFoundException {
        File file = new File("D:\\image\\1.jpg");
        // 上传并且生成缩略图
        StorePath storePath = this.storageClient.uploadImageAndCrtThumbImage(
                new FileInputStream(file), file.length(), "jpg", null);
        // 带分组的路径
        System.out.println(storePath.getFullPath());
        // 不带分组的路径
        System.out.println(storePath.getPath());
        // 获取缩略图路径
        String path = thumbImageConfig.getThumbImagePath(storePath.getPath());
        System.out.println(path);
    }
}
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐