1. 程式人生 > >springboot整合fastdfs

springboot整合fastdfs

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