1. 创建SpringBoot 项目
2. 导入FastDFS Java客户端依赖
 
 <!-- fastdfs -->
<dependency>
    <groupId>net.oschina.zcx7878</groupId>
    <artifactId>fastdfs-client-java</artifactId>
    <version>1.27.0.0</version>
</dependency>
 
 
3. 配置 FastDFS
3.1 在 application.properties 文件中配置如下:
#fastdfs 配置 fastdfs.connect_timeout_in_seconds=5 fastdfs.network_timeout_in_seconds=30 fastdfs.charset=UTF-8 fastdfs.http_anti_steal_token=false fastdfs.http_secret_key=FastDFS1234567890 fastdfs.http_tracker_http_port=80 fastdfs.tracker_servers=192.168.31.225:22122 fastdfs.connection_pool.enabled=true fastdfs.connection_pool.max_count_per_entry=500 fastdfs.connection_pool.max_idle_time=3600 fastdfs.connection_pool.max_wait_time_in_ms=1000 fastdfs.nginx.host=192.168.31.225
3.2 创建FastDFS配置类
新建java文件 FastDFSConfig.java
  
   - 
    
     
    
    
     
      @
      Configuration
     
    
- 
    
     
    
    
     
      public 
      class FastDFSConfig {
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
       @Bean
     
    
- 
    
     
    
    
     
       public StorageClient storageClient() {
     
    
- 
    
     
    
    
     
         StorageClient storageClient 
      = 
      null;
     
    
- 
    
     
    
    
     
         try {
     
    
- 
    
     
    
    
     
             ClientGlobal.initByProperties(
      "application.properties");
     
    
- 
    
     
    
    
     
             TrackerClient trackerClient 
      = new TrackerClient();
     
    
- 
    
     
    
    
     
             TrackerServer trackerServer 
      = trackerClient.getConnection();
     
    
- 
    
     
    
    
     
             storageClient 
      = new StorageClient(trackerServer, 
      null);
     
    
- 
    
     
    
    
     
         } catch (IOException | MyException e) {
     
    
- 
    
     
    
    
     
             e.printStackTrace();
     
    
- 
    
     
    
    
     
         }
     
    
- 
    
     
    
    
     
         
      return storageClient;
     
    
- 
    
     
    
    
     
       }
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      }
     
    
 4. 业务代码中使用
Service 层实现类
  
   - 
    
     
    
    
     
      @Service
     
    
- 
    
     
    
    
     
      public 
      class UploadServiceImpl implements UploadService {
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
       private static 
      final Logger logger 
      = LoggerFactory.getLogger(UploadServiceImpl.
      class);
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
       @Resource
     
    
- 
    
     
    
    
     
       private StorageClient storageClient;
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
       @
      Value(
      "${fastdfs.nginx.host}")
     
    
- 
    
     
    
    
     
       private 
      String serverAddr;
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
       @
      Override
     
    
- 
    
     
    
    
     
       public 
      String upload(MultipartFile 
      file) {
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
         
      String oldName 
      = 
      file.getOriginalFilename();
     
    
- 
    
     
    
    
     
         
      if (oldName !
      = 
      null 
      &
      & !
      "".equals(oldName)) {
     
    
- 
    
     
    
    
     
             
      String ext 
      = oldName.substring(oldName.lastIndexOf(
      ".") 
      + 
      1);
     
    
- 
    
     
    
    
     
             try {
     
    
- 
    
     
    
    
     
                 
      String[] parts 
      = storageClient.upload_
      file(
      file.getBytes(), ext, 
      null);
     
    
- 
    
     
    
    
     
                 
      String path 
      = parts !
      = 
      null ? parts[
      0] 
      + 
      "/" 
      + parts[
      1] : 
      null;
     
    
- 
    
     
    
    
     
                 
      return 
      "http://" 
      + serverAddr 
      + 
      "/" 
      + path;
     
    
- 
    
     
    
    
     
             } catch (IOException | MyException e) {
     
    
- 
    
     
    
    
     
                 logger.
      error(
      "文件上传失败");
     
    
- 
    
     
    
    
     
                 e.printStackTrace();
     
    
- 
    
     
    
    
     
             }
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
         }
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
         
      return 
      null;
     
    
- 
    
     
    
    
     
       }
     
    
- 
    
     
    
    
     
      }
     
    
 Controller 层调用
  
   - 
    
     
    
    
     
      @RestController
     
    
- 
    
     
    
    
     
      @RequestMapping(
      "/fastdfs")
     
    
- 
    
     
    
    
     
      public class UploadController {
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
         
      @Resource
     
    
- 
    
     
    
    
     
          private UploadService uploadService;
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
         
      @PostMapping(
      "/upload")
     
    
- 
    
     
    
    
     
          public String 
      uploadFile(MultipartFile file) {
     
    
- 
    
     
    
    
             
      System
      .out
      .println(
      ">>>>>>>>> 开始上传文件 >>>>>>>>>>>");
     
    
- 
    
     
    
    
             
      return 
      uploadService
      .upload(file);
     
    
- 
    
     
    
    
     
          }
     
    
- 
    
     
    
    
     
      }
     
    
