• pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency> <dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.2</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
  • 配置类

@Configuration
@Import(FdfsClientConfig.class)
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class FdfsConfig {

}

  • 包装类
@Component
public class FastDFSClientWrapper { private final Logger logger = LoggerFactory.getLogger(FastDFSClientWrapper.class); @Autowired
private FastFileStorageClient fastFileStorageClient; /**
* 文件上传
* 最后返回fastDFS中的文件名称;group1/M00/01/04/CgMKrVvS0geAQ0pzAACAAJxmBeM793.doc
*
* @param bytes 文件字节
* @param fileSize 文件大小
* @param extension 文件扩展名
* @return fastDfs路径
*/
public String uploadFile(byte[] bytes, long fileSize, String extension) {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
StorePath storePath = fastFileStorageClient.uploadFile(byteArrayInputStream, fileSize, extension, null);
logger.info(storePath.getGroup() + "==" + storePath.getPath() + "======" + storePath.getFullPath());
return storePath.getFullPath();
} /**
* 下载文件
* 返回文件字节流大小
* @param fileUrl 文件URL
* @return 文件字节
* @throws IOException
*/
public byte[] downloadFile(String group,String path) throws IOException {
DownloadByteArray downloadByteArray = new DownloadByteArray();
byte[] bytes = fastFileStorageClient.downloadFile(group, path, downloadByteArray);
return bytes;
} }
  • html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<meta charset="UTF-8" />
<title>Insert title here</title>
</head>
<body>
<h1 th:inlines="text">文件上传</h1>
<form action="upload" method="post" enctype="multipart/form-data">
<p>选择文件: <input type="file" name="file"/></p>
<p><input type="submit" value="提交"/></p>
</form>
</body>
</html>
  • application.yml
#fastdfs 配置
fdfs:
so-timeout: 1500
connect-timeout: 600
thumb-image:
width: 150
height: 150
tracker-list:
- 192.168.180.104:22122
spring:
thymeleaf:
prefix: classpath:/templates/
  • web层
@RestController
public class FdfsController { @Autowired
private FastDFSClientWrapper fastDFSClientWrapper; private final Logger logger = LoggerFactory.getLogger(FdfsController.class); @RequestMapping("file")
public String file(){
return "/fileUploade";
} @PostMapping("/upload")
public void upload(MultipartFile file) throws Exception {
byte[] bytes = new byte[0];
try {
bytes = file.getBytes();
} catch (IOException e) {
logger.error("获取文件错误");
e.printStackTrace();
}
//获取源文件名称
String originalFileName = file.getOriginalFilename();
//获取文件后缀--.doc
String extension = originalFileName.substring(originalFileName.lastIndexOf(".") + 1);
String fileName = file.getName();
//获取文件大小
long fileSize = file.getSize();
System.out.println(originalFileName + "==" + fileName + "==" + fileSize + "==" + extension + "==" + bytes.length);
String string = fastDFSClientWrapper.uploadFile(bytes, fileSize, extension);
//group1/M00/00/00/wKi0aF3I4dOAIhAXAAAG7e1-evI891.txt
System.out.println(string);
} @GetMapping("/downloadFile")
public void downloadFile(String group,String path,String fileName,HttpServletResponse response) throws Exception {
byte[] bytes = fastDFSClientWrapper.downloadFile(group, path);
//设置相应类型application/octet-stream (注:applicatoin/octet-stream 为通用,一些其它的类型苹果浏览器下载内容可能为空)
response.reset();
response.setContentType("applicatoin/octet-stream");
//设置头信息 Content-Disposition为属性名 附件形式打开下载文件 指定名称 为 设定的fileName
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
// 写入到流
ServletOutputStream out = response.getOutputStream();
out.write(bytes);
out.close(); } }

FastDFS整合SpringBoot(五)的更多相关文章

  1. 【FastDFS】SpringBoot整合FastDFS实战,我只看这一篇!!

    写在前面 在<[FastDFS]小伙伴们说在CentOS 8服务器上搭建FastDFS环境总报错?>和<[FastDFS]面试官:如何实现文件的大规模分布式存储?(全程实战)> ...

  2. Seata整合SpringBoot和Mybatis

    Seata整合SpringBoot和Mybatis 一.背景 二.实现功能 三.每个服务使用到的技术 1.账户服务 2.订单服务 四.服务实现 1.账户服务实现 1.引入jar包 2.项目配置 3.建 ...

  3. RabbitMQ从概念到使用、从Docker安装到RabbitMQ整合Springboot【1.5w字保姆级教学】

    @ 目录 一.前言 二.RabbitMQ作用 1. 异步处理 2. 应用解耦 3. 流量控制 三.RabbitMQ概念 1. RabbitMQ简介 2. 核心概念 四.JMS与AMQP比较 五.Rab ...

  4. docker compose搭建redis7.0.4高可用一主二从三哨兵集群并整合SpringBoot【图文完整版】

    一.前言 redis在我们企业级开发中是很常见的,但是单个redis不能保证我们的稳定使用,所以我们要建立一个集群. redis有两种高可用的方案: High availability with Re ...

  5. 整合springboot(app后台框架搭建四)

    springboot可以说是为了适用SOA服务出现,一方面,极大的简便了配置,加速了开发速度:第二方面,也是一个嵌入式的web服务,通过jar包运行就是一个web服务: 还有提供了很多metric,i ...

  6. 整合 springboot 和 swagger出问题

    整合 springboot 和 swagger ,出现报错, org.springframework.beans.factory.UnsatisfiedDependencyException: Err ...

  7. 【SpringBoot】搜索框架ElasticSearch介绍和整合SpringBoot

    ========================12章 搜索框架ElasticSearch介绍和整合SpringBoot ============================= 加入小D课堂技术交 ...

  8. FastDFS整合nginx后,nginx一直报错

    FastDFS整合nginx后,nginx一直报错: 报错内容: [2018-06-11 09:41:21] ERROR - file: ../common/fdfs_http_shared.c, l ...

  9. SpringBoot(五)_表单验证

    SpringBoot(五)_表单验证 参数校验在我们日常开发中非常常见,最基本的校验有判断属性是否为空.长度是否符合要求等,在传统的开发模式中需要写一堆的 if else 来处理这些逻辑,很繁琐,效率 ...

随机推荐

  1. 博客中gitalk最新评论的获取 github api使用

    博客中,对于网友的评论以及每篇文章的评论数还是很重要的.但是基于静态的页面想要存储动态的评论数据是比较难的,一般博客主题中都内置了评论插件,但是博客主题中对于最新评论的支持显示还是很少的,至少目前我是 ...

  2. write()与writelines()

    f = open('user','a+') f.write('abcde')   #write只能写字符串 f.writelines(['444','rrrr','uuu'])  #writeline ...

  3. 《计算机程式设计》Week4 课堂笔记

    本笔记记录自 Coursera课程 <计算机程式设计> 台湾大学 刘邦锋老师 Week4 Functions 4-1 System Function 函数主要分为两大类系统定义函数与使用者 ...

  4. request.getParameter

    request.getParameter(),该API针对的是 form表单entype的值为 application/x-www-form-urlencoded(默认值), 或者参数跟在地址栏上us ...

  5. 在centos7.4 nginx mysql php部署 thinkphp5.0 项目

    系统 centos7  环境 php 7.1.3 nignx 1.12.2 mysql 5.5.6 我是通过lnmp 集成环境安装 fastcgi.conf 末尾添加 vim fastcig.conf ...

  6. python 递归,深度优先搜索与广度优先搜索算法模拟实现

    一.递归原理小案例分析 (1)# 概述 递归:即一个函数调用了自身,即实现了递归 凡是循环能做到的事,递归一般都能做到! (2)# 写递归的过程 1.写出临界条件 2.找出这一次和上一次关系 3.假设 ...

  7. git_04_回退到上个版本

    前言 使用git版本控制的过程中,多人操作同一个项目时,有时经常会遇到代码冲突报错,一时又无法解决的问题,为了不影响他人正常使用这时便需要回滚代码至原来的版本.如何回滚代码至原来版,可参考以下步骤. ...

  8. 【ABAP系列】SAP 关于出口(user-exit)MV50AFZ1的一些问题

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP 关于出口(user-ex ...

  9. 阅读笔记09-Java程序员必备的Intellij插件

    1. .ignore 生成各种ignore文件,一键创建git ignore文件的模板,免得自己去写 地址:plugins.jetbrains.com/plugin/7495--ignore 2. l ...

  10. glibc升级,解决glib版本过低的问题

    Debian wheezy下的glibc版本为2.13,安装几个软件都运行不了,报以下类似错误:xxxx: /lib/i386-linux-gnu/i686/cmov/libc.so.6: versi ...