上一篇 下一篇 分享链接 返回 返回顶部

香港vps服务器的FastDFS与Springboot集成

发布人:欢子 发布时间:2026-01-21 09:44 阅读量:9
香港vps服务器的FastDFS与Springboot集成:

整合到Springboot项目流程

1、添加pom依赖

com.github.tobato

fastdfs-client

1.25.2-RELEASE

2、将Fdfs配置引入项目

@Import(FdfsClientConfig.class)

@SpringBootApplication

publicclassJingtongApplication{

publicstaticvoidmain(String[]args){

SpringApplication.run(JingtongApplication.class,args);

}

}

3、在spring配置文件中加入fdfs相关配置

根据项目当中使用配置文件类型(.yml和.properties选择其中一个),加入相应的配置。

application.yml

fdfs:

soTimeout:1500

connectTimeout:600

thumbImage:#缩略图生成参数

width:150

height:150

trackerList:#TrackerList参数,支持多个

-192.168.0.201:22122

-192.168.0.202:22122

4、在项目中使用

客户端主要包括以下接口:

TrackerClient-TrackerServer接口

GenerateStorageClient-一般文件存储接口(StorageServer接口)

FastFileStorageClient-为方便项目开发集成的简单接口(StorageServer接口)

AppendFileStorageClient-支持文件续传操作的接口(StorageServer接口)

packagecom.digi_zones.support.fs;

importcom.digi_zones.config.AppConfig;

importcom.digi_zones.contacts.AppConstants;

importcom.github.tobato.fastdfs.domain.FileInfo;

importcom.github.tobato.fastdfs.domain.StorePath;

importcom.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;

importcom.github.tobato.fastdfs.service.FastFileStorageClient;

importorg.apache.commons.io.FilenameUtils;

importorg.apache.commons.lang3.StringUtils;

importorg.slf4j.Logger;

importorg.slf4j.LoggerFactory;

importorg.springframework.beans.factory.annotation.Autowired;

importorg.springframework.stereotype.Component;

importorg.springframework.web.multipart.MultipartFile;

importjava.io.ByteArrayInputStream;

importjava.io.IOException;

importjava.nio.charset.Charset;

/**

*

Description:FastDFS文件上传下载包装类

*

Copyright:Copyright(c)2016

*

*@author杨信

*@version1.0

*@date2016/9/7

*/

@Component

publicclassFastDFSClientWrapper{

privatefinalLoggerlogger=LoggerFactory.getLogger(FastDFSClientWrapper.class);

@Autowired

privateFastFileStorageClientstorageClient;

@Autowired

privateAppConfigappConfig;//项目参数配置

/**

*上传文件

*@paramfile文件对象

*@return文件访问地址

*@throwsIOException

*/

publicStringuploadFile(MultipartFilefile)throwsIOException{

StorePathstorePath=storageClient.uploadFile(file.getInputStream(),file.getSize(),FilenameUtils.getExtension(file.getOriginalFilename()),null);

returngetResAccessUrl(storePath);

}

/**

*将一段字符串生成一个文件上传

*@paramcontent文件内容

*@paramfileExtension

*@return

*/

publicStringuploadFile(Stringcontent,StringfileExtension){

byte[]buff=content.getBytes(Charset.forName("UTF-8"));

ByteArrayInputStreamstream=newByteArrayInputStream(buff);

StorePathstorePath=storageClient.uploadFile(stream,buff.length,fileExtension,null);

returngetResAccessUrl(storePath);

}

//封装图片完整URL地址

privateStringgetResAccessUrl(StorePathstorePath){

StringfileUrl=AppConstants.HTTP_PRODOCOL+appConfig.getResHost()

+":"+appConfig.getFdfsStoragePort()+"/"+storePath.getFullPath();

returnfileUrl;

}

/**

*删除文件

*@paramfileUrl文件访问地址

*@return

*/

publicvoiddeleteFile(StringfileUrl){

if(StringUtils.isEmpty(fileUrl)){

return;

}

try{

StorePathstorePath=StorePath.praseFromUrl(fileUrl);

storageClient.deleteFile(storePath.getGroup(),storePath.getPath());

}catch(FdfsUnsupportStorePathExceptione){

logger.warn(e.getMessage());

}

}

}

除了FastDFSClientWrapper类中用到的api,客户端提供的api还有很多,可根据自身的业务需求,将其它接口也添加到工具类中即可。如下所示:

//上传文件,并添加文件元数据

StorePathuploadFile(InputStreaminputStream,longfileSize,StringfileExtName,SetmetaDataSet);

//获取文件元数据

SetgetMetadata(StringgroupName,Stringpath);

//上传图片并同时生成一个缩略图

StorePathuploadImageAndCrtThumbImage(InputStreaminputStream,longfileSize,StringfileExtName,

SetmetaDataSet);

//。。。

在项目中使用FastDFSClientWrapper:

@Controller

publicclassMyController{

@Autowired

privateFastDFSClientWrapperdfsClient;

//上传图片

@RequestMapping(value="/upload",method=RequestMethod.POST)

publicStringupload(MultipartFilefile,HttpServletRequestrequest,HttpServletResponseresponse)throwsException{

//省略业务逻辑代码。。。

StringimgUrl=dfsClient.uploadFile(file);

//。。。。

returnxxxx;

}

}

FastDFS与Springboot集成

一诺网络香港免备案专区,提供「香港增强VPS」和「香港特惠VPS」两种类型的高可用弹性计算服务,搭载新一代英特尔®至强®铂金处理器,接入CN2低延时高速回国带宽线路,网络访问顺滑、流畅。机房网络架构采用了BGP协议的解决方案可提供多线路互联融合网络,使得不同网络运营商线路的用户都能通过最佳路由实现快速访问。香港云VPS低至29元/月,购买链接:https://www.enuoidc.com/vps.html?typeid=2

目录结构
全文