作者:kalibiubiubiu
/qq_37759106/article/details/82981023
首先说一下从零开始自己去搭一个fastdfs有多不顺,搭起来步骤繁琐,很麻烦,后来看到有人把做好的docker镜像传出来了,那搭建起来就很容易了
有服务器的可以自己在服务器上玩玩,没有的可以新建一个centos7.5虚拟机玩玩,遇到虚拟机不能上网和换阿里云的源的问题可以参考:
/qq_37759106/article/details/82985113
1.第一步安装docker:
在root权限下
yuminstall-ydocker-io#安装dockerservicedockerstar#启动dockerdocker-v#查看docker版本
2.拉取镜像
dockerpullqbanxiaoli/fastdfs
启动fastdfs
dockerrun-d--restart=always--privileged=true--net=host--name=fastdfs-eIP=192.168.127.131-eWEB_PORT=80-v${HOME}/fastdfs:/var/local/fdfsqbanxiaoli/fastdfs
IP 后面是你的服务器公网ip或者虚拟机的IP,-e WEB_PORT=80 指定nginx端口
测试fastdfs是否搭建成功
dockerexec-itfastdfs/bin/bashecho"HelloFastDFS!">index.htmlfdfs_test/etc/fdfs/client.confuploadindex.html
能返回url就意见搭建成功
这样fastdfs就搭建好啦
下面进入springboot整合部分
加入依赖
<dependency><groupId>com.github.tobato</groupId><artifactId>fastdfs-client</artifactId><version>1.26.2</version></dependency>
在springboot启动类上加
@Import(FdfsClientConfig.class)@EnableMBeanExport(registration=RegistrationPolicy.IGNORE_EXISTING)
创建FastDFSClient工具类
packagecom.mon;importcom.github.tobato.fastdfs.conn.FdfsWebServer;importcom.github.tobato.fastdfs.domain.StorePath;importcom.github.tobato.fastdfs.proto.storage.DownloadByteArray;importcom.github.tobato.fastdfs.service.FastFileStorageClient;mons.io.FilenameUtils;mons.lang3.StringUtils;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importorg.springframework.beans.factory.annotation.Autowired;importorg.ponent;importorg.springframework.web.multipart.MultipartFile;importjava.io.*;@ComponentpublicclassFastDFSClient{privatestaticLoggerlog=LoggerFactory.getLogger(FastDFSClient.class);privatestaticFastFileStorageClientfastFileStorageClient;privatestaticFdfsWebServerfdfsWebServer;@AutowiredpublicvoidsetFastDFSClient(FastFileStorageClientfastFileStorageClient,FdfsWebServerfdfsWebServer){FastDFSClient.fastFileStorageClient=fastFileStorageClient;FastDFSClient.fdfsWebServer=fdfsWebServer;}/***@parammultipartFile文件对象*@return返回文件地址*@authorqbanxiaoli*@description上传文件*/publicstaticStringuploadFile(MultipartFilemultipartFile){try{StorePathstorePath=fastFileStorageClient.uploadFile(multipartFile.getInputStream(),multipartFile.getSize(),FilenameUtils.getExtension(multipartFile.getOriginalFilename()),null);returnstorePath.getFullPath();}catch(IOExceptione){log.error(e.getMessage());returnnull;}}/***@parammultipartFile图片对象*@return返回图片地址*@authorqbanxiaoli*@description上传缩略图*/publicstaticStringuploadImageAndCrtThumbImage(MultipartFilemultipartFile){try{StorePathstorePath=fastFileStorageClient.uploadImageAndCrtThumbImage(multipartFile.getInputStream(),multipartFile.getSize(),FilenameUtils.getExtension(multipartFile.getOriginalFilename()),null);returnstorePath.getFullPath();}catch(Exceptione){log.error(e.getMessage());returnnull;}}/***@paramfile文件对象*@return返回文件地址*@authorqbanxiaoli*@description上传文件*/publicstaticStringuploadFile(Filefile){try{FileInputStreaminputStream=newFileInputStream(file);StorePathstorePath=fastFileStorageClient.uploadFile(inputStream,file.length(),FilenameUtils.getExtension(file.getName()),null);returnstorePath.getFullPath();}catch(Exceptione){log.error(e.getMessage());returnnull;}}/***@paramfile图片对象*@return返回图片地址*@authorqbanxiaoli*@description上传缩略图*/publicstaticStringuploadImageAndCrtThumbImage(Filefile){try{FileInputStreaminputStream=newFileInputStream(file);StorePathstorePath=fastFileStorageClient.uploadImageAndCrtThumbImage(inputStream,file.length(),FilenameUtils.getExtension(file.getName()),null);returnstorePath.getFullPath();}catch(Exceptione){log.error(e.getMessage());returnnull;}}/***@parambytesbyte数组*@paramfileExtension文件扩展名*@return返回文件地址*@authorqbanxiaoli*@description将byte数组生成一个文件上传*/publicstaticStringuploadFile(byte[]bytes,StringfileExtension){ByteArrayInputStreamstream=newByteArrayInputStream(bytes);StorePathstorePath=fastFileStorageClient.uploadFile(stream,bytes.length,fileExtension,null);returnstorePath.getFullPath();}/***@paramfileUrl文件访问地址*@paramfile文件保存路径*@authorqbanxiaoli*@description下载文件*/publicstaticbooleandownloadFile(StringfileUrl,Filefile){try{StorePathstorePath=StorePath.praseFromUrl(fileUrl);byte[]bytes=fastFileStorageClient.downloadFile(storePath.getGroup(),storePath.getPath(),newDownloadByteArray());FileOutputStreamstream=newFileOutputStream(file);stream.write(bytes);}catch(Exceptione){log.error(e.getMessage());returnfalse;}returntrue;}/***@paramfileUrl文件访问地址*@authorqbanxiaoli*@description删除文件*/publicstaticbooleandeleteFile(StringfileUrl){if(StringUtils.isEmpty(fileUrl)){returnfalse;}try{StorePathstorePath=StorePath.praseFromUrl(fileUrl);fastFileStorageClient.deleteFile(storePath.getGroup(),storePath.getPath());}catch(Exceptione){log.error(e.getMessage());returnfalse;}returntrue;}//封装文件完整URL地址publicstaticStringgetResAccessUrl(Stringpath){Stringurl=fdfsWebServer.getWebServerUrl()+path;log.info("上传文件地址为:\n"+url);returnurl;}}
配置yml文件
#分布式文件系统fastdfs配置fdfs:#socket连接超时时长soTimeout:1500#连接tracker服务器超时时长connectTimeout:600pool:#从池中借出的对象的最大数目max-total:153#获取连接时的最大等待毫秒数100max-wait-millis:102#缩略图生成参数,可选thumbImage:width:150height:150#跟踪服务器tracker_server请求地址,支持多个,这里只有一个,如果有多个在下方加-x.x.x.x:porttrackerList:-192.168.127.131:22122##存储服务器storage_server访问地址web-server-url:http://192.168.127.131/spring:http:multipart:max-file-size:100MB#最大支持文件大小max-request-size:100MB#最大支持请求大小
测试类
@RunWith(SpringRunner.class)@SpringBootTestpublicclassFileClientApplicationTests{@TestpublicvoidUpload(){StringfileUrl=this.getClass().getResource("/test.jpg").getPath();Filefile=newFile(fileUrl);Stringstr=FastDFSClient.uploadFile(file);FastDFSClient.getResAccessUrl(str);}@TestpublicvoidDelete(){FastDFSClient.deleteFile("group1/M00/00/00/rBEAClu8OiSAFbN_AAbhXQnXzvw031.jpg");}}
运行测试类
返回的url就是能访问的地址
最后一张看效果
END
推荐好文
强大,10k+点赞的 SpringBoot 后台管理系统竟然出了详细教程!分享一套基于SpringBoot和Vue的企业级中后台开源项目,代码很规范!能挣钱的,开源 SpringBoot 商城系统,功能超全,超漂亮!