springboot集成fastdfs

编程

1 引入jar

<dependency>

<groupId>com.github.tobato</groupId>

<artifactId>fastdfs-client</artifactId>

<version>1.26.1-RELEASE</version>

</dependency>

说明: 

springboot的版本为2.0以上时候,使用1.26.1-RELEASE版本

2 引入配置:

 

@Configuration

@Import(FdfsClientConfig.class)

// 解决jmx重复注册bean的问题

@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)

public class FastClientImporter {

}

3 yml 配置:

fdfs:

so-timeout: 1501

connect-timeout: 601

thumb-image: # 缩略图

width: 60

height: 60

tracker-list: # tracker地址

10.96.165.173:22122

4使用:

@Service

public class PictureServiceImpl implements PictureService {

@Autowired

private FastFileStorageClient fastFileStorageClient;

@Value("${IMAGE_SERVER_URL}")

private String IMAGE_SERVER_URL;

@Override

public String uploadPicture(MultipartFile file){

//截取后缀

try {

String originalFilename = file.getOriginalFilename();

System.out.println(originalFilename);

String extName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);

System.out.println(extName);

StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()), null);

System.out.println(storePath);

String fileUrl = IMAGE_SERVER_URL + storePath.getFullPath();

return fileUrl;

} catch (IOException e) {

e.printStackTrace();

return null;

}

}

}

docker 搭建 fastdfs server

前提: 安装docker   

yum install -y docker-io

1 拉取镜像:

docker pull qbanxiaoli/fastdfs

2 docker 启动

docker run -d --restart=always --privileged=true --net=host --name=fastdfs -e IP=192.168.127.131 -e WEB_PORT=80 -v ${HOME}/fastdfs:/var/local/fdfs qbanxiaoli/fastdfs

3 检验:

docker exec -it fastdfs /bin/bash

echo "Hello FastDFS!">index.html

fdfs_test /etc/fdfs/client.conf upload index.html

 

以上是 springboot集成fastdfs 的全部内容, 来源链接: utcz.com/z/510403.html

回到顶部