springboot集成fastdfs
来源:互联网 发布:mac怎么输入省略号 编辑:程序博客网 时间:2024/06/07 14:14
1、配置pom.xml
<dependency><groupId>com.github.tobato</groupId><artifactId>fastdfs-client</artifactId><version>1.25.2-RELEASE</version></dependency>
2、配置application.properties
fdfs.soTimeout=1500fdfs.connectTimeout=600fdfs.thumbImage.width=150fdfs.thumbImage.height=200fdfs.trackerList[0]=ip:port
fdfs.trackerList[1]=ip:port##这个一定要有,比较坑spring.jmx.enabled=false3、applicatio.java
引入 @Import(FdfsClientConfig.class)
@Import(FdfsClientConfig.class)@SpringBootApplicationpublic class FileApplication {public static void main(String[] args) {SpringApplication.run(FileApplication.class, args);}}
通过以上三步就能成功集成
以下是我封装的一些API,
有 上传文件,上传图片及缩略图,删除,装载路径
fastdfs官方提供的api中下载方法无法满足作者系统业务,因此没有采取,但是可以通过api拿到文件 http路径,再进行下载
package fmc.file.clint;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import org.apache.commons.io.FilenameUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Configuration;import org.springframework.util.StringUtils;import org.springframework.web.multipart.MultipartFile;import com.github.tobato.fastdfs.domain.StorePath;import com.github.tobato.fastdfs.service.FastFileStorageClient;import fmc.file.model.util.FdfsInfo;import fmc.file.model.util.NginxInfo;import fmc.file.utils.FileUtil;/** * fdfs API封装类 * @author Administrator * */@Configurationpublic class FdfsClient {@Autowired private FastFileStorageClient storageClient;@Autowiredprivate NginxInfo nginxInfo;@Autowiredprivate FdfsInfo fdfsInfo;/*@Autowiredprivate FileUtil fileUtil;*//** * 上传文件 适用于所有文件 * @param file 文件对象 * @return 文件访问地址 * @throws IOException */public StorePath uploadFile(MultipartFile file) throws IOException{StorePath storePath = storageClient.uploadFile(file.getInputStream(),file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()),null); return storePath; }/** * 将一段字符串生成一个文件上传 * @param content 文件路径 * @return文件访问地址 * @throws FileNotFoundException */@SuppressWarnings("resource")public StorePath uploadFile(String content) throws FileNotFoundException{ if(!StringUtils.hasText(content)){ throw new NullPointerException(); } File file = new File(content);FileInputStream inputStream=new FileInputStream(file);String fileName=file.getName();//获取文件后缀名String strs= FileUtil.getExtName(fileName);if(!StringUtils.hasText(strs)){throw new NullPointerException(); } StorePath storePath = storageClient.uploadFile(inputStream,file.length(),strs,null); return storePath; }/** * 传图片并同时生成一个缩略图 * "JPG", "JPEG", "PNG", "GIF", "BMP", "WBMP" * @param file 文件对象 * @return 文件访问地址 * @throws IOException */public StorePath uploadImageAndCrtThumbImage(MultipartFile file) throws IOException{ StorePath storePath = storageClient.uploadImageAndCrtThumbImage(file.getInputStream(),file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()),null); return storePath; }/** * 传图片并同时生成一个缩略图 * "JPG", "JPEG", "PNG", "GIF", "BMP", "WBMP" * @param content 文件路径 * @return 文件访问地址 * @throws FileNotFoundException */@SuppressWarnings("resource")public StorePath uploadImageAndCrtThumbImage(String content) throws FileNotFoundException{if(!StringUtils.hasText(content)){ throw new NullPointerException(); }File file = new File(content);FileInputStream inputStream=new FileInputStream(file);String fileName=file.getName();//获取文件后缀名String strs= FileUtil.getExtName(fileName);if(!StringUtils.hasText(strs)){throw new NullPointerException(); } StorePath storePath = storageClient.uploadImageAndCrtThumbImage(inputStream,file.length(),strs,null); return storePath; }/** * 封装完整文件地址 * @param fullPah 文件路径 * @return 文件访问地址 */public String getFilePath(String fullPath) {if(!StringUtils.hasText(fullPath)){throw new NullPointerException();}String ngLoc = nginxInfo.getBaseUrl();StringBuffer filePath = new StringBuffer(ngLoc);filePath.append("/");filePath.append(fullPath);return filePath.toString();}/** * 装载缩略图名称 * "JPG", "JPEG", "PNG", "GIF", "BMP", "WBMP" * @param storePath 文件路径 * @return 缩略图名称 */public String getThumbImage(String storePath){if(!StringUtils.hasText(storePath)){ throw new NullPointerException(); }//获取文件名称String pathL = FileUtil.getFileName(storePath) + "_" + fdfsInfo.getSize();//获取文件后缀名String pathR = FileUtil.getExtName(storePath);return pathL + "." + pathR;}/** * 删除文件 * @param fileUrl 文件访问地址 * @return * @throws IOException */ public void deleteFile(String storePath){ if (!StringUtils.hasText(storePath)) { throw new NullPointerException(); } storageClient.deleteFile(storePath); }}
阅读全文
0 0
- FastDFS与Springboot集成
- springboot集成fastdfs
- FastDFS与Springboot集成
- FastDFS与Springboot集成
- FastDFS与Springboot集成
- FastDFS与Springboot集成
- FastDFS实战(四)- Java集成FastDFS
- springboot 集成hibernate 集成struts2
- springboot集成dubbo
- SpringBoot集成Swagger
- SpringBoot之集成SpringDataRedis
- springBoot集成mybatis
- Springboot集成swagger2
- springboot集成dubbo
- SpringBoot-SpringData-JPA集成
- SpringBoot-thymeleaf模板集成
- SpringBoot-SpringSecurity集成
- SpringBoot集成mybatis
- 创建、编译ros package
- WebView里面调用打电话
- lintcode 整数排序 | 希尔排序算法(Java)
- 斐波那契数列的余数周期问题
- SSH之传统方式接入分布式会话(Spring Session)篇
- springboot集成fastdfs
- Find All Numbers Disappeared in an Array(哈希表)
- Eclipse 各个版本区别
- c++中的类型转换方法
- Android开发大牛们的博客地址
- Elasticsearch安装与测试
- Web概述、HTML概述 、文本处理 、图像和超链接 、表格 、 表单
- 虚拟机对象概述
- hive(1.2.2)运行的一些错误(不定期更新)