FastDFS整合SpringBoot(五)
- 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(五)的更多相关文章
- 【FastDFS】SpringBoot整合FastDFS实战,我只看这一篇!!
写在前面 在<[FastDFS]小伙伴们说在CentOS 8服务器上搭建FastDFS环境总报错?>和<[FastDFS]面试官:如何实现文件的大规模分布式存储?(全程实战)> ...
- Seata整合SpringBoot和Mybatis
Seata整合SpringBoot和Mybatis 一.背景 二.实现功能 三.每个服务使用到的技术 1.账户服务 2.订单服务 四.服务实现 1.账户服务实现 1.引入jar包 2.项目配置 3.建 ...
- RabbitMQ从概念到使用、从Docker安装到RabbitMQ整合Springboot【1.5w字保姆级教学】
@ 目录 一.前言 二.RabbitMQ作用 1. 异步处理 2. 应用解耦 3. 流量控制 三.RabbitMQ概念 1. RabbitMQ简介 2. 核心概念 四.JMS与AMQP比较 五.Rab ...
- docker compose搭建redis7.0.4高可用一主二从三哨兵集群并整合SpringBoot【图文完整版】
一.前言 redis在我们企业级开发中是很常见的,但是单个redis不能保证我们的稳定使用,所以我们要建立一个集群. redis有两种高可用的方案: High availability with Re ...
- 整合springboot(app后台框架搭建四)
springboot可以说是为了适用SOA服务出现,一方面,极大的简便了配置,加速了开发速度:第二方面,也是一个嵌入式的web服务,通过jar包运行就是一个web服务: 还有提供了很多metric,i ...
- 整合 springboot 和 swagger出问题
整合 springboot 和 swagger ,出现报错, org.springframework.beans.factory.UnsatisfiedDependencyException: Err ...
- 【SpringBoot】搜索框架ElasticSearch介绍和整合SpringBoot
========================12章 搜索框架ElasticSearch介绍和整合SpringBoot ============================= 加入小D课堂技术交 ...
- FastDFS整合nginx后,nginx一直报错
FastDFS整合nginx后,nginx一直报错: 报错内容: [2018-06-11 09:41:21] ERROR - file: ../common/fdfs_http_shared.c, l ...
- SpringBoot(五)_表单验证
SpringBoot(五)_表单验证 参数校验在我们日常开发中非常常见,最基本的校验有判断属性是否为空.长度是否符合要求等,在传统的开发模式中需要写一堆的 if else 来处理这些逻辑,很繁琐,效率 ...
随机推荐
- Jmeter源码框架
首先jmeter框架入口类: NewDriver类(src/core/org/apache/jmeter/NewDriver.java) public static void main(String[ ...
- Makefile之patsubst
经常要手写项目的Makefile,或者看其他项目的遗留项目的Makefile,有些makefile内置函数常用, 却用完就忘记了,最近项目中使用patsubst,感觉挺好用的 格式:$(pa ...
- java对象的方法属性和代码块的加载顺序
1.静态变量 2.静态代码块 3.局部代码块 4.构造函数 5.普通代码块 6.静态方法 7.普通方法 8.普通属性 for example: package com.JavaTest2; publi ...
- ios平台appstore不支持网页内嵌app解决方案
苹果一直拒绝 UIWebView 内嵌 HTML5 页面的 iPhone.iPad APP应用上架到 App Store,建议这样的APP去做成Safari的Web应用.但是,苹果的审核人员只从界面. ...
- How to increase timeout for your ASP.NET Application ?
How to increase timeout for your ASP.NET Application ? 原文链接:https://www.techcartnow.com/increase-tim ...
- Kali系统 metasploit 使用教程
基础配置 由于kali 2.0 已经没有metasploit 这个服务了,所以service metasploit start 的方式不起作用. 在kali 2.0中启动带数据库支持的MSF方式如下: ...
- 案例 element 表单名两端对齐
>>> .el-form-item label:after { content: ""; display: inline-block; width: 100%; ...
- 2019-4-21-Roslyn-通过-NuGet-库修改应用程序入口函数
title author date CreateTime categories Roslyn 通过 NuGet 库修改应用程序入口函数 lindexi 2019-4-21 17:37:1 +0800 ...
- OpenVINO 安装及使用
安装 https://docs.openvinotoolkit.org/latest/_docs_install_guides_installing_openvino_linux.html 使用 文档 ...
- python-验证功能的装饰器示例
user_list=[ {'}, {'}, {'} ] current_dict={'username':None,'login':False} def auth(auth_type): def au ...