springboot 集成fastDfs
pom.xml 引入依赖
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.1-RELEASE</version>
</dependency>
application.properties 配置
# fastDfs配置
fdfs.connect-timeout=600
fdfs.so-timeout=1500
fdfs.trackerList=192.168.1.207:22122
fdfs.thumbImage.height=150
fdfs.thumbImage.width=150
spring.jmx.enabled=false
fdfs.pool.max-total=200
storage.resHost=http://192.168.1.207/
storage.resPort=8888
DfsAutoConfig.java 自动注入
@Configuration
@Import(FdfsClientConfig.class)
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class DfsAutoConfig { }
DfsResConfig 配置映射关系
@Data
@Component
@ConfigurationProperties("storage")
public class DfsResConfig { private String resHost;
private String resPort;
}F
FastDfsClientUtil 工具类
@Slf4j
@Component
public class FastDfsClientUtil { @Autowired
private FastFileStorageClient storageClient; /**
* @Author AlanMa
* @Description MultipartFile类型的文件上传ַ
* @Date 2019/11/12
* @Param [file]
* @return com.hiynn.data.visual.file.vo.ResultData<java.lang.String>
*/
public ResultData<String> uploadFile(MultipartFile file){ try{
StorePath path = storageClient.uploadFile(file.getInputStream(), file.getSize(),
FilenameUtils.getExtension(file.getOriginalFilename()), null);
return ResultDataUtil.setSuccessResult(path.getFullPath());
}catch (Exception e){
e.printStackTrace();
return ResultDataUtil.setFailedResult();
} } /**
* @Author AlanMa
* @Description 普通的文件上传
* @Date 2019/11/12
* @Param [file]
* @return com.hiynn.data.visual.file.vo.ResultData<java.lang.String>
*/
public ResultData<String> uploadFile(File file){ try{
FileInputStream inputStream = new FileInputStream(file);
StorePath path = storageClient.uploadFile(inputStream, file.length(),
FilenameUtils.getExtension(file.getName()), null);
return ResultDataUtil.setSuccessResult(path.getFullPath());
}catch (Exception e){
e.printStackTrace();
return ResultDataUtil.setFailedResult();
}
} /**
* @Author AlanMa
* @Description 带输入流形式的文件上传
* @Date 2019/11/12
* @Param [is, size, fileName]
* @return com.hiynn.data.visual.file.vo.ResultData<java.lang.String>
*/
public ResultData<String> uploadFileStream(InputStream is, long size, String fileName) { StorePath path = storageClient.uploadFile(is, size, fileName, null);
return ResultDataUtil.setSuccessResult(path.getFullPath());
} /**
* @Author AlanMa
* @Description 将一段文本文件写到fastdfs的服务器上
* @Date 2019/11/12
* @Param [content, fileExtension]
* @return java.lang.String
*/
public String uploadFile(String content, String fileExtension) {
byte[] buff = content.getBytes(Charset.forName("UTF-8"));
ByteArrayInputStream stream = new ByteArrayInputStream(buff);
StorePath path = storageClient.uploadFile(stream, buff.length, fileExtension, null);
return path.getFullPath();
} /**
* @Author AlanMa
* @Description 删除文件
* @Date 2019/11/12
* @Param [fileUrl]
* @return com.hiynn.data.visual.file.vo.ResultData
*/
public ResultData deleteFile(String fileUrl) { if (StringUtils.isEmpty(fileUrl)) {
return ResultDataUtil.setFailedResult();
}
try {
StorePath storePath = StorePath.praseFromUrl(fileUrl);
storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
return ResultDataUtil.setSuccessResult();
} catch (FdfsUnsupportStorePathException e) {
e.printStackTrace();
log.warn(e.getMessage());
return ResultDataUtil.setFailedResult();
}
}
//
// /**
// * @Author AlanMa
// * @Description 上传文件图片
// * @Date 2019/11/12
// * @Param [is, size, fileExtName, metaData]
// * @return java.lang.String
// */
// public String upfileImage(InputStream is, long size, String fileExtName, Set<MateData> metaData) {
// StorePath path = storageClient.uploadImageAndCrtThumbImage(is, size, fileExtName, metaData);
// return path.getFullPath();
// }
}
测试
@Slf4j
@RestController
@RequestMapping("/dfs")
public class FileDfsController extends BaseController { @Autowired
private FastDfsClientUtil fastDfsClientUtil; @Autowired
private DfsResConfig dfsResConfig; @PostMapping("/single")
public ResultData singleUpload(@RequestParam("file") MultipartFile file){
ResultData<String> resultData = fastDfsClientUtil.uploadFile(file);
if (Objects.equals(ResultEnum.SUCCESS.getCode(), resultData.getCode())) {
String url = String.format("%s:%s/%s",dfsResConfig.getResHost(),dfsResConfig.getResPort(),resultData.getData());
return ResultDataUtil.setSuccessResult(url);
}
return resultData; }
}
springboot 集成fastDfs的更多相关文章
- SpringBoot集成FastDFS+Nginx整合基于Token的防盗链
为什么要用SpringBoot? SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人 ...
- SpringBoot集成FastDFS依赖实现文件上传
前言 对FastDFS文件系统安装后的使用. FastDFS的安装请参考这篇:Docker中搭建FastDFS文件系统(多图) 本文环境:IDEA + JDK1.8 + Maven 本文项目代码:ht ...
- (转)Spring Boot(十八):使用 Spring Boot 集成 FastDFS
http://www.ityouknow.com/springboot/2018/01/16/spring-boot-fastdfs.html 上篇文章介绍了如何使用 Spring Boot 上传文件 ...
- SpringBoot2.0集成FastDFS
SpringBoot2.0集成FastDFS 前两篇整体上介绍了通过 Nginx 和 FastDFS 的整合来实现文件服务器.但是,在实际开发中对图片或文件的操作都是通过应用程序来完成的,因此,本篇将 ...
- 【springBoot】springBoot集成redis的key,value序列化的相关问题
使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...
- SpringBoot集成security
本文就SpringBoot集成Security的使用步骤做出解释说明.
- springboot集成Actuator
Actuator监控端点,主要用来监控与管理. 原生端点主要分为三大类:应用配置类.度量指标类.操作控制类. 应用配置类:获取应用程序中加载的配置.环境变量.自动化配置报告等与SpringBoot应用 ...
- SpringBoot集成Shiro并用MongoDB做Session存储
之前项目鉴权一直使用的Shiro,那是在Spring MVC里面使用的比较多,而且都是用XML来配置,用Shiro来做权限控制相对比较简单而且成熟,而且我一直都把Shiro的session放在mong ...
- SpringBoot集成redis的key,value序列化的相关问题
使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...
随机推荐
- 使用C#代码调用Web API
1. POST POST的参数需要加上[FromBady],且参数只能一个 客户端提交数据的时候ContentType 为 application/x-www-form-urlencoded 或者 a ...
- 如何在虚拟机中安装kali linux
整理笔记,把以前印象笔记中记录的一些东西翻出来,想想发个随笔吧. 第一步在官网下载kali linux的镜像. 网址:https://www.kali.org/downloads/ (我的电脑是64位 ...
- Web安全测试 — 手工安全测试方法&修改建议
常见问题 1.XSS(CrossSite Script)跨站脚本攻击 XSS(CrossSite Script)跨站脚本攻击.它指的是恶意攻击者往Web 页面里插入恶意 html代码,当用户浏览该页之 ...
- Python数据预处理(sklearn.preprocessing)—归一化(MinMaxScaler),标准化(StandardScaler),正则化(Normalizer, normalize)
关于数据预处理的几个概念 归一化 (Normalization): 属性缩放到一个指定的最大和最小值(通常是1-0)之间,这可以通过preprocessing.MinMaxScaler类实现. 常 ...
- 使用IOCP完成端口队列做任务队列
使用IOCP完成端口队列做任务队列 与其自己费力设计异步任务队列,不如使用WINDOWS内核级的IOCP完成端口队列做任务队列. 1)引用单元 uses windows; 2)定义完成端口句柄 var ...
- OpenSL ES: 利用OpenSL ES播放一个存在于SDcard上的PCM文件
native-lib.cpp #include <jni.h> #include <string> #include <SLES/OpenSLES.h> #incl ...
- [C#]加密解密 MD5、AES
/// <summary> /// MD5函数 /// </summary> /// <param name="str">原始字符串</p ...
- [webpack]深入理解proxy代理
1.一个基本的代理 module.exports = { //... devServer: { proxy: { '/api': 'http://localhost:3000' } } }; /api ...
- docker之redis使用
#拉取redis > docker pull redis:latest latest: Pulling from library/redis 8d691f585fa8: Pull complet ...
- 运行时给java对象动态的属性赋值
运行时给java对象动态的属性赋值 如何给java对象动态的属性赋值(也就是在代码执行的时候才决定给哪个属性赋值) 1.自定义一个工具类ReflectHelper,代码如下所示: pa ...