没有什么事情是一行代码解决不了的,如果有那么就是两行。
参考资料: https://github.com/tobato/FastDFS_Client
写在前面:此篇博客,主要介绍 fastdfs-client 依赖。
com.github.tobato
fastdfs-client
1.26.4
将FastDFS-Client客户端引入本地化项目的方式非常简单,在SpringBoot项目/src/[com.xxx.主目录]/conf
当中配置
/**
* 导入FastDFS-Client组件
*
* @author tobato
*
*/
@Configuration
@Import(FdfsClientConfig.class)
// 解决jmx重复注册bean的问题
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class ComponetImport {
// 导入依赖组件
}
@Import(FdfsClientConfig.class) 注解也可以加载项目的启动类上。
注意:
@EnableMBeanExport
解决问题JMX重复注册问题,不要再配置spring.jmx.enabled=false
,以免影响SpringBoot默认的JMX监控。
#===================================== fastdfs =============================
fdfs:
so-timeout: 1501
connect-timeout: 600
thumb-image: #缩略图生成参数
width: 150
height: 150
tracker-list:
- 192.168.0.29:22122 #如果有多个tracker节点可以添加多行 - 192.168.0.28:22122
如果有必要可以参考 apache.pool2 参数配置连接池属性
fdfs:
..其他配置信息..
pool:
#从池中借出的对象的最大数目
max-total: 153
#获取连接时的最大等待毫秒数100
max-wait-millis: 102
主要接口包括
参考下面文章进行改造
https://blog.csdn.net/wzl19870309/article/details/74049204
通过加大超时时间后解决
soTimeout: 1500
connectTimeout: 600
以上内容来自:https://github.com/tobato/FastDFS_Client
所有请求参数均在注释中有说明
package com.coco.demo.fastdfs.controller;
import com.github.tobato.fastdfs.domain.MetaData;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashSet;
import java.util.Set;
/**
* @ClassName FastDFSController
* @Description FastDFS 文件上传下载删除
* @Author coco
* @Data 2018/11/16 10:25
* @Version 1.0
**/
@RestController
@RequestMapping("/fdfs")
public class FastDFSController {
@Autowired
FastFileStorageClient fastFileStorageClient;
/**
* 文件上传
*
* storePath
* "group": "group1",
* "path": "M00/00/01/wKj4RFvuZlOALpzMAAJZc0IRPSY16.docx",
* "fullPath": "group1/M00/00/01/wKj4RFvuZlOALpzMAAJZc0IRPSY16.docx"
*
* @param file
* @return StorePath
* @throws IOException
*/
@PostMapping("/upload")
public StorePath uploadFile(MultipartFile file) throws IOException {
// 设置文件信息
Set mataData = new HashSet<>();
mataData.add(new MetaData("author", "author"));
mataData.add(new MetaData("description", "附件上传"));
//文件信息可以为空 直接传null即可
StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(),
FilenameUtils.getExtension(file.getOriginalFilename()), mataData);
return storePath;
}
@PostMapping("/uploadtest")
public StorePath uploadFileTest() throws IOException {
File file = new File("E:\\Desktop\\demo3.docx");
// 设置文件信息
Set mataData = new HashSet<>();
mataData.add(new MetaData("author", "author"));
mataData.add(new MetaData("description", "附件上传"));
StorePath storePath = fastFileStorageClient.uploadFile(new FileInputStream(file), file.length(),
"docx", null);
return storePath;
}
/**
* 文件删除
*
* @param path group1/M00/00/00/wKj4RVvuZQWAAS5RAAJZc0IRPSY48.docx
* @return
*/
@DeleteMapping("/delete")
public String delete(String path) {
// 第一种删除:参数:完整地址
fastFileStorageClient.deleteFile(path);
// 第二种删除:参数:组名加文件路径
// fastFileStorageClient.deleteFile(group,path);
return "删除成功!";
}
/**
* 文件下载
*
* @param group group1
* @param path M00/00/01/wKj4RFvuZlOALpzMAAJZc0IRPSY16.docx
* @param fileName wKj4RFvuZlOALpzMAAJZc0IRPSY16.docx
* @param response
* @throws IOException
*/
@GetMapping("/download")
public void downLoad(@RequestParam String group, @RequestParam String path, @RequestParam String fileName,
HttpServletResponse response) throws IOException {
// 获取文件
byte[] bytes = fastFileStorageClient.downloadFile(group, path, new DownloadByteArray());
//设置相应类型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();
}
}
https://github.com/tobato/FastDFS_Client
https://blog.csdn.net/makelovewith/article/details/82666062