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; }
}

  

最新文章

  1. ABAP游标的使用
  2. SpringMVC中如何在网站启动、结束时执行代码(详细,确保可用)
  3. android -- 之PopupWindow的使用
  4. webapi 获取请求参数
  5. 006--VS2013 C++ 加载其他格式图片,并显示半透明化
  6. 20141128--JavaScript HTML DOM
  7. mfc非模态对话框
  8. 常用的Linux操作命令(一)
  9. Nlog的简单使用
  10. (总结)Linux的chattr与lsattr命令详解
  11. Hex to Int 【十六进制转十进制】
  12. Netty实战 - 1. 基本概念
  13. 配置docker官方源并用yum安装docker
  14. wcf 数值类型赋值不能的问题解决
  15. air报错 Error: Error #3000: Illegal path name
  16. Android——开源框架Universal-Image-Loader + Fragment使用+轮播广告
  17. java所搜引擎slor学习笔记(一)
  18. 一个Linux下C线程池的实现
  19. FastReport.Net使用:[17]线(Line)控件使用
  20. EasyPlayerPro安卓流媒体播放器实现Android H.265硬解码流程

热门文章

  1. 说出Servlet的生命周期,并说出Servlet和CGI的区别。
  2. nginx 动态黑名单
  3. Python 学习随笔 - 2 - list 、tuple 、dict、set 特殊数据类型 及 实际应用
  4. Mac搭建C语言环境
  5. HTML中调用带有SoapHeader头的WebService的两种方法
  6. poj2456
  7. CV1——学习笔记
  8. &lt;JavaScript&gt;调用apply报错:CreateListFromArrayLike called on non-object;
  9. Android Studio创建Module-库模块
  10. NULL和nullptr