Linux安装fastDFS

Linux 安装fastDFS

 安装教程(官网)
 https://github.com/happyfish100/fastdfs/wiki 

用Spring boot 操作fastDFS

pom依赖

<!-- 当前日期最新版-->
<dependency>
    <groupId>com.github.tobato</groupId>
    <artifactId>fastdfs-client</artifactId>
    <version>1.26.7</version>
</dependency>

application.yml

fdfs:
  so-timeout: 1501
  connect-timeout: 2000
  thumb-image:             #缩略图生成参数
    width: 150
    height: 150
  tracker-list:            #TrackerList参数,支持多个
    - 127.0.0.1:22122
controller部分

fdfs:
  tracker-list: 192.168.128.129:22122

controller部分

@RestController
public class TestApi {
 
    @Autowired
    private FastFileStorageClient fastFileStorageClient;
 
    /**
     * 文件上传
     *
     * @param file
     * @return
     * @throws IOException
     */
    @ApiOperation("文件上传")
    @PostMapping("/uppload")
    public StorePath test(@RequestParam MultipartFile file) throws IOException {
 
        // 设置文件信息
        Set<MataData> mataData = new HashSet<>();
        mataData.add(new MataData("author", "zonghui"));
        mataData.add(new MataData("description", "xxx文件,嘿嘿嘿"));
 
        // 上传   (文件上传可不填文件信息,填入null即可)
        StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()), mataData);
 
        return storePath;
    }
 
    /**
     * 文件删除
     *
     * @param path
     * @return
     */
    @ApiOperation("文件删除")
    @DeleteMapping("/delete")
    public String delete(@RequestParam String path) {
 
        // 第一种删除:参数:完整地址
        fastFileStorageClient.deleteFile(path);
 
        // 第二种删除:参数:组名加文件路径
        // fastFileStorageClient.deleteFile(group,path);
 
        return "恭喜恭喜,删除成功!";
    }
 
 
    /**
     * 文件下载
     *
     * @param path
     * @return
     */
    @ApiOperation("文件下载")
    @GetMapping("/download")
    public void downLoad(@RequestParam String group, @RequestParam String path, @RequestParam String fileName, HttpServletResponse response) throws IOException {
 
        // 获取文件
        byte[] bytes = fastFileStorageClient.downloadFile(group, path, new DownloadByteArray());
 
        //设置相应类型application/octet-stream        (注:applicatoin/octet-stream 为通用,一些其它的类型苹果浏览器下载内容可能为空)
        response.reset();
        response.setContentType("applicatoin/octet-stream");
        //设置头信息                 Content-Disposition为属性名  附件形式打开下载文件   指定名称  为 设定的fileName
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        // 写入到流
        ServletOutputStream out = response.getOutputStream();
        out.write(bytes);
        out.close();
    }
 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值