FastDFS实战(五)- Spring Boot集成FastDFS
来源:互联网 发布:知乎禁止转载怎么复制 编辑:程序博客网 时间:2024/06/06 20:48
源码下载地址:https://github.com/tobato/FastDFS_Client
一、 创建Spring Boot项目,并导入fastdfs依赖
<!--fastdfs 客户端依赖--><dependency> <groupId>com.github.tobato</groupId> <artifactId>fastdfs-client</artifactId> <version>1.25.2-RELEASE</version></dependency>
二、配置
入口类
package com.baizhi;import com.github.tobato.fastdfs.FdfsClientConfig;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.EnableMBeanExport;import org.springframework.context.annotation.Import;import org.springframework.jmx.support.RegistrationPolicy;@Import(FdfsClientConfig.class)// 解决jmx重复注册bean的问题@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)@SpringBootApplicationpublic class FastdfsClientApplication { public static void main(String[] args) { SpringApplication.run(FastdfsClientApplication.class, args); }}
application.yml
# =========================================================# 分布式文件系统FDFS配置# =========================================================fdfs: soTimeout: 30 connectTimeout: 60 thumbImage: #缩略图生成参数 width: 150 height: 150 trackerList: #TrackerList参数,支持多个 - 192.168.128.141:22122 pool: max-total: 100 max-wait-millis: 60
测试
package com.baizhi;import com.github.tobato.fastdfs.domain.GroupState;import com.github.tobato.fastdfs.domain.StorePath;import com.github.tobato.fastdfs.proto.storage.DownloadByteArray;import com.github.tobato.fastdfs.service.FastFileStorageClient;import com.github.tobato.fastdfs.service.TrackerClient;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;import java.io.*;import java.util.List;@RunWith(SpringRunner.class)@SpringBootTestpublic class FastdfsClientApplicationTests { @Autowired private FastFileStorageClient fastFileStorageClient; @Autowired private TrackerClient trackerClient; @Test public void contextLoads() { List<GroupState> groupStates = trackerClient.listGroups(); for (GroupState groupState : groupStates) { System.out.println(groupState); } } /** * 测试文件上传 */ @Test public void upload() { try { File file = new File("d:\\ds.jpg"); FileInputStream inputStream = new FileInputStream(file); //StorePath storePath = fastFileStorageClient.uploadFile(inputStream, file.length(), "jpg", null); //fastFileStorageClient.uploadSlaveFile(storePath.getGroup(),storePath.getPath(),inputStream,inputStream.available(),"a_",null); fastFileStorageClient.uploadSlaveFile("group1","M00/00/00/wKiAjVlpNjiAK5IHAADGA0F72jo578.jpg",inputStream,inputStream.available(),"a_",null); //System.out.println(storePath.getGroup() + " " + storePath.getPath()); inputStream.close(); } catch (Exception e) { e.printStackTrace(); } } /** * 测试上传缩略图 */ @Test public void uploadCrtThumbImage() { try { File file = new File("d:\\ds.jpg"); FileInputStream inputStream = new FileInputStream(file); // 测试上传 缩略图 StorePath storePath = fastFileStorageClient.uploadImageAndCrtThumbImage(inputStream, file.length(), "jpg", null); System.out.println(storePath.getGroup() + " " + storePath.getPath()); } catch (FileNotFoundException e) { e.printStackTrace(); } } /** * 测试文件下载 */ @Test public void download() { try { byte[] bytes = fastFileStorageClient.downloadFile("group1", "M00/00/00/wKiAjVlpMfiAagnbAADGA0F72jo134_150x150.jpg", new DownloadByteArray()); FileOutputStream stream = new FileOutputStream("a.jpg"); stream.write(bytes); } catch (IOException e) { e.printStackTrace(); } } /** * 测试文件删除 */ @Test public void deleteFile(){ fastFileStorageClient.deleteFile("group1","M00/00/00/wKiAjVlpQVyARpQwAADGA0F72jo566.jpg"); }}
阅读全文
0 0
- FastDFS实战(五)- Spring Boot集成FastDFS
- FastDFS实战(四)- Java集成FastDFS
- spring boot 连接fastDFS
- FastDFS实战(一)- 介绍
- FastDFS实战(二)- 安装
- spring boot与fastdfs的整合使用
- FastDFS安装并 spring 集成(包含 Nginx)
- 【FastDFS专题】fastdfs使用实战(概念篇)
- 【FastDFS专题】fastdfs使用实战(安装篇)
- 【FastDFS专题】fastdfs使用实战(配置篇)
- 【FastDFS专题】fastdfs使用实战(使用篇)
- 【FastDFS专题】fastdfs使用实战(概念篇)
- 【FastDFS专题】fastdfs使用实战(概念篇)
- 分布式文件存储FastDFS(五)FastDFS常用命令总结
- fastdfs
- fastdfs
- FastDFS
- FastDFS
- Mtalab 运行问题:java.lang.OutOfMemoryError:Java heap space
- LA4487 Exclusive-OR (加权并查集)
- 安装composer
- 计算机网络:TCP套接字通信
- hdu 4366
- FastDFS实战(五)- Spring Boot集成FastDFS
- MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)的理解
- 方法和函数的区别
- 正则表达式之问号
- 各大公司Java后端开发面试题总结
- SOLDI是面向对象的5大原则
- Java易错知识点
- django中除了input,a 的普通标签发送ajax的post与get的区别
- java8新增的日期,时间包以及格式器