fastdfs文件上传 read timeout_SpringBoot2.0 整合 FastDFS

本文详细介绍如何在SpringBoot 2.0项目中集成并使用FastDFS文件管理系统,包括依赖配置、核心配置类编写及文件上传、删除的操作示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前一篇,我们使用docker来部署FastDFS(见使用Docker来搭建fastdfs),这一天我们来试着使用SpringBoot2.0来访问FastDFS

采用maven配置

1、配置依赖包,修改pom文件

com.github.tobato    fastdfs-client    1.26.5

2、springboot配置文件添加

fdfs:  # 链接超时  connect-timeout: 60  # 读取时间  so-timeout: 60  # 生成缩略图参数  thumb-image:    width: 150    height: 150  tracker-list: 192.168.72.130:22122

3、核心配置类

import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.EnableMBeanExport;import org.springframework.context.annotation.Import;import org.springframework.jmx.support.RegistrationPolicy;import com.github.tobato.fastdfs.FdfsClientConfig;@Configuration@Import(FdfsClientConfig.class)// Jmx重复注册bean的问题@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)public class DfsConfig {}

4、文件工具类,自己按实际去修改

@Slf4jpublic class FileDfsUtil {       @Resource    private FastFileStorageClient storageClient ;    /**     * 上传文件     */    public String upload(MultipartFile multipartFile) throws Exception{        String originalFilename = multipartFile.getOriginalFilename().                                  substring(multipartFile.getOriginalFilename().                                  lastIndexOf(".") + 1);        StorePath storePath = this.storageClient.uploadImageAndCrtThumbImage(                              multipartFile.getInputStream(),                              multipartFile.getSize(),originalFilename , null);        return storePath.getFullPath() ;    }    /**     * 删除文件     */    public void deleteFile(String fileUrl) {        if (StringUtils.isEmpty(fileUrl)) {            log.info("fileUrl == >>文件路径为空...");            return;        }        try {            StorePath storePath = StorePath.parseFromUrl(fileUrl);            storageClient.deleteFile(storePath.getGroup(), storePath.getPath());        } catch (Exception e) {        log.info(e.getMessage());        }    }

4、请求代码示例

@RequestMapping("/fastDfs")@ResponseBodypublic  Object fastDfs() throws IOException{File fileRoot=new File("F:/images");File[] files=fileRoot.listFiles();        for(File file:files){        FileInputStream inputFile = new FileInputStream(file);        byte[] buffer = new byte[(int)file.length()];            inputFile.read(buffer);            inputFile.close();    return fileDfsUtil.uploadFile(Base64Utils.encodeToString(buffer), "jpg");        }        return 1;}

请求上面接口后返回

e2ba6f503ba589bf849b0d67be302843.png

访问 nginx地址

http://127.0.0.1:8888/group1/M00/00/00/rBAGil5Xb5aAON-EAAJV6tHqsvQ252.jpg

可以显示图片,即为成功

需要注意的是必须保证springboot所在服务器可以访问storage的网络 例如可以访问storage:23000端口(当然tracker的端口也必须可以访问)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值