zoukankan      html  css  js  c++  java
  • FastDFSJava客户端使用

    1.1.java客户端

    余庆先生提供了一个Java客户端,但是作为一个C程序员,写的java代码可想而知。而且已经很久不维护了。

    这里推荐一个开源的FastDFS客户端,支持最新的SpringBoot2.0。

    配置使用极为简单,支持连接池,支持自动生成缩略图,狂拽酷炫吊炸天啊,有木有。

    地址:tobato/FastDFS_client

    接下来,我们就用FastDFS改造~~~-upload工程。

     

    1.1.1.引入依赖

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

    <fastDFS.client.version>1.26.2</fastDFS.client.version>

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

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

     

    1.1.2.引入配置类

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

     

    1.1.3.编写FastDFS属性

    在application.yml配置文件中追加如下内容:

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

    1.1.4.配置hosts

    将来通过域名:image.~~~.com这个域名访问fastDFS服务器上的图片资源。所以,需要代理到虚拟机地址:

    配置hosts文件,使image.~~~.com可以访问fastDFS服务器

    1.1.5.测试

    创建测试类:

    把以下内容copy进去:

     1 @SpringBootTest
     2 @RunWith(SpringRunner.class)
     3 public class FastDFSTest {
     4  5     @Autowired
     6     private FastFileStorageClient storageClient;
     7  8     @Autowired
     9     private ThumbImageConfig thumbImageConfig;
    10 11     @Test
    12     public void testUpload() throws FileNotFoundException {
    13         // 要上传的文件
    14         File file = new File("C:\Users\joedy\Pictures\xbx1.jpg");
    15         // 上传并保存图片,参数:1-上传的文件流 2-文件的大小 3-文件的后缀 4-可以不管他
    16         StorePath storePath = this.storageClient.uploadFile(
    17                 new FileInputStream(file), file.length(), "jpg", null);
    18         // 带分组的路径
    19         System.out.println(storePath.getFullPath());
    20         // 不带分组的路径
    21         System.out.println(storePath.getPath());
    22     }
    23 24     @Test
    25     public void testUploadAndCreateThumb() throws FileNotFoundException {
    26         File file = new File("C:\Users\joedy\Pictures\xbx1.jpg");
    27         // 上传并且生成缩略图
    28         StorePath storePath = this.storageClient.uploadImageAndCrtThumbImage(
    29                 new FileInputStream(file), file.length(), "png", null);
    30         // 带分组的路径
    31         System.out.println(storePath.getFullPath());
    32         // 不带分组的路径
    33         System.out.println(storePath.getPath());
    34         // 获取缩略图路径
    35         String path = thumbImageConfig.getThumbImagePath(storePath.getPath());
    36         System.out.println(path);
    37     }
    38 }

    结果:

    1 group1/M00/00/00/wKg4ZVsWl5eAdLNZAABAhya2V0c424.jpg
    2 M00/00/00/wKg4ZVsWl5eAdLNZAABAhya2V0c424.jpg
    3 group1/M00/00/00/wKg4ZVsWmD-ARnWiAABAhya2V0c772.png
    4 M00/00/00/wKg4ZVsWmD-ARnWiAABAhya2V0c772.png
    5 M00/00/00/wKg4ZVsWmD-ARnWiAABAhya2V0c772_60x60.png

    访问第二组第一个路径:

    访问最后一个路径(缩略图路径),注意加组名(group1)

     在service中改造上传逻辑

    红色标记为新加入的

    @Service
    public class UploadService {
    
        @Autowired
        private FastFileStorageClient storageClient;
    
        private static final List<String> CONTENT_TYPES = Arrays.asList("image/jpeg", "image/gif");
    
        private static final Logger LOGGER = LoggerFactory.getLogger(UploadService.class);
    
        public String upload(MultipartFile file) {
    
            String originalFilename = file.getOriginalFilename();
            // 校验文件的类型
            String contentType = file.getContentType();
            if (!CONTENT_TYPES.contains(contentType)){
                // 文件类型不合法,直接返回null
                LOGGER.info("文件类型不合法:{}", originalFilename);
                return null;
            }
    
            try {
                // 校验文件的内容
                BufferedImage bufferedImage = ImageIO.read(file.getInputStream());
                if (bufferedImage == null){
                    LOGGER.info("文件内容不合法:{}", originalFilename);
                    return null;
                }
    
                // 保存到服务器
                // file.transferTo(new File("C:\~~~\images\" + originalFilename));
                String ext = StringUtils.substringAfterLast(originalFilename, ".");
                StorePath storePath = this.storageClient.uploadFile(file.getInputStream(), file.getSize(), ext, null);
    
                // 生成url地址,返回
                return "http://image.~~~.com/" + storePath.getFullPath();
            } catch (IOException e) {
                LOGGER.info("服务器内部错误:{}", originalFilename);
                e.printStackTrace();
            }
            return null;
        }
    }
  • 相关阅读:
    3D流水线
    log4cplus 配置文件的编写
    linux下的log4cplus的安装和使用
    日志信息的编写与调用
    转C++内存池实现
    转:自定义内存池的使用
    在linux查看内存的大小
    转:C++内存池
    数组指针 和指针数组的区别
    new的三种形态
  • 原文地址:https://www.cnblogs.com/TJ21/p/12617226.html
Copyright © 2011-2022 走看看