5. postman 工具测试

返回结果就是上传到服务器的文件的访问地址,浏览器地址栏可以打开。

6. 封装工具类
  
   - 
    
     
    
    
     
      /
      **
     
    
- 
    
     
    
    
     
       * FastDFS 文件服务工具类
     
    
- 
    
     
    
    
     
       * @author: cs
     
    
- 
    
     
    
    
     
       * @date: 2023-02-12 19:39:25
     
    
- 
    
     
    
    
     
       * @since: 1.0
     
    
- 
    
     
    
    
     
       */
     
    
- 
    
     
    
    
     
      @Component
     
    
- 
    
     
    
    
     
      public 
      class FastDFSUtil {
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
       private static StorageClient
      1 storageClient
      1;
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
       public static 
      final 
      String FASTDFS_NGINX_HOST_SPREFIX 
      = 
      "http://";
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
       
      /
      / 服务器地址(服务器的Nginx已经配置好了,不需要加端口)
     
    
- 
    
     
    
    
     
       @
      Value(
      "${fastdfs.nginx.host}")
     
    
- 
    
     
    
    
     
       private 
      String serverAddr;
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
       
      /
      **
     
    
- 
    
     
    
    
     
       * 初始化FastDFS 配置
     
    
- 
    
     
    
    
     
       */
     
    
- 
    
     
    
    
     
       @PostConstruct
     
    
- 
    
     
    
    
     
       public void init() {
     
    
- 
    
     
    
    
     
         try {
     
    
- 
    
     
    
    
     
             ClientGlobal.initByProperties(
      "application.properties");
     
    
- 
    
     
    
    
     
             
      /
      / 获取trackerClient服务
     
    
- 
    
     
    
    
     
             TrackerClient trackerClient 
      = new TrackerClient();
     
    
- 
    
     
    
    
     
             TrackerServer trackerServer 
      = trackerClient.getConnection();
     
    
- 
    
     
    
    
     
             storageClient
      1 
      = new StorageClient
      1(trackerServer, 
      null);
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
         } catch (IOException | MyException e) {
     
    
- 
    
     
    
    
     
             e.printStackTrace();
     
    
- 
    
     
    
    
     
         }
     
    
- 
    
     
    
    
     
       }
     
    
- 
    
     
    
    
     
       
     
    
- 
    
     
    
    
     
       
     
    
- 
    
     
    
    
     
       
      /
      **
     
    
- 
    
     
    
    
     
       * 上传文件
     
    
- 
    
     
    
    
     
       * @param file 文件对象
     
    
- 
    
     
    
    
     
       * @return 文件id
     
    
- 
    
     
    
    
     
       */
     
    
- 
    
     
    
    
     
       public 
      String upload(MultipartFile 
      file) {
     
    
- 
    
     
    
    
     
         
      String oldName 
      = 
      file.getOriginalFilename();
     
    
- 
    
     
    
    
     
         
      if (oldName !
      = 
      null 
      &
      & !
      "".equals(oldName)) {
     
    
- 
    
     
    
    
     
             
      String ext 
      = oldName.substring(oldName.lastIndexOf(
      ".") 
      + 
      1);
     
    
- 
    
     
    
    
     
             try {
     
    
- 
    
     
    
    
     
                 
      return FASTDFS_NGINX_HOST_SPREFIX 
      + serverAddr 
      + 
      "/" 
      + storageClient
      1.upload_
      file
      1(
      file.getBytes(), ext, 
      null);
     
    
- 
    
     
    
    
     
             } catch (IOException | MyException e) {
     
    
- 
    
     
    
    
     
                 e.printStackTrace();
     
    
- 
    
     
    
    
     
             }
     
    
- 
    
     
    
    
     
         }
     
    
- 
    
     
    
    
     
         
      return 
      null;
     
    
- 
    
     
    
    
     
       }
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
       
     
    
- 
    
     
    
    
     
      }
     
    
 使用时就不用再写 FastDFSConfig.java 配置类了,直接在类中使用 @Resource 注入即可。
@Resource
private FastDFSUtil fastDFSUtil;
7. 服务器中Nginx的配置

在server 节点下配置以上内容。监听的端口是80,注意开防火墙。
/home/fastdfs/storage/data 是fastdfs 服务存储文件的位置

在安装Nginx 时不用额外安装类似 fastdfs-nginx-module 这个模块。直接在nginx.conf 中将fastdfs文件的存储路径配置一下即可。
转载:https://blog.csdn.net/qq_42026600/article/details/128998605
查看评论
					 
					