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)
publicclassFastClientImporter{
}
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.测试
packagecom.leyou.upload.test;
importcom.github.tobato.fastdfs.domain.fdfs.StorePath;
importcom.github.tobato.fastdfs.domain.fdfs.ThumbImageConfig;
importcom.github.tobato.fastdfs.service.FastFileStorageClient;
importorg.junit.Test;
importorg.junit.runner.RunWith;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.boot.test.context.SpringBootTest;
importorg.springframework.test.context.junit4.SpringRunner;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
/**
*@authorjohn
*@date2019/12/6-15:09
*/
@SpringBootTest
@RunWith(SpringRunner.class)
publicclassFastDFSTest{
@Autowired
privateFastFileStorageClientstorageClient;
@Autowired
privateThumbImageConfigthumbImageConfig;
@Test
publicvoidtestUpload()throwsFileNotFoundException{
//要上传的文件
Filefile=newFile("D:\\imooc\\project\\images\\1.jpg");
//上传并保存图片,参数:1-上传的文件流2-文件的大小3-文件的后缀4-可以不管他
StorePathstorePath=this.storageClient.uploadFile(
newFileInputStream(file),file.length(),"jpg",null);
//带分组的路径
System.out.println(storePath.getFullPath());
//不带分组的路径
System.out.println(storePath.getPath());
}
@Test
publicvoidtestUploadAndCreateThumb()throwsFileNotFoundException{
Filefile=newFile("D:\\imooc\\project\\images\\2.jpg");
//上传并且生成缩略图
StorePathstorePath=this.storageClient.uploadImageAndCrtThumbImage(
newFileInputStream(file),file.length(),"png",null);
//带分组的路径
System.out.println(storePath.getFullPath());
//不带分组的路径
System.out.println(storePath.getPath());
//获取缩略图路径
Stringpath=thumbImageConfig.getThumbImagePath(storePath.getPath());
System.out.println(path);
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。