springboot集成fastDFS實現附件上傳和刪除功能

1.在pom.xml引用jar包

        <!-- FastDFS依賴 -->
        <dependency>
            <groupId>com.github.tobato</groupId>
            <artifactId>fastdfs-client</artifactId>
            <version>1.26.5</version>
        </dependency>

2.application.yml配置fastDFS節點信息

fdfs:
  # 鏈接超時
  connect-timeout: 120
  # 讀取時間
  so-timeout: 120
  # 生成縮略圖參數
  thumb-image:
    width: 150
    height: 150
  tracker-list: 172.16.1.83:22122,172.16.1.74:22122

3.創建DfsConfig.java

package com.currency.tbyoung.fastdfs;

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 DfsConfig {
}

4.創建FileDfsUtil.java

package com.currency.tbyoung.fastdfs;

import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@Component
public class FileDfsUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(FileDfsUtil.class);
    @Resource
    private FastFileStorageClient storageClient ;
    /**
     * 上傳文件
     */
    public String upload(MultipartFile multipartFile) throws Exception{
        String originalFilename = multipartFile.getOriginalFilename().
                substring(multipartFile.getOriginalFilename().
                        lastIndexOf(".") + 1);
        StorePath storePath = this.storageClient.uploadImageAndCrtThumbImage(
                multipartFile.getInputStream(),
                multipartFile.getSize(),originalFilename , null);
        return storePath.getFullPath() ;
    }
    /**
     * 刪除文件
     */
    public String deleteFile(String fileUrl) {
        if (StringUtils.isEmpty(fileUrl)) {
            return "fileUrl == >>文件路徑爲空...";
        }
        try {
            StorePath storePath = StorePath.parseFromUrl(fileUrl);
            storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
        } catch (Exception e) {
            return e.getMessage();
        }
        return "OK";
    }
}

5.創建測試controller

import com.currency.tbyoung.bean.ReturnValues;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource
@RestController
public class FileController {
    @Resource
    private FileDfsUtil fileDfsUtil ;

    @RequestMapping(value = "/uploadFile",headers="content-type=multipart/form-data", method = RequestMethod.POST)
    public ReturnValues uploadFile (@RequestParam("file") MultipartFile file){
        String result ;
        try{
            String path = fileDfsUtil.upload(file) ;
            if (!StringUtils.isEmpty(path)){
                result = path ;
            } else {
                result = "上傳失敗" ;
            }
        } catch (Exception e){
            e.printStackTrace() ;
            result = "服務異常" ;
        }
        return new ReturnValues(ReturnValues.SUCCESS,result);
    }
    /**
     * 文件刪除
     */
    @RequestMapping(value = "/deleteByPath", method = RequestMethod.GET)
    public ReturnValues deleteByPath (String filePathName){
        String result = fileDfsUtil.deleteFile(filePathName);
        if("OK".equals(result)){
            return new ReturnValues(ReturnValues.SUCCESS,"刪除成功");
        }else{
            return new ReturnValues(ReturnValues.ERROR,result);
        }
    }


}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章