FastDFS 整合springboot配置与使用

1 环境准备

资源 备注
FastDFS部署环境 如果未部署参考博文 FastDFS 安装配置和测试
IDEA2021 开发工具看自己喜好

我这里部署的FastDFS部署信息为:

服务器信息 IP地址
tracker服务器 192.168.51.5
storage服务器 192.168.51.6

2 pom文件配置

新建springboot 项目,引入fastdfs官方依赖

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

2 application文件配置

############################################################
#
#  分布式文件系统 FastDFS  配置
#
###########################################################
fdfs:
  connect-timeout: 30         # 连接tracker服务器超时时长
  so-timeout: 30              # socket连接超时时长
  tracker-list: 192.168.51.5:22122  # tracker服务所在的IP地址和端口号 ( #TrackerList参数,支持多个,我这里只有一个,如果有多个在下方加- x.x.x.x:port)
  thumb-image: #缩略图生成参数,可选
    height: 150
    width: 150

3 测试文件上传

3.1 业务层

FastDFSService

package com.auskat.service;

import org.springframework.web.multipart.MultipartFile;

/**
 * 类文件: FastDFSServer
 * <p>
 * <p>
 * 类描述:
 * <p>
 * 作     者: AusKa_T
 * <p>
 * 日     期: 2021/3/11 0011
 * <p>
 * 时     间: 15:01
 * <p>
 */
public interface FastDFSServer {
    
    

    /**
     * 上传文件到fastdfs
     * @param file 文件流
     * @param fileExtName 文件后缀名称
     * @return 上传路径
     * @throws Exception 异常信息
     */
    public String upload(MultipartFile file, String fileExtName) throws Exception;
}

FastDFSServiceImpl

package com.auskat.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectResult;
import com.auskat.config.FileResource;
import com.auskat.service.FastDFSService;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.net.URL;

/**
 * 类文件: FastDFSServerImpl
 * <p>
 * <p>
 * 类描述:
 * <p>
 * 作     者: AusKa_T
 * <p>
 * 日     期: 2021/3/11 0011
 * <p>
 * 时     间: 15:02
 * <p>
 */
@Service
public class FastDFSServiceImpl implements FastDFSService {
    
    

    @Autowired
    FastFileStorageClient fastFileStorageClient;

    /**
     * 上传文件到fastdfs
     * @param file 文件流
     * @param fileExtName 文件后缀名称
     * @return 上传路径
     * @throws Exception 异常信息
     */
    @Override
    public String upload(MultipartFile file,String fileExtName) throws Exception {
    
    
        StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), fileExtName, null);
        String path = storePath.getFullPath();
        return path;
    }
}

3.2 控制层

FastDFSController

package com.auskat.controller;

import com.auskat.service.FastDFSService;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 类文件: FastDFSController
 * <p>
 * <p>
 * 类描述:
 * <p>
 * 作     者: AusKa_T
 * <p>
 * 日     期: 2020/12/7 0007
 * <p>
 * 时     间: 14:46
 * <p>
 */
@RestController
@RequestMapping("fdfs")
public class FastDFSController {
    
    


    @Autowired
    FastDFSService fastDFSService;

		 /**
     * 上传文件到FastDFS
     * @param file 文件流
     * @param request 请求
     * @param response 响应
     * @return 结果
     * @throws Exception 异常信息
     */
     @PostMapping("uploadFile")
    public String uploadFace(
            @RequestParam MultipartFile file,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
    
    

        String path = "";

        // 开始文件上传
        if (file != null) {
    
    
            // 获取文件上传的文件名称
            String fileName = file.getOriginalFilename();
            if (StringUtils.isNotBlank(fileName)) {
    
    
                String[] fileNameArr = fileName.split("\\.");

                // 获取文件的后缀名
                String suffix = fileNameArr[fileNameArr.length - 1];
                path = fastDFSService.upload(file, suffix);
                System.out.println(path);
            } 
            return "上传成功!";
        }else {
    
    
            return "文件不能为空! ";
        }
    }

4 相关信息

  • 博文不易,辛苦各位猿友点个关注和赞,感谢

猜你喜欢

转载自blog.csdn.net/qq_15769939/article/details/115157835
今日推荐