前言

对FastDFS文件系统安装后的使用。

FastDFS的安装请参考这篇:https://www.cnblogs.com/niceyoo/p/13511082.html

本文环境:IDEA + JDK1.8 + Maven

1、引入依赖

简单说一下这个依赖部分,目前大部分都是采用的如下依赖:

<!-- https://mvnrepository.com/artifact/net.oschina.zcx7878/fastdfs-client-java -->
<dependency><groupId>net.oschina.zcx7878</groupId><artifactId>fastdfs-client-java</artifactId><version>1.27.0.0</version>
</dependency>

本着不重复造轮子,且为了使用方便我们可以去GitHub找一个集成好的依赖:

https://github.com/tobato/FastDFS_Client

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

2、将Fdfs配置引入项目

只需要创建一个配置类就可以了:

@Configuration
@Import(FdfsClientConfig.class)
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class ComponetImport {// 导入依赖组件
}

参考截图:

3、在application.yml当中配置Fdfs相关参数

根据自己情况修改相应ip地址及端口号:

server:port: 8080ip: 10.211.55.4 # 根据自己FastDFS服务器修改fdfs:so-timeout: 1501connect-timeout: 601thumb-image:             #缩略图生成参数width: 150height: 150tracker-list:            #TrackerList参数,支持多个- 10.211.55.4:22122web-server-url: http://${ip}:8888/

4、client封装工具类

import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.Charset;@Component
public class FastDFSClient {@Autowiredprivate FastFileStorageClient storageClient;@Autowiredprivate FdfsWebServer fdfsWebServer;/*** 上传文件* @param file 文件对象* @return 文件访问地址* @throws IOException*/public String uploadFile(MultipartFile file) throws IOException {StorePath storePath = storageClient.uploadFile(file.getInputStream(),file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()),null);return getResAccessUrl(storePath);}/*** 上传文件* @param file 文件对象* @return 文件访问地址* @throws IOException*/public String uploadFile(File file) throws IOException {FileInputStream inputStream = new FileInputStream (file);StorePath storePath = storageClient.uploadFile(inputStream,file.length(), FilenameUtils.getExtension(file.getName()),null);return getResAccessUrl(storePath);}/*** 将一段字符串生成一个文件上传* @param content 文件内容* @param fileExtension* @return*/public String uploadFile(String content, String fileExtension) {byte[] buff = content.getBytes(Charset.forName("UTF-8"));ByteArrayInputStream stream = new ByteArrayInputStream(buff);StorePath storePath = storageClient.uploadFile(stream,buff.length, fileExtension,null);return getResAccessUrl(storePath);}/*** 封装图片完整URL地址*/private String getResAccessUrl(StorePath storePath) {String fileUrl = fdfsWebServer.getWebServerUrl() + storePath.getFullPath();return fileUrl;}/*** 删除文件* @param fileUrl 文件访问地址* @return*/public void deleteFile(String fileUrl) {if (StringUtils.isEmpty(fileUrl)) {return;}try {StorePath storePath = StorePath.parseFromUrl(fileUrl);storageClient.deleteFile(storePath.getGroup(), storePath.getPath());} catch (FdfsUnsupportStorePathException e) {System.out.println(e.getMessage());// TODO 只是测试,所以未使用,logger,正式环境请修改打印方式}}/*** 下载文件** @param fileUrl 文件URL* @return 文件字节* @throws IOException*/public byte[] downloadFile(String fileUrl) throws IOException {String group = fileUrl.substring(0, fileUrl.indexOf("/"));String path = fileUrl.substring(fileUrl.indexOf("/") + 1);DownloadByteArray downloadByteArray = new DownloadByteArray();byte[] bytes = storageClient.downloadFile(group, path, downloadByteArray);return bytes;}}

5、创建Conttoler测试类

5.1 文件上传测试
@RestController
@RequestMapping("/file")
public class FileUploadController {@Autowiredprivate FastDFSClient fastDFSClient;/*** 上传* @param file* @return* @throws IOException*/@RequestMapping("/upload")public String uploadFile(MultipartFile file) throws IOException {return fastDFSClient.uploadFile(file);}}

执行效果截图:

5.2、下载文件测试

@RestController
@RequestMapping("/file")
public class FileUploadController {@Autowiredprivate FastDFSClient fastDFSClient;/*** 下载* @param fileUrl* @param response* @throws IOException*/@RequestMapping("/download")public void downloadFile(String fileUrl, HttpServletResponse response) throws IOException {byte[] bytes = fastDFSClient.downloadFile(fileUrl);// TODO 这里只是为了整合fastdfs,所以写死了文件格式。需要在上传的时候保存文件名。下载的时候使用对应的格式response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("sb.xlsx", "UTF-8"));response.setCharacterEncoding("UTF-8");ServletOutputStream outputStream = null;try {outputStream = response.getOutputStream();outputStream.write(bytes);} catch (IOException e) {e.printStackTrace();} finally {try {outputStream.flush();outputStream.close();} catch (IOException e) {e.printStackTrace();}}}}

测试下载路径:

http://127.0.0.1:8080/file/download?fileUrl=group1/M00/00/00/CtM3BF84r4SAEPDgAABoGL78QcY682.jpg

拼接的参数为:group1/M00/00/00/CtM3BF84r4SAEPDgAABoGL78QcY682.jpg

大家想修改路径的话,需要同步修改 downloadFile() 方法里的分隔方式。

你知道的越多,不知道的就越多,欢迎关注我的微信公众号:niceyoo

SpringBoot集成FastDFS依赖实现文件上传相关推荐

  1. SpringBoot集成七牛云-实现文件上传、下载、解决报错、详细案例

    用自己电脑做文件存储,我想没谁愿意了吧,来来和我一起白嫖吧,伙伴们!!!

  2. SpringBoot整合阿里云OSS文件上传、下载、查看、删除

    SpringBoot整合阿里云OSS文件上传.下载.查看.删除 该项目源码地址:https://github.com/ggb2312/springboot-integration-examples ( ...

  3. MinIO入门-02 SpringBoot 整合MinIO并实现文件上传

    SpringBoot 整合MinIO并实现文件上传 1.依赖 <!-- https://mvnrepository.com/artifact/io.minio/minio --> < ...

  4. FastDFS 设计理念、文件上传、下载、同步、删除和断点续传原理

    一.FastDFS 系统架构和功能原理 1.架构详解 storage server:存储服务器(又称存储节点或数据服务器),文件和文件属性(meta data)都保存到存储服务器上.Storage s ...

  5. fastdfs连接mysql_fastDFS文件上传简单案例

    基于fastDFS做了一个简单的文件上传案例(贼简陋),文件上传成功后将文件信息保存到MySQL数据库中 pom.xml 1 2 3 org.springframework.boot 4 spring ...

  6. java百度云文件上传_关于如何在自己项目集成百度云BCE文件上传STS方案

    1. 项目背景 由于本人项目需要,需要在视频点播服务之中需要加载字幕文件(通用格式srt),经过比较好几家的公有云服务,最后选择只有百度云提供字幕服务. 字幕:我们通常在观看外语电影的是,没有国语版时 ...

  7. springBoot 简单优雅是实现文件上传和下载

    前言 好久没有更新spring Boot 这个项目了.最近看了一下docker 的知识,后期打算将spring boot 和docker 结合起来.刚好最近有一个上传文件的工作呢,刚好就想起这个脚手架 ...

  8. SpringBoot整合Jersey2.x实现文件上传API

    前言 SpringBoot的官方文档中关于Jersey的介绍并不是很全面: 27.3 JAX-RS and Jersey,SpringBoot-Sample项目里面也只有非常基础的代码,对于一些复杂的 ...

  9. 【前端】wangeditor源码修改,打包发布到npm,实现上传视频功能,得到视频的第一帧保存为封面,spring-boot+vue,axios实现文件上传,视频图片浏览

    一.实现 1.创建git分支,clone下源码 git地址 创建分支 2.图片上传具有文件选择的功能,所以我完全模仿(抄袭)图片上传 报错不慌,全部改完就不报错了 1)在src/config/inde ...

最新文章

  1. 修改xampp的mysql默认密码
  2. 前端学习(2803):点击商品列表导航到商品详情页
  3. Android4.4的init进程
  4. sql server死锁_如何使用扩展事件和SQL Server代理自动执行SQL Server死锁收集过程
  5. orm框架设计、分析与开发
  6. 历史上华人计算机科学家,郑州大学韩家炜、张宏江两位校友在世界顶尖计算机科学家排名中再度名列华人科学家和中国大陆科学家之首...
  7. 纠错码专题——线性分组码(1)
  8. Unity3D Dither 抖动Shader实现
  9. 微信公众号H5网页支付
  10. 无Internet访问权限-已解决
  11. Java实现Zip压缩包解压
  12. 力荐 50 个最实用的免费机器学习数据集
  13. 电影最top《投名状》摘录
  14. 运筹说 第6期|运筹学自媒体的“百家争鸣”
  15. 如何切换Linux用户(penguin)
  16. 北极寒流带来《后天》享受(组图)零下50度美国城市成灾区出门都犯法
  17. 感悟+北京and新疆知识点
  18. android中gravity什么意思,【Android】“android:gravity”和“android:layout_gravity”属性解释...
  19. 这个传奇大佬,自杀了!
  20. 阿里云超算:高性能容器方案实战之Singularity

热门文章

  1. [html] 你是如何区分HTML和HTML5的?
  2. [vue] vue生命周期的作用是什么?
  3. 前端学习(2839):swiper属性
  4. “约见”面试官系列之常见面试题之第六十二篇之IE和兼容下写法(建议收藏)
  5. 前端学习(2006)vue之电商管理系统电商系统之绘制商品参数的复选框
  6. 前端学习(1854)vue之电商管理系统电商系统之git push后出现错误 ![rejected] master -> master(non-fast-forward) error:failed t
  7. 前端学习(1841):前端面试题之react和vue区别
  8. 前端学习(1688):前端系列javascript之几个面试题
  9. 前端学习(799):根据位置返回字符
  10. 公司网络故障那些事(路由器变交换机)