文件服务器fastdfs的安装

docker安装fastdfs

先使用df -h命令, 查看一下磁盘剩余空间

暴露22122端口 23000 否则下面的创建会有问题
8888端口也需要暴露 将来通过这个端口访问图片

之后分别执行以下两条语句

docker run -d --network=host --name some-tracker -v /var/fdfs/tracker:/var/fdfs delron/fastdfs tracker
docker run -d --network=host --name some-storage -e TRACKER_SERVER=服务器IP地址:22122 -v /var/fdfs/storage:/var/fdfs -e GROUP_NAME=group1 delron/fastdfs storage

使用docker ps 命令确定已经安装成功

在这里插入图片描述

Java使用fashdfs上传图片

创建Maven项目

1、添加依赖

 	<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.13</version>
    </parent>
		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>

        <dependency>
            <groupId>com.github.tobato</groupId>
            <artifactId>fastdfs-client</artifactId>
            <version>1.27.2</version>
        </dependency>

2、添加applocatiom.yml文件配置

fdfs:
  so-timeout: 1501
  connect-timeout: 601
  thumb-image:             #缩略图生成参数
    width: 150
    height: 150
  tracker-list:            #TrackerList参数,支持多个
    - 服务器IP地址:22122

3、添加配置文件fastdfs配置类

解决jmx重复注册bean的问题


import com.github.tobato.fastdfs.FdfsClientConfig;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.context.annotation.Import;
import org.springframework.jmx.support.RegistrationPolicy;

@Configuration
@Import(FdfsClientConfig.class)
// 解决jmx重复注册bean的问题
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class ComponetImport {
    // 导入依赖组件
}

4、添加SpringBoot项目启动类

@SpringBootApplication
public class FastdfsApplication {
    public static void main(String[] args) {
        SpringApplication.run(FastdfsApplication.class);
    }
}

5、创建测试代码

@SpringBootTest
class ComponetImportTest {

    @Autowired
    private FastFileStorageClient storageClient;
    @Test
    void test1() throws IOException {
        FileInputStream fis = new FileInputStream("DPlayer.png");
        StorePath storePath = storageClient.uploadFile(fis, fis.available(), "png", null);
        //StorePath uploadFile(InputStream inputStream, long fileSize, String fileExtName, Set<MetaData> metaDataSet);

        String fullPath = storePath.getFullPath();
        System.out.println("fullPath = " + fullPath);
    }
}

其中,"DPlayer.png"与"pom.xml"平级,也可放在其他路径,文件位置则需要添加相应的位置前缀

运行代码获得图片的完整路径fullPath

fullPath = group1/M00/00/00/CgAIBGT2p_CAFPG7AAuj8_BVOks893.png

6、使用服务器IP地址+端口号+fullPath即可访问图片

http://服务器IP地址:8888/group1/M00/00/00/CgAIBGT2p_CAFPG7AAuj8_BVOks893.png

在这里插入图片描述

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值