FastDFS使用教程

发布时间:2025-12-09 16:24:20 浏览次数:10

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.confcp client.conf.sample client.confcp tracker.conf.sample tracker.confmkdir -p /fastdfs/trackermkdir -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/storagetracker_server = tracker的IP:22122http.server_port = 80

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

client配置(开发不需要)

vi /etc/fdfs/client.conf

base_path = /fastdfs/trackertracker_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=10tracker_server=IP:22122url_have_group_name=truestore_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

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

application

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

service

@Componentpublic 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

@RestControllerpublic 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";}}
需要做网站?需要网络推广?欢迎咨询客户经理 13272073477