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

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

前一篇,我们使用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的端口也必须可以访问)

当使用 Docker 运行 FastDFS,浏览器可正常访问 Nginx,但项目上传文件出现 `read time out` 错误时,可以从以下几个方面进行排查和解决: ### 网络配置方面 - **检查容器网络连通性**:确保 FastDFS 容器和 Nginx 容器之间网络是连通的,可以使用 `ping` 和 `telnet` 命令进行测试。例如,进入 FastDFS 容器,使用 `ping` 命令测试与 Nginx 容器 IP 的连通性,使用 `telnet` 命令测试相应端口是否开放。 ```bash docker exec -it fastdfs_container_id ping nginx_container_ip docker exec -it fastdfs_container_id telnet nginx_container_ip 80 ``` - **检查防火墙设置**:查看宿主机和容器内的防火墙是否阻止了 FastDFS 和 Nginx 之间的通信。对于宿主机,可以使用 `iptables` 或 `firewalld` 检查规则,确保相关端口是开放的。对于容器,可以检查 Docker 的网络策略。 ### FastDFS 配置方面 - **检查 tracker 和 storage 配置**:确认 FastDFS 的 tracker 和 storage 配置文件中,IP 地址和端口配置正确。特别是 `tracker_server` 和 `storage_server` 的配置。 ```properties # tracker.conf 或 storage.conf 中 tracker_server = your_tracker_ip:22122 ``` - **检查 storage 服务状态**:确保 FastDFS 的 storage 服务正常运行,可以通过查看容器日志或使用 `fdfs_monitor` 命令进行检查。 ```bash docker logs fastdfs_storage_container_id fdfs_monitor /etc/fdfs/client.conf ``` ### Nginx 配置方面 - **检查 Nginx 配置文件**:确认 Nginx 配置文件中关于 FastDFS 的配置是否正确,特别是 `server` 块和 `location` 块的配置。 ```nginx server { listen 80; server_name your_domain; location ~/group[0-9]/ { ngx_fastdfs_module; } } ``` - **调整 Nginx 超时时间**:在 Nginx 配置文件中适当调整 `client_body_timeout` 和 `proxy_read_timeout` 等超时时间参数。 ```nginx http { client_body_timeout 60s; proxy_read_timeout 60s; # 其他配置 } ``` ### 项目配置方面 - **检查项目中 FastDFS 客户端配置**:确保项目中使用的 FastDFS 客户端配置文件中的 `tracker_server` 配置正确。 ```properties # 项目中的 fastdfs_client.conf tracker_server = your_tracker_ip:22122 ``` - **调整项目上传超时时间**:在项目代码中,适当调整上传文件的超时时间设置。例如,在 Java 项目中使用 FastDFS 客户端时,可以设置连接和读取超时时间。 ```java ClientGlobal.init("path/to/fastdfs_client.conf"); ClientGlobal.setG_connect_timeout(60 * 1000); // 连接超时时间 ClientGlobal.setG_network_timeout(60 * 1000); // 网络超时时间 ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值