springboot集成fastDfs过程代码实例
作者:青竹玉简 时间:2023-02-21 19:42:20
这篇文章主要介绍了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
<em id="__mceDel">FastDfsClientUtil工具类<br></em>
@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;
}
}
来源:https://www.cnblogs.com/matd/p/11843928.html
标签:spring,boot,集成,fastDfs
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java FineReport报表工具导出EXCEL的四种方式
2021-07-20 02:58:17
![](https://img.aspxhome.com/file/2023/1/67451_0s.png)
关于MD5算法原理与常用实现方式
2023-03-18 11:09:04
![](https://img.aspxhome.com/file/2023/8/62828_0s.png)
Springboot 项目读取Resources目录下的文件(推荐)
2023-07-27 09:40:47
![](https://img.aspxhome.com/file/2023/9/57699_0s.png)
详解Lombok安装及Spring Boot集成Lombok
2023-11-28 23:39:55
![](https://img.aspxhome.com/file/2023/9/60369_0s.png)
Spring Boot小型项目如何使用异步任务管理器实现不同业务间的解耦
2022-11-21 07:49:54
![](https://img.aspxhome.com/file/2023/3/69123_0s.png)
java处理数据库不支持的emoji表情符问题解决
2021-08-21 00:16:58
详解java IO流之缓冲流的使用
2023-08-08 18:33:16
Java模拟死锁发生之演绎哲学家进餐问题案例详解
2022-10-09 22:52:44
java微信公众号支付开发之现金红包
2023-09-01 17:28:38
![](https://img.aspxhome.com/file/2023/3/66163_0s.jpg)
Spring Boot项目使用Flyway的详细教程
2021-12-19 12:05:58
![](https://img.aspxhome.com/file/2023/5/70305_0s.png)
Java 超详细讲解ThreadLocal类的使用
2021-11-13 05:58:29
![](https://img.aspxhome.com/file/2023/2/71012_0s.png)
java实现单人版五子棋游戏
2021-09-03 03:24:20
![](https://img.aspxhome.com/file/2023/0/63060_0s.png)
Spring Security和Shiro的相同点与不同点整理
2023-01-15 17:07:20
Springcloud seata nacos环境搭建过程图解
2022-11-15 00:34:14
![](https://img.aspxhome.com/file/2023/1/61341_0s.png)
Java反射获取class对象方式解析
2023-04-22 15:51:30
EasyExcel实现导入+各种数据校验功能
2022-05-12 13:05:26
![](https://img.aspxhome.com/file/2023/1/71921_0s.png)
在启动后台 jar包时,使用指定的 application.yml操作
2023-01-08 20:10:12
Java 抽象类特点总结
2023-07-28 10:39:46
![](https://img.aspxhome.com/file/2023/2/66662_0s.png)
如何优雅的处理Spring Boot异常信息详解
2023-11-29 09:50:02
![](https://img.aspxhome.com/file/2023/6/60536_0s.jpg)
不看后悔!揭秘游戏服务器开发
2023-08-22 16:20:30
![](https://img.aspxhome.com/file/2023/6/58306_0s.png)