springboot集成fastDFS完成文件上传

springboot集成fastDFS完成文件上传

fastDFS是一个开源的分布式的文件服务器,安装和配置方法请参考其他博客,这里安装配置完毕后的springboot配置使用方法

安装完成后需要两个IP地址:

183.207.183.130:22122 (服务地址,一般是固定的)

http://183.207.183.130:18001(图片展示地址,Nginx配置的地址)

/group1/M00/41/01/ChAB3l020QWAJMfbAAAQW2uGEIQ109.png(图片上传后的路径)

1、引用jar包

  
        
            com.github.tobato
            fastdfs-client
            1.26.5
        

2、加入配置:application.yml

fdfs:
  so-timeout: 1501
  connect-timeout: 601
  thumb-image:             #缩略图生成参数
    width: 200
    height: 200
  tracker-list:            #TrackerList参数,支持多个
   - 183.207.183.130:22122

3、集成配置config

package com.njwd.common.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 ComponetImport {
}

4、API上传删除工具类

package com.njwd.common.util;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.fdfs.ThumbImageConfig;
import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
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.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;

/**
 * Fastdfs工具类
 */
@Component
public class FastdfsClientUtil {

    private final Logger logger = LoggerFactory.getLogger(FastdfsClientUtil.class);
    @Autowired
    private FastFileStorageClient storageClient;
    @Autowired
    private ThumbImageConfig thumbImageConfig;


    //上传文件
    public String upload(MultipartFile myfile) throws Exception{
        //文件名
        String originalFilename = myfile.getOriginalFilename().substring(myfile.getOriginalFilename().lastIndexOf(".") + 1);
        // 文件扩展名
        String ext = originalFilename.substring(originalFilename.lastIndexOf(".") + 1, originalFilename.length());

        StorePath storePath = this.storageClient.uploadImageAndCrtThumbImage(myfile.getInputStream(), myfile.getSize(),originalFilename , null);

        String path = storePath.getFullPath();

        return path;
    }

    /**
     * 删除文件
     * @Param fileUrl 文件访问地址
     */
    public void deleteFile(String fileUrl) {
        if (StringUtils.isEmpty(fileUrl)) {
            return;
        }
        try {
            StorePath storePath = StorePath.parseFromUrl(fileUrl);
            storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
        } catch (FdfsUnsupportStorePathException e) {
            logger.warn(e.getMessage());
        }
    }
}

5、controller使用范例

package com.njwd.common.controller;

import com.njwd.common.util.ExportExcelUtil;
import com.njwd.common.util.FastdfsClientUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Api("文件上传接口")
@RequestMapping("/upload")
@RestController
public class UploadController {

    @Autowired
    FastdfsClientUtil fastdfsClientUtil;

    @ApiOperation(value = "文件上传", notes = "文件上传")
    @PostMapping("/file")
    public String uploadFile(@RequestParam("file") MultipartFile file) throws Exception{
        return fastdfsClientUtil.upload(file);
    }

}

 

你可能感兴趣的:(spring)