springboot集成fastdfs

1、配置pom.xml


	com.github.tobato
	fastdfs-client
	1.25.2-RELEASE

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);
    }
}



你可能感兴趣的:(springboot)