接上一篇,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,全部都大功告成了,拜拜!有啥问题群里联系我!