springboot整合FastDFS完整示例

  • SpringBoot整合FastDFS很简单,这里使用的组件是:
<dependency>
	<groupId>com.github.tobatogroupId>
	<artifactId>fastdfs-clientartifactId>
	<version>1.26.2version>
	
	
dependency>
  • 创建FastDFSConfiguration类:
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.context.annotation.Import;
import org.springframework.jmx.support.RegistrationPolicy;

import com.github.tobato.fastdfs.FdfsClientConfig;
 
@Configuration
@Import(FdfsClientConfig.class)
@EnableMBeanExport(registration= RegistrationPolicy.IGNORE_EXISTING)// 解决jmx重复注册bean的问题
public class FastDFSConfiguration {
 
}
  • application.properties配置文件中添加FastDFS的相关配置:
### FastDFS相关配置
## tracker地址,多个可fdfs.trackerList[0]、fdfs.trackerList[1]等方式配置
fdfs.trackerList=[ip]:[port]
## 连接超时时间
fdfs.connect-timeout=5000
## 读取inputsream阻塞时间
fdfs.so-timeout=3000
## 连接池最大数量 
fdfs.pool.max-total=200
## 每个tracker地址的最大连接数
fdfs.pool.max-total-per-key=20
## 连接耗尽时等待获取连接的最大毫秒数
fdfs.pool.max-wait-millis=25000
## 缩略图相关配置
fdfs.thumbImage.height=150
fdfs.thumbImage.width=150
  • 把上面配置中的[ip]和[port]替换成自己FastDFS的tracker地址就整合完成了,然后添加文件上传、下载、删除方法的包装类:
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;

import org.apache.commons.io.FileUtils;
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 com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.domain.ThumbImageConfig;
import com.github.tobato.fastdfs.proto.storage.DownloadCallback;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
 
/**
 * 
类 名: FastDFSClientWrapper *
描 述: FastDFS文件上传下载包装类 *
作 者: Qiwan *
创 建: 2019年3月28日 下午4:48:58 *
版 本: v1.0.0 */
@Component public class FastDFSClientWrapper { @Autowired private FastFileStorageClient storageClient; @Autowired private ThumbImageConfig thumbImageConfig; /** * @Description: 上传文件 * @param file 文件对象 * @return 文件路径 * @throws IOException String */ public String uploadFile(MultipartFile file) throws IOException { StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()), null); return storePath.getFullPath(); } /** * @Description: 上传文件 * @param bytes 文件数据 * @param format 文件格式(后缀) * @return String 文件路径 */ public String uploadFile(byte[] bytes, String format) { StorePath storePath = storageClient.uploadFile(new ByteArrayInputStream(bytes), bytes.length, format, null); return storePath.getFullPath(); } /** * @Description: 上传文件 * @param file 文件对象 * @return * @throws IOException String */ public String uploadFile(File file) throws IOException { StorePath storePath = storageClient.uploadFile(FileUtils.openInputStream(file), file.length(), FilenameUtils.getExtension(file.getName()), null); return storePath.getFullPath(); } /** * @Description: 把字符串作为指定格式的文件上传 * @param content * @param fileExtension * @return String */ public String uploadFile(String content, String fileExtension) { byte[] buff = content.getBytes(Charset.forName("UTF-8")); ByteArrayInputStream stream = new ByteArrayInputStream(buff); StorePath storePath = storageClient.uploadFile(stream, buff.length, fileExtension, null); return storePath.getFullPath(); } /** * @Description: 上传文件 * @param file 文件对象 * @return 文件路径 * @throws IOException String */ public String uploadImageAndCrtThumbImage(MultipartFile file) throws IOException { StorePath storePath = storageClient.uploadImageAndCrtThumbImage(file.getInputStream(), file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()), null); return storePath.getFullPath(); } /** * @Description: 根据图片路径获取缩略图路径(使用uploadImageAndCrtThumbImage方法上传图片) * @param filePath 图片路径 * @return String 缩略图路径 */ public String getThumbImagePath(String filePath) { return thumbImageConfig.getThumbImagePath(filePath); } /** * @Description: 根据文件路径下载文件 * @param filePath 文件路径 * @return 文件字节数据 * @throws IOException byte[] */ public byte[] downFile(String filePath) throws IOException { StorePath storePath = StorePath.praseFromUrl(filePath); return storageClient.downloadFile(storePath.getGroup(), storePath.getPath(), new DownloadCallback<byte[]>() { @Override public byte[] recv(InputStream ins) throws IOException { return org.apache.commons.io.IOUtils.toByteArray(ins); } }); } /** * @Description: 根据文件地址删除文件 * @param fileUrl 文件访问地址 */ public void deleteFile(String filePath) { StorePath storePath = StorePath.praseFromUrl(filePath); storageClient.deleteFile(storePath.getGroup(), storePath.getPath()); } }

你可能感兴趣的:(Java,Spring,Spring,Boot,FastDFS)