FastDFS-分布式文件管理系统:
FastDFS是一个开源的轻量级分布式文件系统,它对文件进行管理,功能包括:文件存储、文件同步、文件访问(文件上传、文件下载)等,解决了大容量存储和负载均衡的问题。特别适合以文件为载体的在线服务,如相册网站、视频网站等等。
快速起步:
1:参考资料文件夹下:fastdfs安装,并启动
2: 环境搭建
(1)新建项目:fastdfs-demo,导入依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--fastdfs-->
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.5</version>
</dependency>
</dependencies>
(2)编写配置信息 : 创建 fast_dfs.properties文件
#socket连接超时时长
fdfs.soTimeout=1500
#连接tracker服务器超时时长
fdfs.connectTimeout=600
fdfs.trackerList=192.168.200.129:22122
fileServerUrl=http://192.168.200.129/
(3)引导类
package com.itheima.fastdfs;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class FastDFSApplication {
public static void main(String[] args) {
SpringApplication.run(FastDFSApplication.class,args);
}
}
(4)fastdfs封装配置类
4.1:新建配置类:com.ding.fastdfs.config.FdfsConfiguration
package com.ding.fastdfs.config;
import com.github.tobato.fastdfs.FdfsClientConfig;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.context.annotation.Import;
import org.springframework.jmx.support.RegistrationPolicy;
@Configuration
@PropertySource("fast_dfs.properties")
@Import(FdfsClientConfig.class) // 导入FastDFS-Client组件
public class FdfsConfiguration {
}
4.2:封装fastdfs客户端:com.ding.fastdfs.config.FastDFSClientUtil
也就是编写fastDFS的工具类
package com.ding.fastdfs.config;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.proto.storage.DownloadCallback;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
@Component
public class FastDFSClientUtil {
@Autowired
private FastFileStorageClient storageClient;
/**
* 上传
* @param file
* @return
* @throws IOException
*/
public String uploadFile(MultipartFile file) throws IOException {
StorePath storePath = storageClient.uploadFile((InputStream) file.getInputStream(), file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()), null);
return storePath.getFullPath();
}
/**
* 删除
* @param filePath
*/
public void delFile(String filePath) {
storageClient.deleteFile(filePath);
}
/**
* 下载
* @param groupName
* @param path
* @return
*/
public byte[] download(String groupName, String path) throws IOException {
InputStream ins = storageClient.downloadFile(groupName, path, new DownloadCallback<InputStream>() {
@Override
public InputStream recv(InputStream ins) throws IOException {
// 将此ins返回给上面的ins
return ins;
}
});
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buff = new byte[100];
int rc = 0;
while ((rc = ins.read(buff, 0, 100)) > 0) {
byteArrayOutputStream.write(buff, 0, rc);
}
return byteArrayOutputStream.toByteArray();
}
}
5:测试 上传
新建controller测试上传:com.ding.fastdfs.controller.FileController
package com.ding.fastdfs.controller;
import com.itheima.fastdfs.config.FastDFSClientUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@RestController
@RequestMapping("/file")
public class FileController {
@Autowired
private FastDFSClientUtil fastDFSClientUtil;
@Value("${fileServerUrl}")
private String fileServerUrl ;
@PostMapping("/upload")
public String uploadFile(MultipartFile file) {
try {
//判断文件是否存在
if (file == null) {
throw new RuntimeException("文件不存在");
}
//获取文件的完整名称
String originalFilename = file.getOriginalFilename();
if (StringUtils.isEmpty(originalFilename)) {
throw new RuntimeException("文件不存在");
}
String url = fastDFSClientUtil.uploadFile(file); //这步就是最核心的步骤
return fileServerUrl+url;
} catch (Exception e) {
e.printStackTrace();
}
return "文件上传失败";
}
}