FastDFS使用教程
创始人
2024-02-11 15:50:50
0

FastDFS使用教程

    • FastDFS
      • 下载必要安装包
      • libfastcommon
      • fastdfs
        • tracker配置
        • storage配置
        • client配置(开发不需要)
      • fastdfs-nginx-module 和 Nginx
      • 使用
        • pom
        • application
        • service
        • controller

FastDFS

下载必要安装包

wget -c "https://github.com/happyfish100/fastdfs/archive/V6.06.tar.gz"
wget -c "https://github.com/happyfish100/libfastcommon/archive/V1.0.43.tar.gz"
wget -c "https://github.com/happyfish100/fastdfs-nginx-module/archive/V1.22.tar.gz"
Nginx

libfastcommon

解压后放到/usr/local ./make.sh && ./make.sh install

fastdfs

解压后放到/usr/local ./make.sh && ./make.sh install

cd /etc/fdfs/
cp storage.conf.sample storage.conf
cp client.conf.sample client.conf
cp tracker.conf.sample tracker.conf
mkdir -p /fastdfs/tracker
mkdir -p /fastdfs/storage

tracker配置

vi /etc/fdfs/tracker.conf

base_path = /fastdfs/tracker

启动tracker:/etc/init.d/fdfs_trackerd start
停止tracker:/etc/init.d/fdfs_trackerd stop

storage配置

vi /etc/fdfs/storage.conf

base_path = /fastdfs/storage 
store_path0 = /fastdfs/storage
tracker_server = tracker的IP:22122
http.server_port = 80

启动storage:/etc/init.d/fdfs_storaged start

client配置(开发不需要)

vi /etc/fdfs/client.conf

base_path = /fastdfs/tracker
tracker_server = tracker的IP:22122

#设置开机启动:vi /etc/rc.d/rc.local

/etc/init.d/fdfs_trackerd start
/etc/init.d/fdfs_storaged start

fastdfs-nginx-module 和 Nginx

fastdfs-nginx-module解压后放到/usr/local

nginx添加模块(解压目录,不是nginx主目录)
./configure --prefix=/usr/local/nginx --with-http_stub_status_module

fastdfs-nginx-module配置文件修改并将config文件中的/usr/local替换成/usr:cd /usr/local/fastdfs-nginx-module-1.22/src/ vi config

nginx添加模块:./configure --add-module=/usr/local/fastdfs-nginx-module-1.22/src/

nginx编译安装:make && make install

fastdfs-nginx-module配置文件复制
cp /usr/local/fastdfs-nginx-module-1.22/src/mod_fastdfs.conf /etc/fdfs/
并修改配置文件
vi /etc/fdfs/mod_fastdfs.conf

connect_timeout=10
tracker_server=IP:22122
url_have_group_name=true
store_path0=/fastdfs/storage

进入fastdfd的conf目录, 将http.conf,mime.types两个文件拷贝到/etc/fdfs/目录下
cp http.conf mime.types /etc/fdfs/

创建一个软连接,在/fastdfs/storage文件存储目录下创建软连接,将其链接到实际存放数据的目录
ln -s /fastdfs/storage/data/ /fastdfs/storage/data/M00

修改nginx.conf
vi /usr/local/nginx/conf/nginx.conf

server {listen       80;server_name  IP;location ~/group([0-9])/M00 {root  /fastdfs/storage/data;ngx_fastdfs_module;}
}

启动nginx
根据路径访问文件

使用

pom

com.github.tobatofastdfs-client1.26.7

application

fdfs.so-timeout = 1501
fdfs.connect-timeout = 601
fdfs.thumb-image.width= 150
fdfs.thumb-image.height= 150
fdfs.tracker-list=IP:22122
fdfs.web-server-url=IP:23001/

service

@Component
public class FastDFSClient {@Autowiredprivate FastFileStorageClient storageClient;@Autowiredprivate 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 fdfsWebServer.getWebServerUrl() + storePath.getFullPath();//完整url}/*** 下载文件* @param fileUrl 文件url* @return*/public byte[]  download(String fileUrl) {String group = fileUrl.substring(0, fileUrl.indexOf("/"));String path = fileUrl.substring(fileUrl.indexOf("/") + 1);byte[] bytes = storageClient.downloadFile(group, path, new DownloadByteArray());return bytes;}/*** 删除文件* @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) {logger.warn(e.getMessage());}}
}

controller

