基于FastDFS在SpringBoot上的上传与下载

本文介绍如何在项目中部署并使用FastDFS文件系统,包括配置参数、引入依赖、编写上传与下载页面及实现上传下载功能的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

方法:

1.在application.properties里设置参数,其中tracker-list是tracker的端口

fdfs.so-timeout=1500
fdfs.connect-timeout=600
fdfs.tracker-list=192.168.118.12:22122

2.导包,使用Maven进行导包依赖

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

3.编写上传页面

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>pageurl:fileUploadPage</title>
</head>
<body>
    <div align="center">
        <h1>上传文件</h1>
        <form action="solveUpload" method="post" enctype="multipart/form-data">
            <input type="file" name="file">
            <button type="submit">提交</button>
        </form>
    </div>
</body>
</html>

4.注入FastFileStorageClient对象

@Autowired
FastFileStorageClient fc;

5.编写上传和下载方法

上传:

@RequestMapping("solveUpload")
@ResponseBody
public String solveUpload(@RequestParam("file")MultipartFile file) throws IOException {
    Set<MetaData> metaDataSet=new HashSet<>();//设置metaDataSet文件头元数据
    metaDataSet.add(new MetaData("Author", "littlepage"));
    metaDataSet.add(new MetaData("CreateDate","2019-7-24"));
    StorePath uploadFile=fc.uploadFile(file.getInputStream(),file.getSize(),
            file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")+1),
            metaDataSet);//上传直接返回数据,这边简略了关于路径保存数据库,直接返回路径
    return uploadFile.getPath();
}

下载:

@RequestMapping("apic")
@ResponseBody
public ResponseEntity<byte[]> down(HttpServletResponse resp){
    HttpHeaders httpHeaders=new HttpHeaders();//new出一个httpHeader对象
    httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);//设置Media类型为直接下载类型
    httpHeaders.setContentDispositionFormData("name", "aa.png");//设置名字和文件名
    byte[] bs=fc.downloadFile("group1", "M00/00/00/wKh2DF03SZiAQvM4AADNi5XjeDQ722.png",
     new DownloadByteArray());//下载 return new ResponseEntity<>(bs,httpHeaders,HttpStatus.OK);//返回封装实体 }

 

转载于:https://www.cnblogs.com/littlepage/p/11235478.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值