FastDFS与Springboot集成
来源:互联网 发布:大数据的特点是什么 编辑:程序博客网 时间:2024/06/04 22:46
原文链接:http://blog.csdn.net/xyang81/article/details/52850667
上一篇《FastDFS分布文件系统Java客户端使用》基于官方提供的Java客户端库介绍了文件上传、下载和删除的功能。淘宝在今年9月份在官方Java客户端的基础上进行了大量重构,且提供了更多丰富的api,主要新增的特性如下:
1> 对关键部分代码加入了单元测试,便于理解与服务端的接口交易,提高接口质量
2> 将以前对byte硬解析风格重构为使用 对象+注解 的形式,尽量增强了代码的可读性
3> 支持对服务端的连接池管理(commons-pool2)
4> 支持上传图片时候检查图片格式,并且自动生成缩略图
5> 和Springboot整合方便
源码下载地址:https://github.com/tobato/FastDFS_Client
整合到Springboot项目流程
注意:必须是Springboot项目
1、添加pom依赖
<dependency> <groupId>com.github.tobato</groupId> <artifactId>fastdfs-client</artifactId> <version>1.25.2-RELEASE</version></dependency>
2、将Fdfs配置引入项目
我将注解配置加在springboot的入口类中:@Import(FdfsClientConfig.class)
@Import(FdfsClientConfig.class)@SpringBootApplicationpublic class JingtongApplication { public static void main(String[] args) { SpringApplication.run(JingtongApplication.class, args); }}
3、在spring配置文件中加入fdfs相关配置
根据项目当中使用配置文件类型(.yml和.properties选择其中一个),加入相应的配置。
application.yml
fdfs: soTimeout: 1500 connectTimeout: 600 thumbImage: #缩略图生成参数 width: 150 height: 150 trackerList: #TrackerList参数,支持多个 - 192.168.0.201:22122 - 192.168.0.202:22122
application.properties
fdfs.soTimeout=1500fdfs.connectTimeout=600fdfs.thumbImage.width=150fdfs.thumbImage.height=150fdfs.trackerList[0]=192.168.0.201:22122fdfs.trackerList[1]=192.168.0.202:22122
4、在项目中使用
客户端主要包括以下接口:
TrackerClient - TrackerServer接口
GenerateStorageClient - 一般文件存储接口 (StorageServer接口)
FastFileStorageClient - 为方便项目开发集成的简单接口(StorageServer接口)
AppendFileStorageClient - 支持文件续传操作的接口 (StorageServer接口)
笔者在前一个项目当中将fdfs主要用于图片存储,基于FastFileStorageClient接口和springmvc提供的MultipartFile接口封装了一个简单的工具类,方便全局管理与调用。如下所示:
package com.digi_zones.support.fs;import com.digi_zones.config.AppConfig;import com.digi_zones.contacts.AppConstants;import com.github.tobato.fastdfs.domain.FileInfo;import com.github.tobato.fastdfs.domain.StorePath;import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;import com.github.tobato.fastdfs.service.FastFileStorageClient;import org.apache.commons.io.FilenameUtils;import org.apache.commons.lang3.StringUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import org.springframework.web.multipart.MultipartFile;import java.io.ByteArrayInputStream;import java.io.IOException;import java.nio.charset.Charset;/** * <p>Description: FastDFS文件上传下载包装类</p> * <p>Copyright: Copyright (c) 2016</p> * * @author 杨信 * @version 1.0 * @date 2016/9/7 */@Componentpublic class FastDFSClientWrapper { private final Logger logger = LoggerFactory.getLogger(FastDFSClientWrapper.class); @Autowired private FastFileStorageClient storageClient; @Autowired private AppConfig appConfig; // 项目参数配置 /** * 上传文件 * @param file 文件对象 * @return 文件访问地址 * @throws IOException */ public String uploadFile(MultipartFile file) throws IOException { StorePath storePath = storageClient.uploadFile(file.getInputStream(),file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()),null); return getResAccessUrl(storePath); } /** * 将一段字符串生成一个文件上传 * @param content 文件内容 * @param fileExtension * @return */ public String uploadFile(String content, String fileExtension) { byte[] buff = content.getBytes(Charset.forName("UTF-8")); ByteArrayInputStream stream = new ByteArrayInputStream(buff); StorePath storePath = storageClient.uploadFile(stream,buff.length, fileExtension,null); return getResAccessUrl(storePath); } // 封装图片完整URL地址 private String getResAccessUrl(StorePath storePath) { String fileUrl = AppConstants.HTTP_PRODOCOL + appConfig.getResHost() + ":" + appConfig.getFdfsStoragePort() + "/" + storePath.getFullPath(); return fileUrl; } /** * 删除文件 * @param fileUrl 文件访问地址 * @return */ public void deleteFile(String fileUrl) { if (StringUtils.isEmpty(fileUrl)) { return; } try { StorePath storePath = StorePath.praseFromUrl(fileUrl); storageClient.deleteFile(storePath.getGroup(), storePath.getPath()); } catch (FdfsUnsupportStorePathException e) { logger.warn(e.getMessage()); } }}
除了FastDFSClientWrapper类中用到的api,客户端提供的api还有很多,可根据自身的业务需求,将其它接口也添加到工具类中即可。如下所示:
// 上传文件,并添加文件元数据StorePath uploadFile(InputStream inputStream, long fileSize, String fileExtName, Set<MateData> metaDataSet);// 获取文件元数据Set<MateData> getMetadata(String groupName, String path);// 上传图片并同时生成一个缩略图StorePath uploadImageAndCrtThumbImage(InputStream inputStream, long fileSize, String fileExtName, Set<MateData> metaDataSet);// 。。。
在项目中使用FastDFSClientWrapper:
@Controllerpublic class MyController { @Autowired private FastDFSClientWrapper dfsClient; // 上传图片 @RequestMapping(value = "/upload", method = RequestMethod.POST) public String upload(MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws Exception { // 省略业务逻辑代码。。。 String imgUrl = dfsClient.uploadFile(file); // 。。。。 return xxxx; }}
- FastDFS与Springboot集成
- FastDFS与Springboot集成
- FastDFS与Springboot集成
- FastDFS与Springboot集成
- FastDFS与Springboot集成
- springboot集成fastdfs
- SpringBoot与Mybatis集成
- FastDFS NGINX集成与图片防盗
- Fastdfs与java集成使用过程
- Kafka 安装-配置-监控 与集成springboot
- SpringBoot应用与原理之集成Log4j2
- 随便谈谈RabbitMQ与springBoot进行集成。
- FastDFS实战(四)- Java集成FastDFS
- Maven +SpringBoot 集成 阿里大鱼 短信接口详解与Demo
- 使用SpringBoot之logback与logstash集成问题
- CentOS7下RabbitMq及与SpringBoot集成注意点
- springboot 集成hibernate 集成struts2
- springboot集成dubbo
- STM32 IAP 在线升级详解
- JAVA开发3--将开发的java项目导出为jar包
- c# 获取 摄像头 实现录像
- dubbo管理台的部署
- Understanding TCP/IP Network Stack & Writing Network Apps
- FastDFS与Springboot集成
- 子网寻址
- Facebook 发布新 Node 模块管理器 Yarn,或将取代 npm 客户端
- framework快速开发平台
- html5绘制动态矩形
- php 正则匹配中文及中文字符
- [Windows批处理]清除IE缓存
- 近发现了一个很诡异的NullPointerException
- Android学习路线图