资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

springboot2.0中怎么利用fastdfs实现文件分布式上传

这篇文章给大家介绍springboot2.0中怎么利用fastdfs实现文件分布式上传,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

创新互联建站始终坚持【策划先行,效果至上】的经营理念,通过多达十年累计超上千家客户的网站建设总结了一套系统有效的全网营销推广解决方案,现已广泛运用于各行各业的客户,其中包括:花箱等企业,备受客户称赞。

1. 引入依赖

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

1.26.7

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

 com.github.tobato  fastdfs-client

@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);  }}

关于springboot2.0中怎么利用fastdfs实现文件分布式上传就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


文章题目:springboot2.0中怎么利用fastdfs实现文件分布式上传
标题URL:http://cdkjz.cn/article/jisihs.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220