您的位置:首页 > 编程语言 > Java开发

FastDFS实战(五)- Spring Boot集成FastDFS

2017-07-15 16:45 411 查看
源码下载地址: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)

@SpringBootApplication
public 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)
@SpringBootTest
public 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");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: