色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術(shù)文章
文章詳情頁

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

瀏覽:118日期:2023-03-11 18:47:46
前言

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

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

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

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

1、引入依賴

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

<!-- 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è)集成好的依賴:

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è)配置類就可以了:

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

參考截圖:

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

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

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

server: port: 8080ip: 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封裝工具類

創(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;@Componentpublic 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); } /** * 將一段字符串生成一個(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 文件訪問地址 * @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í)行效果截圖:

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

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

測試下載路徑:

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

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

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

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

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

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 日韩制服诱惑 | 久久久久久久久久久96av | 精品视频 久久久 | 中文字幕成人免费视频 | 国产真真人女人特级毛片 | 日韩天天干 | 成人黄色一级视频 | 国产原创视频在线 | 香蕉网站狼人久久五月亭亭 | 激情宗合网 | 欧美日韩一区二区三区免费 | 99精品久久久久久久免费看蜜月 | 国产精品免费精品自在线观看 | 高清波多野结衣一区二区三区 | 国产成人一区二区三区在线播放 | 亚洲视频欧美视频 | 成年女人在线观看片免费视频 | 国产成年 | 久章草在线观看 | 亚洲精品久久久久久久久久久网站 | 精品国产日韩亚洲一区二区 | 日本乱人伦片中文三区 | 国产成人精品三级 | 欧美片欧美日韩国产综合片 | 日本一区二区三区不卡在线视频 | 欧美视频精品在线观看 | 欧美又粗又硬又大久久久 | 欧美一级毛片在线看视频 | 欧洲欧美成人免费大片 | 亚洲男人的天堂在线 | 国产亚洲一区二区三区不卡 | 一级毛片一级毛片一级毛片 | 欧美一区二区在线观看免费网站 | 99超级碰碰成人香蕉网 | 中文字幕综合在线 | 午夜一级毛片不卡 | 国产v片成人影院在线观看 国产v片在线播放免费观 | 天天澡天天碰天天狠伊人五月 | 亚洲国产经典 | 亚洲aⅴ在线 | 国产一级a毛片 |