FastDFS文件上傳下載流程


上傳:

  1. Client通過Tracker server查找可用的Storage server。

  2. Tracker server向Client返回一台可用的Storage server的IP地址和端口號。

  3. Client直接通過Tracker server返回的IP地址和端口與其中一台Storage server建立連接並進行文件上傳。

  4. 上傳完成,Storage server返回Client一個文件ID,文件上傳結束。

下載:

  1. Client通過Tracker server查找要下載文件所在的的Storage server。

  2. Tracker server向Client返回包含指定文件的某個Storage server的IP地址和端口號。

  3. Client直接通過Tracker server返回的IP地址和端口與其中一台Storage server建立連接並指定要下載文件。

  4. 下載文件成功。

java客戶端

springboot項目引入依賴

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

配置類

package com.leyou.config;
 
import com.github.tobato.fastdfs.FdfsClientConfig;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.context.annotation.Import;
import org.springframework.jmx.support.RegistrationPolicy;
 
@Configuration
@Import(FdfsClientConfig.class)
/**
 * 解決jmx重復注冊bean的問題
  */
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class FastClientImporter {
}

在yml中編寫FastDFS的屬性

測試:

package com.leyou.test;
 
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.domain.ThumbImageConfig;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.leyou.LyUploadService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
 
@RunWith(SpringRunner.class)
@SpringBootTest(classes = LyUploadService.class)
public class FdfsTest {
 
    @Autowired
    private FastFileStorageClient storageClient;
 
    @Autowired
    private ThumbImageConfig thumbImageConfig;
 
    @Test
    public void testUpload() throws FileNotFoundException {
        File file = new File("G:\\LeYou\\upload\\spitter_logo_50.png");
        // 上傳
        StorePath storePath = this.storageClient.uploadFile(
                new FileInputStream(file), file.length(), "png", null);
        // 帶分組的路徑
        System.out.println(storePath.getFullPath());
        // 不帶分組的路徑
        System.out.println(storePath.getPath());
    }
 
    @Test
    public void testUploadAndCreateThumb() throws FileNotFoundException {
        File file = new File("G:\\LeYou\\upload\\spitter_logo_50.png");
        // 上傳並且生成縮略圖
        StorePath storePath = this.storageClient.uploadImageAndCrtThumbImage(
                new FileInputStream(file), file.length(), "png", null);
        // 帶分組的路徑
        System.out.println(storePath.getFullPath());
        // 不帶分組的路徑
        System.out.println(storePath.getPath());
        // 獲取縮略圖路徑
        String path = thumbImageConfig.getThumbImagePath(storePath.getPath());
        System.out.println(path);
    }
}

具體應用中上傳

package com.leyou.upload.service.serviceimpl;

import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.leyou.upload.service.UploadService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
 
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
 
@Service
public class UploadServiceImpl implements UploadService {
 
    @Autowired
    private FastFileStorageClient storageClient;
 
    private static final Logger logger= LoggerFactory.getLogger(UploadServiceImpl.class);
 
    /**
     *     支持上傳的文件類型
     */
    private static final List<String> suffixes = Arrays.asList("image/png","image/jpeg","image/jpg");
 
 
    @Override
    public String upload(MultipartFile file) {
        /**
         * 1.圖片信息校驗
         *      1)校驗文件類型
         *      2)校驗圖片內容
         * 2.保存圖片
         *      1)生成保存目錄
         *      2)保存圖片
         *      3)拼接圖片地址
         */
        try {
            String type = file.getContentType();
            if (!suffixes.contains(type)) {
                logger.info("上傳文件失敗,文件類型不匹配:{}", type);
                return null;
            }
            BufferedImage image = ImageIO.read(file.getInputStream());
            if (image == null) {
                logger.info("上傳失敗,文件內容不符合要求");
                return null;
            }
 
            StorePath storePath = this.storageClient.uploadFile(
                  file.getInputStream(), file.getSize(), getExtension(file.getOriginalFilename()), null);
       
            String url = "http://image.leyou.com/"+storePath.getFullPath();
            return url;
        }catch (Exception e){
            return null;
        }
    }
 
    public String getExtension(String fileName){
        return StringUtils.substringAfterLast(fileName,".");
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM