上一篇:使用MySQL,请用好 JSON 这张牌!
首先说一下从零开始自己去搭一个fastdfs挺麻烦,后来看到有人把做好的 docker 镜像传出来了,那搭建起来就很容易了
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,-eWEB_PORT=80指定nginx端口
测试fastdfs是否搭建成功
dockerexec-itfastdfs/bin/bashecho"HelloFastDFS!">index.htmlfdfs_test/etc/fdfs/client.confuploadindex.html
能返回 url 就意见搭建成功,搜索公众号互联网架构师回复“2T”,送你一份惊喜礼包。
这样 fastdfs 就搭建好啦。
下面进入 Spring Boot 整合部分
<groupId>com.github.tobato</groupId><artifactId>fastdfs-client</artifactId><version>1.26.2</version>
在 Spring Boot 启动类上加:
@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文件,搜索公众号互联网架构师回复“2T”,送你一份惊喜礼包。
#分布式文件系统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 就是能访问的地址
最后一张看效果
原文链接:/qq_37759106/article/details/82981023
感谢您的阅读,也欢迎您发表关于这篇文章的任何建议,关注我,技术不迷茫!小编到你上高速。
· END ·
最后,关注公众号互联网架构师,在后台回复:2T,可以获取我整理的 Java 系列面试题和答案,非常齐全。
正文结束
推荐阅读 ↓↓↓
1.心态崩了!税前2万4,到手1万4,年终奖扣税方式1月1日起施行~
2.深圳一普通中学老师工资单曝光,秒杀程序员,网友:敢问是哪个学校毕业的?
3.从零开始搭建创业公司后台技术栈
4.程序员一般可以从什么平台接私活?
5.清华大学: 元宇宙研究报告!
6.为什么国内 996 干不过国外的 955呢?
7.这封“领导痛批95后下属”的邮件,句句扎心!
8.15张图看懂瞎忙和高效的区别!