springboot整合fastDFS
说到spring boot整合其他框架,还是三个步骤:pom文件依赖、配置文件、启动类;
1、pom文件依赖:
<!-- https://mvnrepository.com/artifact/com.github.tobato/fastdfs-client --> <dependency> <groupId>com.github.tobato</groupId> <artifactId>fastdfs-client</artifactId> <version>1.26.7</version> </dependency>
2、配置文件
server: port: 1006 spring: application: name: lcn-file servlet: #请求文件大小 multipart: max-file-size: 50MB max-request-size: 50MB eureka: client: service-url: defaultZone: http://localhost:7900/eureka/ fdfs: so-timeout: 1500 # 读取时间 connect-timeout: 600 # 连接超时时间 thumb-image: # 缩略图 width: 150 height: 150 tracker-list: # tracker服务配置地址列表 - 192.168.10.41:22122 pool: #从池中借出的对象的最大数目(配置为-1表示不限制) max-total: -1 #获取连接时的最大等待毫秒数(默认配置为5秒) max-wait-millis: 5000 #每个key最大连接数 max-total-per-key: 50 #每个key对应的连接池最大空闲连接数 max-idle-per-key: 10 #每个key对应的连接池最小空闲连接数 min-idle-per-key: 5 upload: baseUrl: http://192.168.10.41/
3、将FastDFS-Client客户端引入本地化项目的方式非常简单,在SpringBoot项目中添加一个配置类:
package com.dongl.file.config; import com.github.tobato.fastdfs.FdfsClientConfig; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableMBeanExport; import org.springframework.context.annotation.Import; import org.springframework.jmx.support.RegistrationPolicy; /** * @author D-L * @version 1.0.0 * @ClassName FdfsConfig.java * @Description 导入FastDFS-Client组件 * @createTime 2021-07-07 10:44:00 */ @Configuration @Import(FdfsClientConfig.class) //只需要一行注解 @Import(FdfsClientConfig.class)就可以拥有带有连接池的FastDFS Java客户端了 @EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING) // 解决jmx重复注册bean的问题 public class FdfsConfig { }
4、fastDFS文件上传下载工具类
package com.dongl.file.service; import com.github.tobato.fastdfs.domain.fdfs.StorePath; import com.github.tobato.fastdfs.domain.proto.storage.DownloadByteArray; import com.github.tobato.fastdfs.service.FastFileStorageClient; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.io.ByteArrayInputStream; /** * @author D-L * @version 1.0.0 * @ClassName FastDFSClientWrapper.java * @Description fastDFS文件上传下载工具类 * @createTime 2021-07-07 10:51:00 */ @Component @Slf4j public class FastDFSClient { @Autowired private FastFileStorageClient fastFileStorageClient; @Value("${upload.baseUrl}") private String baseUrl; /** * 文件上传 * * @param bytes 文件字节 * @param fileSize 文件大小 * @param extension 文件扩展名 * @return fastDfs路径 */ public String uploadFile(byte[] bytes, long fileSize, String extension) { log.info("fastDFS文件上传开始------"); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes); StorePath storePath = fastFileStorageClient.uploadFile(byteArrayInputStream, fileSize, extension, null); String fullPath = storePath.getFullPath(); log.info("fastDFS文件上传完成------"); return baseUrl + fullPath; } /** * 下载文件 * * @param fileUrl 文件URL * @return 文件字节 */ public byte[] downloadFile(String fileUrl) { String group = fileUrl.substring(0, fileUrl.indexOf("/")); String path = fileUrl.substring(fileUrl.indexOf("/") + 1); DownloadByteArray downloadByteArray = new DownloadByteArray(); byte[] bytes = fastFileStorageClient.downloadFile(group, path, downloadByteArray); return bytes; } }
5、文件上传下载访问接口
package com.dongl.file.controller; import com.dongl.file.service.FastDFSClient; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.net.URLEncoder; /** * @author D-L * @version 1.0.0 * @ClassName FastDFSController.java * @Description fastDFS文件上传下载 * @createTime 2021-07-07 10:55:00 */ @Slf4j @RestController @RequestMapping("/fastDFS") public class FastDFSController { @Autowired private FastDFSClient fastDFSClient; @PostMapping("/upload") public String uploadFile(MultipartFile file) throws IOException { String result =""; try { byte[] bytes = file.getBytes(); String originalFileName = file.getOriginalFilename(); String extension = originalFileName.substring(originalFileName.lastIndexOf(".") + 1); long fileSize = file.getSize(); result = fastDFSClient.uploadFile(bytes, fileSize, extension); }catch (Exception e) { log.error("fastDFS 文件上传失败------fileName:{}" , file.getName()); } return result; } /** * * @param fileUrl group1/M00/00/00/wKgKGWDlIo-AKfngAAFlGYb7GuA61.xlsx * @param response */ @GetMapping("/download") public void downloadFile(String fileUrl, HttpServletResponse response){ String suffix = fileUrl.substring(fileUrl.lastIndexOf(".")); byte[] bytes = new byte[0]; try { bytes = fastDFSClient.downloadFile(fileUrl); response.reset(); response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("fastDFS" + suffix, "UTF-8")); response.setCharacterEncoding("UTF-8"); } catch (IOException e) { log.error("fastDFS 下载文件失败------fileUrl:{}" , fileUrl); } ServletOutputStream outputStream = null; try { outputStream = response.getOutputStream(); outputStream.write(bytes); } catch (IOException e) { e.printStackTrace(); } finally { try { outputStream.flush(); outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }