springboot中用Fast DFS分布式系统实现文件的上传下载

文章介绍了如何在SpringBoot应用中集成FastDFS,包括添加FastDFS客户端依赖,配置连接参数,以及在控制器中使用@Autowired注解注入FastDFS客户端对象来实现文件的上传和下载功能。上传文件返回文件存储路径,下载文件则根据路径解析组名和文件名进行下载。
摘要由CSDN通过智能技术生成

一、引入FastDFS客户端依赖:

二、在application.yml配置文件中配置FastDFS客户端,包括连接超时、网络超时、字符集等参数的设置以及FastDFS服务器的地址和端口号等信息。

三、使用@Autowired注入FastDFS客户端对象,在需要上传或下载文件的控制器中调用FastDFS提供的API接口。具体上传、下载的流程如下:

  • 上传文件:调用FastDFS的uploadFile方法将文件流上传至FastDFS服务器,并返回文件存储路径(StorePath对象)。通过访问路径即可获取文件内容。
  • 下载文件:根据上传时返回的访问路径解析出文件所在的组名(group)和文件名(path),然后调用FastDFS的downloadFile方法下载文件,方法会返回一个字节数组。将字节数组写回响应中即可完成文件下载。
  1. 根据返回的StorePath对象构造文件的访问路径。访问路径以http://+FastDFS服务器IP+端口号+/+group+/+path的形式组成,其中group是FastDFS服务器上的分组名称, path是文件在分组里的路径和文件名组成的字符串。

  2. 将文件流或字节数组返回给客户端,完成文件的上传或下载操作。

1.引入FastDFS依赖 

 在pom.xml文件中加入FastDFS客户端的依赖:

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

2.配置FastDFS客户端

在application.yml配置文件中加入FastDFS客户端的配置,例如:

# fastdfs配置
fdfs:
  # 请求连接池配置
  soTimeout: 1500
  connectTimeout: 600
  tracker-list:
    - 192.168.10.100:22122
    - 192.168.10.101:22122

上传文件的控制器方法:

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class FileController {

    @Autowired
    private FastFileStorageClient storageClient;

    @PostMapping("/upload")
    public String upload(@RequestPart MultipartFile file) throws IOException {
        StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(),
                FilenameUtils.getExtension(file.getOriginalFilename()), null);
        return storePath.getFullPath();
    }
}

下载文件的控制器方法:

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.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class DownloadController {

    @Autowired
    private FastFileStorageClient storageClient;

    @GetMapping("/download")
    public ResponseEntity<byte[]> download(@RequestParam String filepath) throws IOException {
        StorePath storePath = StorePath.parseFromUrl(filepath);
        byte[] content = storageClient.downloadFile(storePath.getGroup(), storePath.getPath(),
                new DownloadByteArray());
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        headers.setContentDispositionFormData("attachment", storePath.getFilename());
        headers.setContentLength(content.length);
        return new ResponseEntity<>(content, headers, HttpStatus.OK);
    }
}

其中@Autowired注入了FastFileStorageClient,使用该客户端的uploadFile方法进行文件上传,通过downloadFile的方式下载指定路径下的文件。

注意,上传时返回的storePath包含了group和path,用于构造文件访问路径。下载时需要从访问路径中解析出group和path参数再进行下载。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值