赞
踩
拉取镜像:
docker pull morunchang/fastdfs
运行tracker:
docker run -d --name tracker --net=host morunchang/fastdfs sh tracker.sh
运行storage
docker run -d --name storage --net=host -e TRACKER_IP=虚拟机ip:22122 -e GROUP_NAME=group1 morunchang/fastdfs sh storage.sh
使用的网络模式是–net=host,host模式可以不用映射容器端口宿主机, 替换为你机器的Ip即可 是组名,即storage的组 如果想要增加新的storage服务器,再次运行该命令,注意更换 新组名
1.引入依赖
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.7</version>
</dependency>
2.在配置文件中配置:(这里用application.yml)
fdfs:
# 链接超时
connect-timeout: 60
# 读取时间 so-timeout: 60 # 生成缩略图参数
thumb-image:
width: 150
height: 150
tracker-list: 虚拟机ip:22122
3.编写一个配置类
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)
public class DfsConfig {
}
4.使用方法
private static final Logger LOGGER = LoggerFactory.getLogger(FileDfsUtil.class); @Resource private FastFileStorageClient storageClient ; /** * 上传文件 */ public String upload(MultipartFile multipartFile) throws Exception{ String originalFilename = multipartFile.getOriginalFilename(). substring(multipartFile.getOriginalFilename(). lastIndexOf(".") + 1); StorePath storePath = this.storageClient.uploadImageAndCrtThumbImage( multipartFile.getInputStream(), multipartFile.getSize(),originalFilename , null); return storePath.getFullPath() ; } /** * 删除文件 */ public void deleteFile(String fileUrl) { if (StringUtils.isEmpty(fileUrl)) { LOGGER.info("fileUrl == >>文件路径为空..."); return; } try { StorePath storePath = StorePath.parseFromUrl(fileUrl); storageClient.deleteFile(storePath.getGroup(), storePath.getPath()); } catch (Exception e) { LOGGER.info(e.getMessage()); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。