Spring Boot整合FastDFS完整示例

10 篇文章 0 订阅
6 篇文章 0 订阅
  • SpringBoot整合FastDFS很简单,这里使用的组件是:
<dependency>
	<groupId>com.github.tobato</groupId>
	<artifactId>fastdfs-client</artifactId>
	<version>1.26.2</version>
	<!-- 出现日志相关异常时取消以下注释 -->
	<!-- <exclusions>
		<exclusion>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-classic</artifactId>
		</exclusion>
	</exclusions> -->
</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]替换成自己FastDFStracker地址就整合完成了,然后添加文件上传、下载、删除方法的包装类:
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;
 
/**
 * <br>类 名: FastDFSClientWrapper
 * <br>描 述: FastDFS文件上传下载包装类
 * <br>作 者: Qiwan
 * <br>创 建: 2019年3月28日 下午4:48:58
 * <br>版 本: 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());
	}
}
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值