栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

实战 FastDFS Java 客户端实现分布式文件上传

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

实战 FastDFS Java 客户端实现分布式文件上传

安装 FastDFS Java 客户端

先从 GitHub 上将项目源码克隆下来:

$ git clone https://github.com/happyfish100/fastdfs-client-java.git

然后不要忘了部署到 Nexus 依赖私服:

$ mvn clean install deploy

最后在需要用到的项目中添加 POM 依赖即可:


org.csourcefastdfs-client-java1.27-SNAPSHOT

引入依赖以后在 application.yml 中添加 FastDFS 服务端配置:

fastdfs:
  base:
    # 一般为 Nginx 代理地址
    url: http://{fastdfs_server ip}/
storage:
  type: fastdfs
  fastdfs:
    tracker_server: {tracker_server ip}
创建 FastDFS 工具类 定义文件存储接口
package com.antoniopeng.fastdfs.service.upload;


public interface StorageService {
    
    
    public String upload(byte[] data, String extName);

    
    public int delete(String fileId);
}
实现文件存储接口
package com.antoniopeng.fastdfs.service.upload;

import org.csource.common.NamevaluePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerGroup;
import org.csource.fastdfs.TrackerServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;


public class FastDFSStorageService implements StorageService, InitializingBean {
    
    private static final Logger logger = LoggerFactory.getLogger(FastDFSStorageService.class);

    private TrackerClient trackerClient;

    @Value("${storage.fastdfs.tracker_server}")
    private String trackerServer;

    @Override
    public String upload(byte[] data, String extName) {
 
 TrackerServer trackerServer = null;
 StorageServer storageServer = null;
 StorageClient1 storageClient1 = null;
 try {
     NamevaluePair[] meta_list = null; // new NamevaluePair[0];

     trackerServer = trackerClient.getConnection();
     if (trackerServer == null) {
  logger.error("getConnection return null");
     }
     storageServer = trackerClient.getStoreStorage(trackerServer);
     storageClient1 = new StorageClient1(trackerServer, storageServer);
     String fileid = storageClient1.upload_file1(data, extName, meta_list);
     logger.debug("uploaded file <{}>", fileid);
     return fileid;
 } catch (Exception ex) {
     logger.error("Upload fail", ex);
     return null;
 } finally {
     if (storageServer != null) {
  try {
      storageServer.close();
  } catch (IOException e) {
      e.printStackTrace();
  }
     }
     if (trackerServer != null) {
  try {
      trackerServer.close();
  } catch (IOException e) {
      e.printStackTrace();
  }
     }
     storageClient1 = null;
 }
    }

    @Override
    public int delete(String fileId) {
 
 TrackerServer trackerServer = null;
 StorageServer storageServer = null;
 StorageClient1 storageClient1 = null;
 int index = fileId.indexOf('/');
 String groupName = fileId.substring(0, index);
 try {
     trackerServer = trackerClient.getConnection();
     if (trackerServer == null) {
  logger.error("getConnection return null");
     }
     storageServer = trackerClient.getStoreStorage(trackerServer, groupName);
     storageClient1 = new StorageClient1(trackerServer, storageServer);
     int result = storageClient1.delete_file1(fileId);
     return result;
 } catch (Exception ex) {
     logger.error("Delete fail", ex);
     return 1;
 } finally {
     if (storageServer != null) {
  try {
      storageServer.close();
  } catch (IOException e) {
      e.printStackTrace();
  }
     }
     if (trackerServer != null) {
  try {
      trackerServer.close();
  } catch (IOException e) {
      e.printStackTrace();
  }
     }
     storageClient1 = null;
 }
    }

    @Override
    public void afterPropertiesSet() throws Exception {
 
 File confFile = File.createTempFile("fastdfs", ".conf");
 PrintWriter confWriter = new PrintWriter(new FileWriter(confFile));
 confWriter.println("tracker_server=" + trackerServer);
 confWriter.close();
 ClientGlobal.init(confFile.getAbsolutePath());
 confFile.delete();
 TrackerGroup trackerGroup = ClientGlobal.g_tracker_group;
 trackerClient = new TrackerClient(trackerGroup);

 logger.info("Init FastDFS with tracker_server : {}", trackerServer);
    }
}
创建文件存储工厂类
package com.antoniopeng.fastdfs.service.upload;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;

import java.util.HashMap;
import java.util.Map;


public class StorageFactory implements FactoryBean {

    @Autowired
    private AutowireCapableBeanFactory acbf;

    
    @Value("${storage.type}")
    private String type;

    private Map> classMap;

    public StorageFactory() {
 classMap = new HashMap<>();
 classMap.put("fastdfs", FastDFSStorageService.class);
    }

    @Override
    public StorageService getObject() throws Exception {
 Class<? extends StorageService> clazz = classMap.get(type);
 if (clazz == null) {
     throw new RuntimeException("Unsupported storage type [" + type + "], valid are " + classMap.keySet());
 }

 StorageService bean = clazz.newInstance();
 acbf.autowireBean(bean);
 acbf.initializeBean(bean, bean.getClass().getSimpleName());
 return bean;
    }

    @Override
    public Class<?> getObjectType() {
 return StorageService.class;
    }

    @Override
    public boolean isSingleton() {
 return true;
    }
}
配置文件存储工厂类
package com.antoniopeng.fastdfs.service.upload;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class FastDFSConfiguration {
    @Bean
    public StorageFactory storageFactory() {
 return new StorageFactory();
    }
}
创建 FastDFS 访问控制器
package com.antoniopeng.fastdfs.controller.upload;

import com.antoniopeng.fastdfs.service.upload.StorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
public class UploadController {
    
    @Value("${fastdfs.base.url}")
    private String FASTDFS_base_URL;

    @Autowired
    private StorageService storageService;

    
    @RequestMapping(value = "upload", method = RequestMethod.POST)
    public Map upload(MultipartFile dropFile, MultipartFile[] editorFiles) {
 Map result = new HashMap<>();

 // Dropzone 上传
 if (dropFile != null) {
     result.put("fileName", writeFile(dropFile));
 }

 // wangEditor 上传
 if (editorFiles != null && editorFiles.length > 0) {
     List fileNames = new ArrayList<>();

     for (MultipartFile editorFile : editorFiles) {
  fileNames.add(writeFile(editorFile));
     }

     result.put("errno", 0);
     result.put("data", fileNames);
 }

 return result;
    }

    
    private String writeFile(MultipartFile multipartFile) {
 // 获取文件后缀
 String oname = multipartFile.getOriginalFilename();
 String extName = oName.substring(oName.lastIndexOf(".") + 1);

 // 文件存放路径
 String url = null;
 try {
     String uploadUrl = storageService.upload(multipartFile.getBytes(), extName);
     url = FASTDFS_base_URL + uploadUrl;
 } catch (IOException e) {
     e.printStackTrace();
 }

 // 返回文件完整路径
 return url;
    }
}
  • 文章作者:彭超
  • 本文首发于个人博客:antoniopeng.com
  • 版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 [彭超的博客]!
转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号