zoukankan      html  css  js  c++  java
  • springboot2.0结合fastdfs实现文件分布式上传

    1. 引入依赖

    在父工程中,我们已经管理了依赖,版本为:

    <fastDFS.client.version>1.26.7</fastDFS.client.version>
    

    因此,这里我们直接在工程的pom.xml中引入坐标即可:

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

    @Configuration
    @Import(FdfsClientConfig.class)
    // 解决jmx重复注册bean的问题
    @EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
    public class FastClientImporter {
        
    }
    

    2. 在application.yml文件中编写FastDFS属性

    fdfs:
      so-timeout: 1501 # 超时时间
      connect-timeout: 601 # 连接超时时间
      thumb-image: # 缩略图
         60
        height: 60
      tracker-list: # tracker地址:你的虚拟机服务器地址+端口(默认是22122)
        - 192.168.0.22:22122
    

    3. 测试

    package com.leyou.upload.test;
    
    import com.github.tobato.fastdfs.domain.fdfs.StorePath;
    import com.github.tobato.fastdfs.domain.fdfs.ThumbImageConfig;
    import com.github.tobato.fastdfs.service.FastFileStorageClient;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    
    /**
     * @author john
     * @date 2019/12/6 - 15:09
     */
    @SpringBootTest
    @RunWith(SpringRunner.class)
    public class FastDFSTest {
    
        @Autowired
        private FastFileStorageClient storageClient;
    
        @Autowired
        private ThumbImageConfig thumbImageConfig;
    
        @Test
        public void testUpload() throws FileNotFoundException {
            // 要上传的文件
            File file = new File("D:\imooc\project\images\1.jpg");
            // 上传并保存图片,参数:1-上传的文件流 2-文件的大小 3-文件的后缀 4-可以不管他
            StorePath storePath = this.storageClient.uploadFile(
                    new FileInputStream(file), file.length(), "jpg", null);
            // 带分组的路径
            System.out.println(storePath.getFullPath());
            // 不带分组的路径
            System.out.println(storePath.getPath());
        }
    
        @Test
        public void testUploadAndCreateThumb() throws FileNotFoundException {
            File file = new File("D:\imooc\project\images\2.jpg");
            // 上传并且生成缩略图
            StorePath storePath = this.storageClient.uploadImageAndCrtThumbImage(
                    new FileInputStream(file), file.length(), "png", null);
            // 带分组的路径
            System.out.println(storePath.getFullPath());
            // 不带分组的路径
            System.out.println(storePath.getPath());
            // 获取缩略图路径
            String path = thumbImageConfig.getThumbImagePath(storePath.getPath());
            System.out.println(path);
        }
    }
    

  • 相关阅读:
    java cocurrent并发包
    阻塞队列只有一个线程在同一时刻对其进行或者读或者写
    在并发编程中使用生产者和消费者模式能够解决绝大多数并发问题。该模式通过平衡生产线程和消费线程的工作能力来提高程序的整体处理数据的速度。
    深入理解生产者消费者
    java并发编程阻塞队列
    高并发
    ClassLoader Java中类加载出现在哪个阶段,编译期和运行期? 类加载和类装载是一样的吗
    JAVA设计模式之工厂模式(简单工厂模式+工厂方法模式)
    Java并发编程-Executor框架(转)
    Java主线程等待所有子线程执行完毕再执行解决办法(转)
  • 原文地址:https://www.cnblogs.com/ifme/p/11997630.html
Copyright © 2011-2022 走看看