@RestController
public class FastDFSController {@Autowiredprivate FastDFSClient fastDFSClient;@PostMapping("/upload")public String uploadFile(@RequestParam("file") MultipartFile file) throws IOException {byte[] bytes = file.getBytes();String originalFileName = file.getOriginalFilename();String extension = FilenameUtils.getExtension(file.getOriginalFilename());String fileName = file.getName();long fileSize = file.getSize();System.out.println(originalFileName + "==========" + fileName + "===========" + fileSize + "===============" + extension + "===========" + bytes.length);return fastDFSClient.uploadFile(file);}@PostMapping("/download")public void downloadFile(@RequestParam("fileUrl")String fileUrl, HttpServletResponse response) throws IOException {byte[] bytes = fastDFSClient.download(fileUrl);// 这里只是为了整合fastdfs,所以写死了文件格式。需要在上传的时候保存文件名。下载的时候使用对应的格式response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("1.jpg", "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 (Exception e) {e.printStackTrace();}}}@GetMapping("/deleteFile")public String deleteFile(@RequestParam("fileUrl")String fileUrl) {fastDFSClient.deleteFile(fileUrl);return "success";}
}

相关内容

热门资讯

岳父经典散文 岳父经典散文  在现实生活或工作学习中,大家经常看到散文吧?散文是一种常见的文学体裁,取材广泛,艺术...
大美三峡散文 大美三峡散文  一、传统诗文中的三峡形象  江水奔流、壁立千仞、激流险滩、千帆竞渡、巫山云雨、猿声啼...
散文今时明月旧时约 散文今时明月旧时约  错爱过多少冷酷的月亮,我才能看清自己。这一行行饱含深情的文字,只愿成为一生难忘...
春天的散文 春天的散文300字9篇  春天到来了,湖边的柳树露出一束束新叶,在湖边随风飘扬,婀娜多姿。小编整理的...
青草青青散文 青草青青散文(精选15篇)  在平平淡淡的日常中,许多人都写过散文吧?散文分为叙事散文、抒情散文、哲...
愿岁月温柔以待优秀散文 愿岁月温柔以待优秀散文(通用20篇)  无论是身处学校还是步入社会,大家都不可避免的会接触到散文吧?...
守住一颗宁静的心经典散文 守住一颗宁静的心经典散文  生活是一望无际的大海,人便是大海上的一叶扁舟,大海没有风平浪静的时候,所...
雪的短文散文 雪的短文散文  冬日的早晨,随着闹铃的响起,钻出暖暖的被窝,拉开窗帘。心中不觉掠过一阵惊喜:屋顶上、...
迪卡尔 中秋夜散文 迪卡尔 中秋夜散文  1  往年的中秋与今年的中秋并没有太大的差别,只是,对我来说,迪卡尔的出现,让...
散文近来可好 散文近来可好  曾经默默无言的静潜在同学群里,记得毕业那天,你在我的毕业留言本上绘着几株倩倩的青竹,...
酸石榴,甜石榴散文 酸石榴,甜石榴散文  老家院里种了两株石榴,约二十余年树龄,都有黝黑的粗干虬枝,一北一南,分据院子两...
爱情散文:有一种距离与爱无关 精选爱情散文:有一种距离与爱无关  两个人从陌生到相识,到有好感,到热恋,之间的距离愈来愈小;再到后...
妈妈别打我散文 妈妈别打我散文  初中毕业的时候,那时是07年,我做着08年去北京看奥运的打算。  不料,我去读了高...
自夸等于自贬散文 自夸等于自贬散文  一个人不能妄自菲薄,更不能妄自尊大,妄自菲薄的人看不到自身的优点,可怜可叹;妄自...
梁实秋《男人》散文 梁实秋《男人》散文  在生活、工作和学习中,大家都不可避免的会接触到散文吧?散文不讲究音韵,不讲究排...
菜花黄优美散文 菜花黄优美散文  在江南,过了春分,春色就越发的鲜艳了。最鲜艳的莫过于,田野里肆意铺陈的菜花。像是油...
鲁迅散文《二十四孝图》原文 《二十四孝图》读后感推荐度:二十四孝图读后感推荐度:寒窑赋原文解读推荐度:清明散文推荐度:我眼中的鲁...
朱自清春散文 朱自清春散文  朱自清,原名自华,号秋实,后改名自清,字佩弦。中国现代散文家、诗人、学者、民主战士。...
今夜请将我遗忘散文诗 今夜请将我遗忘散文诗  今夜请将我遗忘。一座青冢,衰草斜阳。我坐在坟头,低声吟唱,扑朔的蝙蝠为我送葬...
温柔如水的抒情散文 温柔如水的抒情散文  温柔如水,柔情似水,女人是水做的,天生就应该温柔善解人意;温柔体贴,默默关爱身...