最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot集成FastDFS依賴實現(xiàn)文件上傳的示例

 更新時間:2021年05月11日 14:36:14   作者:niceyoo  
這篇文章主要介紹了SpringBoot集成FastDFS依賴實現(xiàn)文件上傳,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

前言

對FastDFS文件系統(tǒng)安裝后的使用。

FastDFS的安裝請參考這篇:Docker中搭建FastDFS文件系統(tǒng)(多圖)

本文環(huán)境:IDEA + JDK1.8 + Maven

本文項目代碼:fastdfs_jb51.rar

1、引入依賴

簡單說一下這個依賴部分,目前大部分都是采用的如下依賴:

<!-- https://mvnrepository.com/artifact/net.oschina.zcx7878/fastdfs-client-java -->
<dependency>
    <groupId>net.oschina.zcx7878</groupId>
    <artifactId>fastdfs-client-java</artifactId>
    <version>1.27.0.0</version>
</dependency>

本著不重復造輪子,且為了使用方便我們可以去GitHub找一個集成好的依賴:

https://github.com/tobato/FastDFS_Client

<dependency>
    <groupId>com.github.tobato</groupId>
    <artifactId>fastdfs-client</artifactId>
    <version>1.27.2</version>
</dependency>

2、將Fdfs配置引入項目

只需要創(chuàng)建一個配置類就可以了:

@Configuration
@Import(FdfsClientConfig.class)
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class ComponetImport {
    // 導入依賴組件
}

參考截圖:

3、在application.yml當中配置Fdfs相關(guān)參數(shù)

根據(jù)自己情況修改相應ip地址及端口號:

server:
  port: 8080

ip: 10.211.55.4 # 根據(jù)自己FastDFS服務器修改

fdfs:
  so-timeout: 1501
  connect-timeout: 601
  thumb-image:             #縮略圖生成參數(shù)
    width: 150
    height: 150
  tracker-list:            #TrackerList參數(shù),支持多個
    - 10.211.55.4:22122
  web-server-url: http://${ip}:8888/

4、client封裝工具類

創(chuàng)建FastDFSClient.java包裝工具類,方便后面使用:

import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.Charset;

@Component
public class FastDFSClient {

    @Autowired
    private FastFileStorageClient storageClient;

    @Autowired
    private FdfsWebServer fdfsWebServer;

    /**
     * 上傳文件
     * @param file 文件對象
     * @return 文件訪問地址
     * @throws IOException
     */
    public String uploadFile(MultipartFile file) throws IOException {
        StorePath storePath = storageClient.uploadFile(file.getInputStream(),file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()),null);
        return getResAccessUrl(storePath);
    }

    /**
     * 上傳文件
     * @param file 文件對象
     * @return 文件訪問地址
     * @throws IOException
     */
    public String uploadFile(File file) throws IOException {
        FileInputStream inputStream = new FileInputStream (file);
        StorePath storePath = storageClient.uploadFile(inputStream,file.length(), FilenameUtils.getExtension(file.getName()),null);
        return getResAccessUrl(storePath);
    }

    /**
     * 將一段字符串生成一個文件上傳
     * @param content 文件內(nèi)容
     * @param fileExtension
     * @return
     */
    public String uploadFile(String content, String fileExtension) {
        byte[] buff = content.getBytes(Charset.forName("UTF-8"));
        ByteArrayInputStream stream = new ByteArrayInputStream(buff);
        StorePath storePath = storageClient.uploadFile(stream,buff.length, fileExtension,null);
        return getResAccessUrl(storePath);
    }

    /**
     * 封裝圖片完整URL地址
      */
    private String getResAccessUrl(StorePath storePath) {
        String fileUrl = fdfsWebServer.getWebServerUrl() + storePath.getFullPath();
        return fileUrl;
    }

    /**
     * 刪除文件
     * @param fileUrl 文件訪問地址
     * @return
     */
    public void deleteFile(String fileUrl) {
        if (StringUtils.isEmpty(fileUrl)) {
            return;
        }
        try {
            StorePath storePath = StorePath.parseFromUrl(fileUrl);
            storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
        } catch (FdfsUnsupportStorePathException e) {
            System.out.println(e.getMessage());
            /** TODO 只是測試,所以未使用,logger,正式環(huán)境請修改打印方式 **/
        }
    }
    /**
     * 下載文件
     *
     * @param fileUrl 文件URL
     * @return 文件字節(jié)
     * @throws IOException
     */
    public byte[] downloadFile(String fileUrl) throws IOException {
        String group = fileUrl.substring(0, fileUrl.indexOf("/"));
        String path = fileUrl.substring(fileUrl.indexOf("/") + 1);
        DownloadByteArray downloadByteArray = new DownloadByteArray();
        byte[] bytes = storageClient.downloadFile(group, path, downloadByteArray);
        return bytes;
    }

}

5、創(chuàng)建Conttoler測試類

5.1 文件上傳測試

@RestController
@RequestMapping("/file")
public class FileUploadController {

