SpringBoot2.0整合Fastdfs

接上一篇,docker搭建FastDFS文件系统。

这一篇实现SpringBoot2.0整合Fastdfs。

1.引入FastDfs依赖

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

2.application.yml配置文件

fdfs:
  connect-timeout: 600
  so-timeout: 1500
  trackerList: 127.0.0.1:22122 
  thumb-image:  
    width: 150
    height: 150
  pool:
    max-total: 200

3.在启动类添加如下配置

@Configuration
@SpringBootApplication
@MapperScan("com.gbq.boot.web.mapper")
@EnableMBeanExport(registration= RegistrationPolicy.IGNORE_EXISTING)
@Import(FdfsClientConfig.class)
@EnableAsync
public class BootApplication {
    public static void main(String[] args) {
        SpringApplication.run(BootApplication.class, args);
    }
}


 4.controller 接口编写

package com.gbq.boot.web.controller;


import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.velocity.shaded.commons.io.FilenameUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;

@RestController
@RequestMapping("/file")
public class FileController {

    @Resource
    private FastFileStorageClient fastFileStorageClient;

    /**
     * 文件上传
     * @return result
     */
    @ResponseBody
    @PostMapping("/upload")
    public HashMap<String, Object> uploadImageByCover(MultipartFile attach){
        HashMap<String,Object> result = new HashMap<>();
        try {
            //上传
            StorePath path = fastFileStorageClient.
                            uploadFile(attach.getInputStream(),attach.getSize(),
                            FilenameUtils.getExtension(attach.getOriginalFilename()),null);
            //获取路径加名称
            String picName = path.getFullPath();
            result.put("msg",picName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 文件删除
     * @param path
     * @return
     */
    @DeleteMapping("/delete")
    public HashMap<String, Object> delete(@RequestParam String path) {
        HashMap<String,Object> result = new HashMap<>();
        // 第一种删除:参数:完整地址
        fastFileStorageClient.deleteFile(path);
        result.put("msg","恭喜恭喜,删除成功!");
        // 第二种删除:参数:组名加文件路径
        // fastFileStorageClient.deleteFile(group,path);

        return result;
    }


  /**
     * 文件下载
     * @param url 路径
     * @return
     */
    @GetMapping("/download")
    public void downLoad(@RequestParam String url, HttpServletResponse response) throws IOException {
        String group = url.substring(0, url.indexOf("/"));
        String path = url.substring(url.indexOf("/") + 1);
        //文件后缀
        String substring = url.substring(url.lastIndexOf(".") + 1);
        byte[] bytes = fastFileStorageClient.downloadFile(group, path, new DownloadByteArray());

        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(UUID.randomUUID().toString()+"."+substring, "UTF-8"));

        // 写出
        ServletOutputStream outputStream = response.getOutputStream();
        IOUtils.write(bytes, outputStream);
    }
}

 

测试一下!!!!

上传!

浏览器访问一下

删除!把之前的照片删掉!

再通过浏览器访问,应该就是404,记得清除浏览器缓存哦!

下载!记得再上传一张照片,如果服务器有照片请忽略!

 

注意一下上传路径,fastFileStorageClient会加上你的服务器地址+ip,只需要刚刚上传成功路径即可!

 

ok,全部都大功告成了,拜拜!有啥问题群里联系我!

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

答 案

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

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

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

打赏作者

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

抵扣说明:

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

余额充值