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的更多相关文章

  1. SpringBoot集成FastDFS+Nginx整合基于Token的防盗链

    为什么要用SpringBoot? SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人 ...

  2. SpringBoot集成FastDFS依赖实现文件上传

    前言 对FastDFS文件系统安装后的使用. FastDFS的安装请参考这篇:Docker中搭建FastDFS文件系统(多图) 本文环境:IDEA + JDK1.8 + Maven 本文项目代码:ht ...

  3. (转)Spring Boot(十八):使用 Spring Boot 集成 FastDFS

    http://www.ityouknow.com/springboot/2018/01/16/spring-boot-fastdfs.html 上篇文章介绍了如何使用 Spring Boot 上传文件 ...

  4. SpringBoot2.0集成FastDFS

    SpringBoot2.0集成FastDFS 前两篇整体上介绍了通过 Nginx 和 FastDFS 的整合来实现文件服务器.但是,在实际开发中对图片或文件的操作都是通过应用程序来完成的,因此,本篇将 ...

  5. 【springBoot】springBoot集成redis的key,value序列化的相关问题

    使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...

  6. SpringBoot集成security

    本文就SpringBoot集成Security的使用步骤做出解释说明.

  7. springboot集成Actuator

    Actuator监控端点,主要用来监控与管理. 原生端点主要分为三大类:应用配置类.度量指标类.操作控制类. 应用配置类:获取应用程序中加载的配置.环境变量.自动化配置报告等与SpringBoot应用 ...

  8. SpringBoot集成Shiro并用MongoDB做Session存储

    之前项目鉴权一直使用的Shiro,那是在Spring MVC里面使用的比较多,而且都是用XML来配置,用Shiro来做权限控制相对比较简单而且成熟,而且我一直都把Shiro的session放在mong ...

  9. SpringBoot集成redis的key,value序列化的相关问题

    使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...

随机推荐

  1. CSS 交集选择器和并集选择器

    交集选择器是and 也就是要同时满足 且只能交2个只能交2个只能交2个,第一个是标记,第二个是class或者id,之间不可以有空格 eg:  span.small-height 并集选择器是or,也就 ...

  2. Java 面向对象(七)

    枚举 枚举的引入(模拟枚举) class Student { private int restDay; public int getRestDay() { return restDay; } publ ...

  3. Flutter移动电商实战 --(52)购物车_数据模型建立和Provide修改

    根据json数据生成模型类 {"goodsId":"2171c20d77c340729d5d7ebc2039c08d","goodsName" ...

  4. 如何git revert merge commit?

    答: git revert -m <parent-number> <commit-id> (适用于merge操作的commit) 参考资料: https://blog.csdn ...

  5. <JavaScript>使用onmousemove事件实现移动(拖拽)div 出现的关于offsetX的问题

    出现的问题如下图所示(截屏看不出来看log) 再移动鼠标的过程中会不断的出现异常值导致拖动的div不断切换位置,回到左上角. 我以为是冒泡机制导致的所以添加了下面一段阻止冒泡,随便也阻止了默认事件,但 ...

  6. MySQL VARCHAR字段最大长度到底是多少

    MySQL VARCHAR字段最大长度到底是多少   varchar(n),n表示什么? MySQL5.0.3之前varchar(n)这里的n表示字节数 MySQL5.0.3之后varchar(n)这 ...

  7. javascript的promise

  8. c#子线程执行完怎么通知主线程(转)

    定义一个委托实现回调函数 public delegate void CallBackDelegate(string message); 程序开始的时候 //把回调的方法给委托变量 CallBackDe ...

  9. Mac下载工具软件提示损坏

    今天装Navicat的时候一直报错文件损坏,最后请教别人才知道,这里记录下: 原因: Mac默认不允许任何来源的软件安装,安全问题,需要我们设置下即可: 解决方法: 方法一: 方法二: 终端输入命令: ...

  10. 【Leetcode_easy】686. Repeated String Match

    problem 686. Repeated String Match solution1: 使用string类的find函数: class Solution { public: int repeate ...