    @Autowired
    private FastDFSClient fastDFSClient;

    /**
     * 上傳
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping("/upload")
    public String uploadFile(MultipartFile file) throws IOException {
        return fastDFSClient.uploadFile(file);
    }

}

執(zhí)行效果截圖:

5.2、下載文件測試

@RestController
@RequestMapping("/file")
public class FileUploadController {

    @Autowired
    private FastDFSClient fastDFSClient;

    /**
     * 下載
     * @param fileUrl
     * @param response
     * @throws IOException
     */
    @RequestMapping("/download")
    public void downloadFile(String fileUrl, HttpServletResponse response) throws IOException {
        byte[] bytes = fastDFSClient.downloadFile(fileUrl);
        /** TODO 這里只是為了整合fastdfs,所以寫死了文件格式。需要在上傳的時候保存文件名。下載的時候使用對應的格式 **/
        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("sb.xlsx", "UTF-8"));
        response.setCharacterEncoding("UTF-8");
        ServletOutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
            outputStream.write(bytes);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                outputStream.flush();
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

測試下載路徑:

http://127.0.0.1:8080/file/download?fileUrl=group1/M00/00/00/CtM3BF84r4SAEPDgAABoGL78QcY682.jpg

拼接的參數(shù)為:group1/M00/00/00/CtM3BF84r4SAEPDgAABoGL78QcY682.jpg

大家想修改路徑的話,需要同步修改 downloadFile() 方法里的分隔方式。

到此這篇關(guān)于SpringBoot集成FastDFS依賴實現(xiàn)文件上傳的示例的文章就介紹到這了,更多相關(guān)SpringBoot FastDFS文件上傳內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Spring Boot web項目的TDD流程

    Spring Boot web項目的TDD流程

    TDD(Test-driven development) 測試驅(qū)動開發(fā),簡單點說就是編寫測試,再編寫代碼。這是首要一條,不可動搖的一條,先寫代碼后寫測試的都是假TDD。
    2021-05-05
  • SSH框架網(wǎng)上商城項目第2戰(zhàn)之基本增刪查改、Service和Action的抽取

    SSH框架網(wǎng)上商城項目第2戰(zhàn)之基本增刪查改、Service和Action的抽取

    SSH框架網(wǎng)上商城項目第2戰(zhàn)之基本增刪查改、Service和Action的抽取,感興趣的小伙伴們可以參考一下
    2016-05-05
  • 淺談Java中格式化輸出

    淺談Java中格式化輸出

    這篇文章主要介紹了Java中格式化輸出,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-04-04
  • java實現(xiàn)畫圖板上畫一條直線

    java實現(xiàn)畫圖板上畫一條直線

    這篇文章主要為大家詳細介紹了java實現(xiàn)畫圖板上畫一條直線,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • java爬蟲Jsoup主要類及功能使用詳解

    java爬蟲Jsoup主要類及功能使用詳解

    這篇文章主要為大家介紹了java爬蟲Jsoup主要類及功能使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • Windows下Java環(huán)境配置的超詳細教程

    Windows下Java環(huán)境配置的超詳細教程

    這篇文章主要給大家介紹了關(guān)于Windows下Java環(huán)境配置的超詳細教程,文中通過圖文將配置的過程介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2023-01-01
  • 一文給你通俗易懂的講解Java異常

    一文給你通俗易懂的講解Java異常

    這篇文章主要給大家介紹了關(guān)于Java異常的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-05-05
  • Java面向?qū)ο笾鄳B(tài)

    Java面向?qū)ο笾鄳B(tài)

    這篇文章主要介紹了Java面向?qū)ο笾鄳B(tài),文章以什么是多態(tài)、多態(tài)的實現(xiàn)條件、多態(tài)的訪問特點、多態(tài)的優(yōu)點和缺點的相關(guān)資料展開文章內(nèi)容,需要的小伙伴可以參考一下
    2021-10-10
  • Spring Security 安全認證的示例代碼

    Spring Security 安全認證的示例代碼

    這篇文章主要介紹了Spring Security 安全認證的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-10-10
  • java關(guān)鍵字static學習心得

    java關(guān)鍵字static學習心得

    本篇文章給大家分享一篇關(guān)于java關(guān)鍵字static的學習心得,有這方面需要的朋友學習下吧。
    2018-01-01

最新評論

兴山县| 彭阳县| 绥阳县| 巍山| 荆门市| 新郑市| 行唐县| 旌德县| 黄山市| 静海县| 米泉市| 大理市| 油尖旺区| 昭平县| 屏山县| 盐城市| 新巴尔虎右旗| 高阳县| 屏山县| 枣强县| 宜兴市| 新乡市| 千阳县| 贞丰县| 榕江县| 新闻| 常德市| 大邑县| 囊谦县| 秦皇岛市| 河源市| 汉阴县| 同德县| 泾川县| 田林县| 自贡市| 海宁市| 靖江市| 荃湾区| 遵义县| 大连市|