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 相关信息

  • 博文不易,辛苦各位猿友点个关注和赞,感谢
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小P聊技术

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值