springboot2.0结合fastdfs实现文件分布式上传

1. 引入依赖

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

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

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

1<dependency> 2 <groupId>com.github.tobato</groupId> 3 <artifactId>fastdfs-client</artifactId> 4</dependency>

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

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

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

3. 测试

1package com.leyou.upload.test; 2 3import com.github.tobato.fastdfs.domain.fdfs.StorePath; 4import com.github.tobato.fastdfs.domain.fdfs.ThumbImageConfig; 5import com.github.tobato.fastdfs.service.FastFileStorageClient; 6import org.junit.Test; 7import org.junit.runner.RunWith; 8import org.springframework.beans.factory.annotation.Autowired; 9import org.springframework.boot.test.context.SpringBootTest; 10import org.springframework.test.context.junit4.SpringRunner; 11 12import java.io.File; 13import java.io.FileInputStream; 14import java.io.FileNotFoundException; 15 16/** 17 * @author john 18 * @date 2019/12/6 - 15:09 19 */ 20@SpringBootTest 21@RunWith(SpringRunner.class) 22public class FastDFSTest { 23 24 @Autowired 25 private FastFileStorageClient storageClient; 26 27 @Autowired 28 private ThumbImageConfig thumbImageConfig; 29 30 @Test 31 public void testUpload() throws FileNotFoundException { 32 // 要上传的文件 33 File file = new File("D:\\imooc\\project\\images\\1.jpg"); 34 // 上传并保存图片,参数:1-上传的文件流 2-文件的大小 3-文件的后缀 4-可以不管他 35 StorePath storePath = this.storageClient.uploadFile( 36 new FileInputStream(file), file.length(), "jpg", null); 37 // 带分组的路径 38 System.out.println(storePath.getFullPath()); 39 // 不带分组的路径 40 System.out.println(storePath.getPath()); 41 } 42 43 @Test 44 public void testUploadAndCreateThumb() throws FileNotFoundException { 45 File file = new File("D:\\imooc\\project\\images\\2.jpg"); 46 // 上传并且生成缩略图 47 StorePath storePath = this.storageClient.uploadImageAndCrtThumbImage( 48 new FileInputStream(file), file.length(), "png", null); 49 // 带分组的路径 50 System.out.println(storePath.getFullPath()); 51 // 不带分组的路径 52 System.out.println(storePath.getPath()); 53 // 获取缩略图路径 54 String path = thumbImageConfig.getThumbImagePath(storePath.getPath()); 55 System.out.println(path); 56 } 57}

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )