当前位置: 首页 > news >正文

FastDFS SpringBoot 客户端 Demo搭建,支持文件上传下载

一、准备 fastdfs-client-java 依赖包

1、从 Git 下载 FastDFS java client SDK 源码

https://github.com/happyfish100/fastdfs-client-java.git

<fastdfs-client-java 源码见附件>

2、使用ant从源码构建

ant clean package

3、使用maven从源码安装

mvn clean install

4、使用maven从jar文件安装

mvn install:install-file -DgroupId=org.csource -DartifactId=fastdfs-client-java -Dversion=${version} -Dpackaging=jar -Dfile=fastdfs-client-java-${version}.jar

5、在Demo的maven项目pom.xml中添加依赖

<dependency><groupId>org.csource</groupId><artifactId>fastdfs-client-java</artifactId><version>1.27-SNAPSHOT</version>
</dependency>

二、Demo搭建

1、POM配置,添加相关依赖

        <dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><dependency><groupId>org.csource</groupId><artifactId>fastdfs-client-java</artifactId><version>1.27-SNAPSHOT</version></dependency><!-- fastdfs --><dependency><groupId>com.github.tobato</groupId><artifactId>fastdfs-client</artifactId><version>1.27.2</version></dependency>

2、FastdfsConfig

package com.ty.fdfs;import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;@Data
@Component
@ConfigurationProperties(prefix = "fdfs")
public class FastdfsConfig
{/*** fastdfs对外域名*/private String outurl;
}

3、FastDFSService

package com.ty.fdfs;import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;import javax.annotation.Resource;
import java.io.IOException;@Slf4j
@Service
public class FastDFSService {@Resourceprivate FastDFSClientWrapper fastDFSClientWrapper ;@Resourceprivate FastdfsConfig fastdfsConfig ;public  String uploadFile(MultipartFile file){try {byte[] bytes = file.getBytes();String originalFileName = file.getOriginalFilename();String extension = originalFileName.substring(originalFileName.lastIndexOf(".") + 1);String fileName = file.getName();long fileSize = file.getSize();log.info("文件上传文件属性[originalFileName:{},fileName:{},fileSize:{},extension:{}, bytes.lengt:{}]",originalFileName,fileName,fileSize,extension,bytes.length);String url =  fastDFSClientWrapper.uploadFile(bytes, fileSize, extension);String resultUrl = fastdfsConfig.getOuturl() + url;log.info("文件地址:{}",resultUrl);return resultUrl;} catch (IOException e) {log.error("fastdfs上传文件失败,{}",e);}return null;}/*** 下载文件** @param fileUrl 文件URL* @return 文件字节* @throws IOException*/public  byte[] downloadFile(String fileUrl) throws IOException {byte[] bytes = fastDFSClientWrapper.downloadFile(fileUrl);return bytes;}/*** 下载文件** @param fileUrl 文件URL* @return 文件字节* @throws IOException*/public  void deleteFile(String fileUrl) throws IOException {fastDFSClientWrapper.deleteFile(fileUrl);}
}

4、FileController

package com.ty.fdfs;import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;import javax.annotation.Resource;@RestController
public class FileController {@Resourceprivate FastDFSService fastDFSService;@PostMapping("/file/upload")public String upload(MultipartFile file){return fastDFSService.uploadFile(file);}
}

5、FastDFSClientWrapper

package com.ty.fdfs;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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.IOException;@Component
public class FastDFSClientWrapper {private final Logger logger = LoggerFactory.getLogger(FastDFSClientWrapper.class);@Resourceprivate FastFileStorageClient fastFileStorageClient;/*** 文件上传** @param bytes     文件字节* @param fileSize  文件大小* @param extension 文件扩展名* @return fastDfs路径*/public String uploadFile(byte[] bytes, long fileSize, String extension) {ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);StorePath storePath = fastFileStorageClient.uploadFile(byteArrayInputStream, fileSize, extension, null);System.out.println(storePath.getGroup() + "===" + storePath.getPath() + "======" + storePath.getFullPath());return storePath.getFullPath();}/*** 下载文件** @param fileUrl 文件URL* @return 文件字节* @throws IOException*/public byte[] downloadFile(String fileUrl) throws IOException {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;}/*** 删除文件** @param fileUrl 文件URL* @return 文件字节* @throws IOException*/public void  deleteFile(String fileUrl) throws IOException {fastFileStorageClient.deleteFile( fileUrl);}
}
http://www.lryc.cn/news/372782.html

相关文章:

  • 十大成长型思维:定位思维、商业思维、时间管理思维、学习成长思维、精力管理思维、逻辑表达思维、聚焦思维、金字塔原理、目标思维、反思思维
  • GraphQL(9):Spring Boot集成Graphql简单实例
  • vue3+ Element-Plus 点击勾选框往input中动态添加多个tag
  • 唯美仙侠手游【九幽仙域】win服务端+GM后台+详细教程
  • Qt creator day2练习
  • 哪里有海量的短视频素材,以及短视频制作教程?
  • 文章MSM_metagenomics(三):Alpha多样性分析
  • Web前端与其他前端:深度对比与差异性剖析
  • AI 客服定制:LangChain集成订单能力
  • 【计算机毕业设计】242基于微信小程序的外卖点餐系统
  • java程序监控linux服务器硬件,cpu、mem、disk等
  • 高考报志愿闲谈
  • 面试官考我Object类中的所有方法及场景使用?我...
  • Web前端精通教程:深入探索与实战指南
  • 四轴飞行器、无人机(STM32、NRF24L01)
  • OpenAI Assistants API:如何使用代码或无需代码创建您自己的AI助手
  • CentOS7 配置Nginx域名HTTPS
  • C++入门8 构造函数析构函数顺序|拷贝构造
  • 【git使用四】git分支理解与操作(详解)
  • 【docker】如何解决artalk的跨域访问问题
  • MYSQL 索引下推 45讲
  • CentOS7服务器中安装openCV4.8的教程
  • Java课程设计:基于swing的贪吃蛇小游戏
  • 【HarmonyOS】HUAWEI DevEco Studio 下载地址汇总
  • 华为OD刷题C卷 - 每日刷题30(小明找位置,分隔均衡字符串)
  • SOFTS: 时间序列预测的最新模型以及Python使用示例
  • C++ 取近似值
  • 云原生系列之Docker常用命令
  • opencv_GUI
  • FlowUs轻量化AI:趁这波升级专业版,全年无限AI助力笔记产出与二次编写