FastDFS文件上传下载流程

1|0上传:

  1. Client通过Tracker server查找可用的Storage server。

  2. Tracker server向Client返回一台可用的Storage server的IP地址和端口号。

  3. Client直接通过Tracker server返回的IP地址和端口与其中一台Storage server建立连接并进行文件上传。

  4. 上传完成,Storage server返回Client一个文件ID,文件上传结束。

2|0下载:

  1. Client通过Tracker server查找要下载文件所在的的Storage server。

  2. Tracker server向Client返回包含指定文件的某个Storage server的IP地址和端口号。

  3. Client直接通过Tracker server返回的IP地址和端口与其中一台Storage server建立连接并指定要下载文件。

  4. 下载文件成功。

3|0java客户端

3|1springboot项目引入依赖

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

3|2配置类

package com.leyou.config; 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 FastClientImporter { }

3|3在yml中编写FastDFS的属性

3|4测试:

package com.leyou.test; import com.github.tobato.fastdfs.domain.StorePath; import com.github.tobato.fastdfs.domain.ThumbImageConfig; import com.github.tobato.fastdfs.service.FastFileStorageClient; import com.leyou.LyUploadService; 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(classes = LyUploadService.class) public class FdfsTest { @Autowired private FastFileStorageClient storageClient; @Autowired private ThumbImageConfig thumbImageConfig; @Test public void testUpload() throws FileNotFoundException { File file = new File("G:\\LeYou\\upload\\spitter_logo_50.png"); // 上传 StorePath storePath = this.storageClient.uploadFile( new FileInputStream(file), file.length(), "png", null); // 带分组的路径 System.out.println(storePath.getFullPath()); // 不带分组的路径 System.out.println(storePath.getPath()); } @Test public void testUploadAndCreateThumb() throws FileNotFoundException { File file = new File("G:\\LeYou\\upload\\spitter_logo_50.png"); // 上传并且生成缩略图 StorePath storePath = this.storageClient.uploadImageAndCrtThumbImage( new FileInputStream(file), file.length(), "png", null); // 带分组的路径 System.out.println(storePath.getFullPath()); // 不带分组的路径 System.out.println(storePath.getPath()); // 获取缩略图路径 String path = thumbImageConfig.getThumbImagePath(storePath.getPath()); System.out.println(path); } }

4|0具体应用中上传

package com.leyou.upload.service.serviceimpl; import com.github.tobato.fastdfs.domain.StorePath; import com.github.tobato.fastdfs.service.FastFileStorageClient; import com.leyou.upload.service.UploadService; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.util.Arrays; import java.util.List; import java.util.Objects; @Service public class UploadServiceImpl implements UploadService { @Autowired private FastFileStorageClient storageClient; private static final Logger logger= LoggerFactory.getLogger(UploadServiceImpl.class); /** * 支持上传的文件类型 */ private static final List<String> suffixes = Arrays.asList("image/png","image/jpeg","image/jpg"); @Override public String upload(MultipartFile file) { /** * 1.图片信息校验 * 1)校验文件类型 * 2)校验图片内容 * 2.保存图片 * 1)生成保存目录 * 2)保存图片 * 3)拼接图片地址 */ try { String type = file.getContentType(); if (!suffixes.contains(type)) { logger.info("上传文件失败,文件类型不匹配:{}", type); return null; } BufferedImage image = ImageIO.read(file.getInputStream()); if (image == null) { logger.info("上传失败,文件内容不符合要求"); return null; } StorePath storePath = this.storageClient.uploadFile( file.getInputStream(), file.getSize(), getExtension(file.getOriginalFilename()), null); String url = "http://image.leyou.com/"+storePath.getFullPath(); return url; }catch (Exception e){ return null; } } public String getExtension(String fileName){ return StringUtils.substringAfterLast(fileName,"."); } }

__EOF__

本文作者程序员小宇
本文链接https://www.cnblogs.com/treasury/p/12872754.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   程序员小宇  阅读(1966)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示