<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Spring Boot 整合 FastDFS 搭建分布式文件服務(wù)器

          共 7236字,需瀏覽 15分鐘

           ·

          2022-02-22 14:18

          不點(diǎn)藍(lán)字關(guān)注,我們哪來(lái)故事?


          一個(gè)指導(dǎo)程序員進(jìn)入大公司/獨(dú)角獸?的精品社群,致力于分享職場(chǎng)達(dá)人的專業(yè)打法,包括「學(xué)習(xí)路線+簡(jiǎn)歷模板+實(shí)習(xí)避坑+筆試面試+試用轉(zhuǎn)正+升職加薪+跳槽技巧」。

          點(diǎn)這里去了解,劍指大廠吧!







          來(lái)源:blog.csdn.net/qq_37759106/
          article/details/82981023

          首先說(shuō)一下從零開(kāi)始自己去搭一個(gè)fastdfs挺麻煩,后來(lái)看到有人把做好的 docker 鏡像傳出來(lái)了,那搭建起來(lái)就很容易了

          1.第一步安裝docker:

          在 root 權(quán)限下

          yum?install?-y?docker-io?#安裝docker
          service?docker?star?#啟動(dòng)docker
          docker?-v?#?查看docker版本

          2. 拉取鏡像

          docker?pull?qbanxiaoli/fastdfs

          啟動(dòng) fastdfs

          docker?run?-d?--restart=always?--privileged=true?--net=host?--name=fastdfs?-e?IP=192.168.127.131?-e?WEB_PORT=80?-v?${HOME}/fastdfs:/var/local/fdfs?qbanxiaoli/fastdfs

          IP 后面是你的服務(wù)器公網(wǎng)ip或者虛擬機(jī)的IP,-e WEB_PORT=80 指定 nginx 端口

          測(cè)試fastdfs是否搭建成功

          docker?exec?-it?fastdfs?/bin/bash
          echo?"Hello?FastDFS!">index.html
          fdfs_test?/etc/fdfs/client.conf?upload?index.html

          能返回 url 就意見(jiàn)搭建成功

          這樣 fastdfs 就搭建好啦。

          下面進(jìn)入 Spring Boot 整合部分

          <groupId>com.github.tobatogroupId>
          ???<artifactId>fastdfs-clientartifactId>
          <version>1.26.2version>

          在 Spring Boot 啟動(dòng)類上加:

          @Import(FdfsClientConfig.class)
          @EnableMBeanExport(registration?=?RegistrationPolicy.IGNORE_EXISTING)

          創(chuàng)建FastDFSClient工具類

          package?com.yd.client.common;

          import?com.github.tobato.fastdfs.conn.FdfsWebServer;
          import?com.github.tobato.fastdfs.domain.StorePath;
          import?com.github.tobato.fastdfs.proto.storage.DownloadByteArray;
          import?com.github.tobato.fastdfs.service.FastFileStorageClient;
          import?org.apache.commons.io.FilenameUtils;
          import?org.apache.commons.lang3.StringUtils;
          import?org.slf4j.Logger;
          import?org.slf4j.LoggerFactory;
          import?org.springframework.beans.factory.annotation.Autowired;
          import?org.springframework.stereotype.Component;
          import?org.springframework.web.multipart.MultipartFile;

          import?java.io.*;

          @Component
          public?class?FastDFSClient?{

          ????private?static?Logger?log?=LoggerFactory.getLogger(FastDFSClient.class);

          ????private?static?FastFileStorageClient?fastFileStorageClient;

          ????private?static?FdfsWebServer?fdfsWebServer;

          ????@Autowired
          ????public?void?setFastDFSClient(FastFileStorageClient?fastFileStorageClient,?FdfsWebServer?fdfsWebServer)?{
          ????????FastDFSClient.fastFileStorageClient?=?fastFileStorageClient;
          ????????FastDFSClient.fdfsWebServer?=?fdfsWebServer;
          ????}

          ????/**
          ?????*?@param?multipartFile?文件對(duì)象
          ?????*?@return?返回文件地址
          ?????*?@author?qbanxiaoli
          ?????*?@description?上傳文件
          ?????*/

          ????public?static?String?uploadFile(MultipartFile?multipartFile)?{
          ????????try?{
          ????????????StorePath?storePath?=?fastFileStorageClient.uploadFile(multipartFile.getInputStream(),?multipartFile.getSize(),?FilenameUtils.getExtension(multipartFile.getOriginalFilename()),?null);
          ????????????return?storePath.getFullPath();
          ????????}?catch?(IOException?e)?{
          ????????????log.error(e.getMessage());
          ????????????return?null;
          ????????}
          ????}

          ????/**
          ?????*?@param?multipartFile?圖片對(duì)象
          ?????*?@return?返回圖片地址
          ?????*?@author?qbanxiaoli
          ?????*?@description?上傳縮略圖
          ?????*/

          ????public?static?String?uploadImageAndCrtThumbImage(MultipartFile?multipartFile)?{
          ????????try?{
          ????????????StorePath?storePath?=?fastFileStorageClient.uploadImageAndCrtThumbImage(multipartFile.getInputStream(),?multipartFile.getSize(),?FilenameUtils.getExtension(multipartFile.getOriginalFilename()),?null);
          ????????????return?storePath.getFullPath();
          ????????}?catch?(Exception?e)?{
          ????????????log.error(e.getMessage());
          ????????????return?null;
          ????????}
          ????}

          ????/**
          ?????*?@param?file?文件對(duì)象
          ?????*?@return?返回文件地址
          ?????*?@author?qbanxiaoli
          ?????*?@description?上傳文件
          ?????*/

          ????public?static?String?uploadFile(File?file)?{
          ????????try?{
          ????????????FileInputStream?inputStream?=?new?FileInputStream(file);
          ????????????StorePath?storePath?=?fastFileStorageClient.uploadFile(inputStream,?file.length(),?FilenameUtils.getExtension(file.getName()),?null);
          ????????????return?storePath.getFullPath();
          ????????}?catch?(Exception?e)?{
          ????????????log.error(e.getMessage());
          ????????????return?null;
          ????????}
          ????}

          ????/**
          ?????*?@param?file?圖片對(duì)象
          ?????*?@return?返回圖片地址
          ?????*?@author?qbanxiaoli
          ?????*?@description?上傳縮略圖
          ?????*/

          ????public?static?String?uploadImageAndCrtThumbImage(File?file)?{
          ????????try?{
          ????????????FileInputStream?inputStream?=?new?FileInputStream(file);
          ????????????StorePath?storePath?=?fastFileStorageClient.uploadImageAndCrtThumbImage(inputStream,?file.length(),?FilenameUtils.getExtension(file.getName()),?null);
          ????????????return?storePath.getFullPath();
          ????????}?catch?(Exception?e)?{
          ????????????log.error(e.getMessage());
          ????????????return?null;
          ????????}
          ????}

          ????/**
          ?????*?@param?bytes?byte數(shù)組
          ?????*?@param?fileExtension?文件擴(kuò)展名
          ?????*?@return?返回文件地址
          ?????*?@author?qbanxiaoli
          ?????*?@description?將byte數(shù)組生成一個(gè)文件上傳
          ?????*/

          ????public?static?String?uploadFile(byte[]?bytes,?String?fileExtension)?{
          ????????ByteArrayInputStream?stream?=?new?ByteArrayInputStream(bytes);
          ????????StorePath?storePath?=?fastFileStorageClient.uploadFile(stream,?bytes.length,?fileExtension,?null);
          ????????return?storePath.getFullPath();
          ????}

          ????/**
          ?????*?@param?fileUrl?文件訪問(wèn)地址
          ?????*?@param?file?文件保存路徑
          ?????*?@author?qbanxiaoli
          ?????*?@description?下載文件
          ?????*/

          ????public?static?boolean?downloadFile(String?fileUrl,?File?file)?{
          ????????try?{
          ????????????StorePath?storePath?=?StorePath.praseFromUrl(fileUrl);
          ????????????byte[]?bytes?=?fastFileStorageClient.downloadFile(storePath.getGroup(),?storePath.getPath(),?new?DownloadByteArray());
          ????????????FileOutputStream?stream?=?new?FileOutputStream(file);
          ????????????stream.write(bytes);
          ????????}?catch?(Exception?e)?{
          ????????????log.error(e.getMessage());
          ????????????return?false;
          ????????}
          ????????return?true;
          ????}

          ????/**
          ?????*?@param?fileUrl?文件訪問(wèn)地址
          ?????*?@author?qbanxiaoli
          ?????*?@description?刪除文件
          ?????*/

          ????public?static?boolean?deleteFile(String?fileUrl)?{
          ????????if?(StringUtils.isEmpty(fileUrl))?{
          ????????????return?false;
          ????????}
          ????????try?{
          ????????????StorePath?storePath?=?StorePath.praseFromUrl(fileUrl);
          ????????????fastFileStorageClient.deleteFile(storePath.getGroup(),?storePath.getPath());
          ????????}?catch?(Exception?e)?{
          ????????????log.error(e.getMessage());
          ????????????return?false;
          ????????}
          ????????return?true;
          ????}

          ????//?封裝文件完整URL地址
          ????public?static?String?getResAccessUrl(String?path)?{
          ????????String?url?=?fdfsWebServer.getWebServerUrl()?+?path;
          ????????log.info("上傳文件地址為:\n"?+?url);
          ????????return?url;
          ????}

          }

          配置yml文件

          #?分布式文件系統(tǒng)fastdfs配置
          fdfs:
          ??#?socket連接超時(shí)時(shí)長(zhǎng)
          ??soTimeout:?1500
          ??#?連接tracker服務(wù)器超時(shí)時(shí)長(zhǎng)
          ??connectTimeout:?600
          ??pool:
          ????#?從池中借出的對(duì)象的最大數(shù)目
          ????max-total:?153
          ????#?獲取連接時(shí)的最大等待毫秒數(shù)100
          ????max-wait-millis:?102
          ??#?縮略圖生成參數(shù),可選
          ??thumbImage:
          ????width:?150
          ????height:?150
          ??#?跟蹤服務(wù)器tracker_server請(qǐng)求地址,支持多個(gè),這里只有一個(gè),如果有多個(gè)在下方加-?x.x.x.x:port
          ??trackerList:
          ????-?192.168.127.131:22122
          ??#
          ??#?存儲(chǔ)服務(wù)器storage_server訪問(wèn)地址
          ??web-server-url:?http://192.168.127.131/
          ??spring:
          ????http:
          ??????multipart:
          ????????max-file-size:?100MB?#?最大支持文件大小
          ????????max-request-size:?100MB?#?最大支持請(qǐng)求大小
          **測(cè)試類**@RunWith(SpringRunner.class)@SpringBootTestpublic?class?FileClientApplicationTests?{??@Test?public?void?Upload()?{??String?fileUrl?=?this.getClass().getResource("/test.jpg").getPath();??File?file?=?new?File(fileUrl);??String?str?=?FastDFSClient.uploadFile(file);??FastDFSClient.getResAccessUrl(str);?}??@Test?public?void?Delete()?{??FastDFSClient.deleteFile("group1/M00/00/00/rBEAClu8OiSAFbN_AAbhXQnXzvw031.jpg");?}}

          運(yùn)行測(cè)試類

          返回的 url 就是能訪問(wèn)的地址

          最后一張看效果

          推薦


          歡迎加入我的知識(shí)星球,一起劍指大廠,不斷晉升加薪。

          劍指大廠不僅是一個(gè)獲取信息的圈子,還是一個(gè)規(guī)劃職業(yè)的導(dǎo)師。已在知識(shí)星球,更新如下(點(diǎn)擊下圖了解):


          //////?END?//////
          ↓ 點(diǎn)擊下方關(guān)注,看更多架構(gòu)分享?↓

          ↓ 或加泥瓦匠微信,交流更多技術(shù)?↓

          瀏覽 93
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  成人激情直播 | 色吧大香蕉 | 日本中文字幕中文翻译歌词 | 天天狠天天干 | 天天撸日日干 |