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";}
}

相关内容

热门资讯

七夕情人节的贺卡祝福语 七夕情人节的贺卡祝福语  在学习、工作乃至生活中,大家都写过祝福语吧,祝福语往往灵活机动,可以随机应...
十六岁的生日祝福语 十六岁的生日祝福语大全  在日常学习、工作抑或是生活中,大家都有写祝福语的经历,对祝福语很是熟悉吧,...
光棍节祝福语短信 光棍节祝福语短信集合35条  光棍节了,送你一条带有爱情魔力的短信!月老说收短信的光棍,将会邂逅一段...
简短七夕情人节祝福语 简短七夕情人节祝福语  无论是身处学校还是步入社会,大家都尝试过写祝福语吧,祝福语可以起到可以增进情...
创意教师节祝福语 2021创意教师节祝福语  在日常学习、工作或生活中,大家一定都接触过祝福语吧,祝福语可以起到可以增...
自己祝福自己母亲节祝福语 自己祝福自己母亲节祝福语(精选215句)  无论是在学校还是在社会中,大家或多或少都会用到过祝福语吧...
经典母亲节贺卡的祝福语 经典母亲节贺卡的祝福语  在日复一日的学习、工作或生活中,要用到祝福语的情况还是蛮多的,祝福语可以起...
送给朋友的祝福语 送给朋友的祝福语15篇  在现实生活或工作学习中,许多人都有过写祝福语的经历,对祝福语都不陌生吧,借...
暖心生日祝福语给老师 暖心生日祝福语给老师  无论在学习、工作或是生活中,许多人都有过写祝福语的经历,对祝福语都不陌生吧,...
聚会酒桌的祝福话 聚会酒桌的祝福话汇总  以真诚为半径,以尊敬为圆心,送你一个圆圆的祝福,给你端起这杯酒。以下是小编为...
升学宴祝福语 升学宴祝福语精选15篇  无论在学习、工作或是生活中,大家都写过祝福语,肯定对各类祝福语都很熟悉吧,...
春节喜庆的祝福语 春节喜庆的祝福语集合28句  每年的这个时候,祝福就像海洋涌向你,希望我的祝福像一叶轻舟,载你乘风破...
二十周年简短祝福语 二十周年简短祝福语  在日常学习、工作或生活中,大家都经常接触到祝福语吧,祝福语可以给人们带去温暖和...
表达元旦快乐的祝福语微信 精选表达元旦快乐的祝福语微信汇编30条  笑眼看星光璀璨,歌舞欢辞旧迎新,鞭炮响喜地欢天,巨龙飞五洲...
酒桌上的祝福话 酒桌上的祝福话(精选110句)  在日常的学习、工作、生活中,要用到祝福的话的情况还是蛮多的,祝福的...
升学宴的祝福语 升学宴的祝福语(精选145句)  在平日的学习、工作和生活里,要用到祝福语的情况还是蛮多的,祝福语可...
冬天祝福语 冬天祝福语(精选55句)  在平平淡淡的日常中,要用到祝福语的情况还是蛮多的,祝福语能给亲朋好带去好...
秋天祝福语 秋天祝福语(精选265句)  在我们平凡的日常里,说到祝福语,大家肯定都不陌生吧,祝福语是人们表达心...