FastDFS SpringBoot 客户端 Demo搭建,支持文件上传下载
一、准备 fastdfs-client-java 依赖包
1、从 Git 下载 FastDFS java client SDK 源码
https://github.com/happyfish100/fastdfs-client-java.git
<fastdfs-client-java 源码见附件>
2、使用ant从源码构建
ant clean package
3、使用maven从源码安装
mvn clean install
4、使用maven从jar文件安装
mvn install:install-file -DgroupId=org.csource -DartifactId=fastdfs-client-java -Dversion=${version} -Dpackaging=jar -Dfile=fastdfs-client-java-${version}.jar
5、在Demo的maven项目pom.xml中添加依赖
<dependency><groupId>org.csource</groupId><artifactId>fastdfs-client-java</artifactId><version>1.27-SNAPSHOT</version>
</dependency>
二、Demo搭建
1、POM配置,添加相关依赖
<dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><dependency><groupId>org.csource</groupId><artifactId>fastdfs-client-java</artifactId><version>1.27-SNAPSHOT</version></dependency><!-- fastdfs --><dependency><groupId>com.github.tobato</groupId><artifactId>fastdfs-client</artifactId><version>1.27.2</version></dependency>
2、FastdfsConfig
package com.ty.fdfs;import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;@Data
@Component
@ConfigurationProperties(prefix = "fdfs")
public class FastdfsConfig
{/*** fastdfs对外域名*/private String outurl;
}
3、FastDFSService
package com.ty.fdfs;import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;import javax.annotation.Resource;
import java.io.IOException;@Slf4j
@Service
public class FastDFSService {@Resourceprivate FastDFSClientWrapper fastDFSClientWrapper ;@Resourceprivate FastdfsConfig fastdfsConfig ;public String uploadFile(MultipartFile file){try {byte[] bytes = file.getBytes();String originalFileName = file.getOriginalFilename();String extension = originalFileName.substring(originalFileName.lastIndexOf(".") + 1);String fileName = file.getName();long fileSize = file.getSize();log.info("文件上传文件属性[originalFileName:{},fileName:{},fileSize:{},extension:{}, bytes.lengt:{}]",originalFileName,fileName,fileSize,extension,bytes.length);String url = fastDFSClientWrapper.uploadFile(bytes, fileSize, extension);String resultUrl = fastdfsConfig.getOuturl() + url;log.info("文件地址:{}",resultUrl);return resultUrl;} catch (IOException e) {log.error("fastdfs上传文件失败,{}",e);}return null;}/*** 下载文件** @param fileUrl 文件URL* @return 文件字节* @throws IOException*/public byte[] downloadFile(String fileUrl) throws IOException {byte[] bytes = fastDFSClientWrapper.downloadFile(fileUrl);return bytes;}/*** 下载文件** @param fileUrl 文件URL* @return 文件字节* @throws IOException*/public void deleteFile(String fileUrl) throws IOException {fastDFSClientWrapper.deleteFile(fileUrl);}
}
4、FileController
package com.ty.fdfs;import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;import javax.annotation.Resource;@RestController
public class FileController {@Resourceprivate FastDFSService fastDFSService;@PostMapping("/file/upload")public String upload(MultipartFile file){return fastDFSService.uploadFile(file);}
}
5、FastDFSClientWrapper
package com.ty.fdfs;import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.IOException;@Component
public class FastDFSClientWrapper {private final Logger logger = LoggerFactory.getLogger(FastDFSClientWrapper.class);@Resourceprivate FastFileStorageClient fastFileStorageClient;/*** 文件上传** @param bytes 文件字节* @param fileSize 文件大小* @param extension 文件扩展名* @return fastDfs路径*/public String uploadFile(byte[] bytes, long fileSize, String extension) {ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);StorePath storePath = fastFileStorageClient.uploadFile(byteArrayInputStream, fileSize, extension, null);System.out.println(storePath.getGroup() + "===" + storePath.getPath() + "======" + storePath.getFullPath());return storePath.getFullPath();}/*** 下载文件** @param fileUrl 文件URL* @return 文件字节* @throws IOException*/public byte[] downloadFile(String fileUrl) throws IOException {String group = fileUrl.substring(0, fileUrl.indexOf("/"));String path = fileUrl.substring(fileUrl.indexOf("/") + 1);DownloadByteArray downloadByteArray = new DownloadByteArray();byte[] bytes = fastFileStorageClient.downloadFile(group, path, downloadByteArray);return bytes;}/*** 删除文件** @param fileUrl 文件URL* @return 文件字节* @throws IOException*/public void deleteFile(String fileUrl) throws IOException {fastFileStorageClient.deleteFile( fileUrl);}
}