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