欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

前言

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

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

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

本文項(xiàng)目代碼:fastdfs_jb51.rar

1、引入依賴(lài)

簡(jiǎn)單說(shuō)一下這個(gè)依賴(lài)部分,目前大部分都是采用的如下依賴(lài):

<!-- 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>

本著不重復(fù)造輪子,且為了使用方便我們可以去GitHub找一個(gè)集成好的依賴(lài):

https://github.com/tobato/FastDFS_Client

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

2、將Fdfs配置引入項(xiàng)目

只需要?jiǎng)?chuàng)建一個(gè)配置類(lèi)就可以了:

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

參考截圖:

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

根據(jù)自己情況修改相應(yīng)ip地址及端口號(hào):

server:
  port: 8080

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

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

4、client封裝工具類(lèi)

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

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 文件對(duì)象
     * @return 文件訪(fǎng)問(wèn)地址
     * @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 文件對(duì)象
     * @return 文件訪(fǎng)問(wèn)地址
     * @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);
    }

    /**
     * 將一段字符串生成一個(gè)文件上傳
     * @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 文件訪(fǎng)問(wèn)地址
     * @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 只是測(cè)試,所以未使用,logger,正式環(huán)境請(qǐng)修改打印方式 **/
        }
    }
    /**
     * 下載文件
     *
     * @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測(cè)試類(lèi)

5.1 文件上傳測(cè)試

@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、下載文件測(cè)試

@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,所以寫(xiě)死了文件格式。需要在上傳的時(shí)候保存文件名。下載的時(shí)候使用對(duì)應(yīng)的格式 **/
        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();
            }
        }
    }

}

測(cè)試下載路徑:

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

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

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

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

相關(guān)文章

  • JAVA發(fā)送HTTP請(qǐng)求的四種方式總結(jié)

    JAVA發(fā)送HTTP請(qǐng)求的四種方式總結(jié)

    這篇文章主要給大家介紹了關(guān)于JAVA發(fā)送HTTP請(qǐng)求的多種方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • Java利用條件運(yùn)算符的嵌套來(lái)完成學(xué)習(xí)成績(jī)的劃分

    Java利用條件運(yùn)算符的嵌套來(lái)完成學(xué)習(xí)成績(jī)的劃分

    這篇文章主要介紹了Java利用條件運(yùn)算符的嵌套來(lái)完成學(xué)習(xí)成績(jī)的劃分,需要的朋友可以參考下
    2017-02-02
  • Springboot 如何獲取上下文對(duì)象

    Springboot 如何獲取上下文對(duì)象

    這篇文章主要介紹了Springboot 如何獲取上下文對(duì)象的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Java面試題沖刺第八天--Spring框架2

    Java面試題沖刺第八天--Spring框架2

    這篇文章主要為大家分享了最有價(jià)值的三道Spring框架面試題,涵蓋內(nèi)容全面,包括數(shù)據(jù)結(jié)構(gòu)和算法相關(guān)的題目、經(jīng)典面試編程題等,感興趣的小伙伴們可以參考一下
    2021-07-07
  • Spring Boot 定制與優(yōu)化內(nèi)置的Tomcat容器實(shí)例詳解

    Spring Boot 定制與優(yōu)化內(nèi)置的Tomcat容器實(shí)例詳解

    本文主要記錄對(duì)內(nèi)置容器優(yōu)化和定制的方式,用于自己加深對(duì)SpringBoot理解。本文給大家介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友參考下吧
    2017-12-12
  • Java實(shí)現(xiàn)AES加密算法的簡(jiǎn)單示例分享

    Java實(shí)現(xiàn)AES加密算法的簡(jiǎn)單示例分享

    這篇文章主要介紹了Java實(shí)現(xiàn)AES加密算法的簡(jiǎn)單示例分享,AES算法是基于對(duì)密碼值的置換和替代,需要的朋友可以參考下
    2016-04-04
  • spring boot 自動(dòng)更新靜態(tài)文件和后臺(tái)代碼的實(shí)例

    spring boot 自動(dòng)更新靜態(tài)文件和后臺(tái)代碼的實(shí)例

    下面小編就為大家分享一篇spring boot 自動(dòng)更新靜態(tài)文件和后臺(tái)代碼的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2017-12-12
  • JVM垃圾回收原理解析

    JVM垃圾回收原理解析

    這篇文章主要介紹了JVM垃圾回收原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • 一文深入分析java.lang.ClassNotFoundException異常

    一文深入分析java.lang.ClassNotFoundException異常

    這篇文章主要給大家介紹了關(guān)于java.lang.ClassNotFoundException異常的相關(guān)資料,java.lang.ClassNotFoundException是Java編程時(shí)經(jīng)常會(huì)遇到的一個(gè)異常,它表示JVM在嘗試加載某個(gè)類(lèi)時(shí)未能找到該類(lèi),需要的朋友可以參考下
    2023-10-10
  • SpringMVC使用@Valid注解進(jìn)行數(shù)據(jù)驗(yàn)證的方法

    SpringMVC使用@Valid注解進(jìn)行數(shù)據(jù)驗(yàn)證的方法

    本篇文章主要介紹了SpringMVC使用@Valid注解進(jìn)行數(shù)據(jù)驗(yàn)證的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-02-02

最新評(píng)論