欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

springboot2.0如何通过fastdfs实现文件分布式上传

时间:2021-04-27 09:08:14|栏目:JAVA代码|点击:

这篇文章主要介绍了springboot2.0如何通过fastdfs实现文件分布式上传,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

1. 引入依赖

在父工程中,我们已经管理了依赖,版本为:

<fastDFS.client.version>1.26.7</fastDFS.client.version>

因此,这里我们直接在工程的pom.xml中引入坐标即可:

<dependency>
  <groupId>com.github.tobato</groupId>
  <artifactId>fastdfs-client</artifactId>
</dependency>

@Configuration
@Import(FdfsClientConfig.class)
// 解决jmx重复注册bean的问题
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class FastClientImporter {
}

2. 在application.yml文件中编写FastDFS属性

fdfs:
 so-timeout: 1501 # 超时时间
 connect-timeout: 601 # 连接超时时间
 thumb-image: # 缩略图
  width: 60
  height: 60
 tracker-list: # tracker地址:你的虚拟机服务器地址+端口(默认是22122)
  - 192.168.0.22:22122

3. 测试

package com.leyou.upload.test;

import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.fdfs.ThumbImageConfig;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

/**
 * @author john
 * @date 2019/12/6 - 15:09
 */
@SpringBootTest
@RunWith(SpringRunner.class)
public class FastDFSTest {

  @Autowired
  private FastFileStorageClient storageClient;

  @Autowired
  private ThumbImageConfig thumbImageConfig;

  @Test
  public void testUpload() throws FileNotFoundException {
    // 要上传的文件
    File file = new File("D:\\imooc\\project\\images\\1.jpg");
    // 上传并保存图片,参数:1-上传的文件流 2-文件的大小 3-文件的后缀 4-可以不管他
    StorePath storePath = this.storageClient.uploadFile(
        new FileInputStream(file), file.length(), "jpg", null);
    // 带分组的路径
    System.out.println(storePath.getFullPath());
    // 不带分组的路径
    System.out.println(storePath.getPath());
  }
  @Test
  public void testUploadAndCreateThumb() throws FileNotFoundException {
    File file = new File("D:\\imooc\\project\\images\\2.jpg");
    // 上传并且生成缩略图
    StorePath storePath = this.storageClient.uploadImageAndCrtThumbImage(
        new FileInputStream(file), file.length(), "png", null);
    // 带分组的路径
    System.out.println(storePath.getFullPath());
    // 不带分组的路径
    System.out.println(storePath.getPath());
    // 获取缩略图路径
    String path = thumbImageConfig.getThumbImagePath(storePath.getPath());
    System.out.println(path);
  }
}

上一篇:idea git未提交代码文件名字变色(图解)

栏    目:JAVA代码

下一篇:mybatis使用pageHelper插件进行查询分页

本文标题:springboot2.0如何通过fastdfs实现文件分布式上传

本文地址:http://www.codeinn.net/misctech/109710.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有