springboot集成fastdfs

本文介绍了如何将FastDFS文件存储系统与SpringBoot应用进行集成,主要包括在pom.xml中添加依赖配置,以及在application.properties中设置相关属性的详细步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、配置pom.xml

<dependency>
	<groupId>com.github.tobato</groupId>
	<artifactId>fastdfs-client</artifactId>
	<version>1.25.2-RELEASE</version>
</dependency>

2、配置application.properties

fdfs.soTimeout=1500
fdfs.connectTimeout=600
fdfs.thumbImage.width=150
fdfs.thumbImage.height=200
fdfs.trackerList[0]=ip:port
fdfs.trackerList[1]=ip:port
##这个一定要有,比较坑
spring.jmx.enabled=false


3、applicatio.java

引入 @Import(FdfsClientConfig.class)

@Import(FdfsClientConfig.class)
@SpringBootApplication
public class FileApplication {

	public static void main(String[] args) {

		SpringApplication.run(FileApplication.class, args);

	}

}


通过以上三步就能成功集成



以下是我封装的一些API,

有 上传文件,上传图片及缩略图,删除,装载路径

fastdfs官方提供的api中下载方法无法满足作者系统业务,因此没有采取,但是可以通过api拿到文件 http路径,再进行下载

package fmc.file.clint;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;

import fmc.file.model.util.FdfsInfo;
import fmc.file.model.util.NginxInfo;
import fmc.file.utils.FileUtil;

/**
 * fdfs API封装类
 * @author Administrator
 *
 */
@Configuration
public class FdfsClient {

	@Autowired
    private FastFileStorageClient storageClient;
	
	@Autowired
	private NginxInfo nginxInfo;
	
	
	@Autowired
	private FdfsInfo fdfsInfo;
	
	/*@Autowired
	private FileUtil fileUtil;*/
	
	
	/**
     * 上传文件  适用于所有文件
     * @param file 文件对象
     * @return 文件访问地址
     * @throws IOException
     */
	public StorePath uploadFile(MultipartFile file) throws IOException{
		StorePath storePath = storageClient.uploadFile(file.getInputStream(),file.getSize(), 
				FilenameUtils.getExtension(file.getOriginalFilename()),null);
        return storePath;
    }
	
	/**
     * 将一段字符串生成一个文件上传
     * @param content 文件路径
     * @return文件访问地址
	 * @throws FileNotFoundException 
     */
	@SuppressWarnings("resource")
	public StorePath uploadFile(String content) throws FileNotFoundException{
    	if(!StringUtils.hasText(content)){
    		throw new NullPointerException();
    	}
    	File file = new File(content);
		FileInputStream inputStream=new FileInputStream(file);
		String fileName=file.getName();
		//获取文件后缀名
		String strs= FileUtil.getExtName(fileName);
		if(!StringUtils.hasText(strs)){
			throw new NullPointerException();
        }
        StorePath storePath = storageClient.uploadFile(inputStream,file.length(),strs,null);
        return storePath;
    }
	
	/**
     * 传图片并同时生成一个缩略图
     * "JPG", "JPEG", "PNG", "GIF", "BMP", "WBMP"
     * @param file 文件对象
     * @return 文件访问地址
     * @throws IOException
     */
	public StorePath uploadImageAndCrtThumbImage(MultipartFile file) throws IOException{
        StorePath storePath = storageClient.uploadImageAndCrtThumbImage(file.getInputStream(),file.getSize(), 
        		FilenameUtils.getExtension(file.getOriginalFilename()),null);
        return storePath;
    }
	
	/**
     * 传图片并同时生成一个缩略图 
     * "JPG", "JPEG", "PNG", "GIF", "BMP", "WBMP"
     * @param content 文件路径
     * @return 文件访问地址
	 * @throws FileNotFoundException 
     */
	@SuppressWarnings("resource")
	public StorePath uploadImageAndCrtThumbImage(String content) throws FileNotFoundException{
		if(!StringUtils.hasText(content)){
    		throw new NullPointerException();
    	}
		File file = new File(content);
		FileInputStream inputStream=new FileInputStream(file);
		String fileName=file.getName();
		//获取文件后缀名
		String strs= FileUtil.getExtName(fileName);
		if(!StringUtils.hasText(strs)){
			throw new NullPointerException();
        }
        StorePath storePath = storageClient.uploadImageAndCrtThumbImage(inputStream,file.length(),strs,null);
        return storePath;
    }
	
	/**
     * 封装完整文件地址
     * @param fullPah 文件路径
     * @return 文件访问地址
     */
	public String getFilePath(String fullPath) {
		if(!StringUtils.hasText(fullPath)){
			throw new NullPointerException();
		}
		
		String ngLoc = nginxInfo.getBaseUrl();
		StringBuffer filePath = new StringBuffer(ngLoc);
		filePath.append("/");
		filePath.append(fullPath);
		return filePath.toString();
	}
	
	
	/**
     * 装载缩略图名称
     * "JPG", "JPEG", "PNG", "GIF", "BMP", "WBMP"
     * @param storePath 文件路径
     * @return 缩略图名称
     */
	public String getThumbImage(String storePath){
		if(!StringUtils.hasText(storePath)){
    		throw new NullPointerException();
    	}
		
		//获取文件名称
		String pathL = FileUtil.getFileName(storePath) + "_" + fdfsInfo.getSize();
		//获取文件后缀名
		String pathR = FileUtil.getExtName(storePath);
		return pathL + "." + pathR;
	}
	
	/**
     * 删除文件
     * @param fileUrl 文件访问地址
     * @return
     * @throws IOException
     */
    public void deleteFile(String storePath){
        if (!StringUtils.hasText(storePath)) {
        	throw new NullPointerException();
        }
        storageClient.deleteFile(storePath);
    }
}



评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值