FastDFS工具类

fastDFS工具类

  • maven依赖
<!--fastDFS-->
<dependency>
   <groupId>com.github.tobato</groupId>
   <artifactId>fastdfs-client</artifactId>
   <version>1.26.6</version>
</dependency>
  • 配置类
package com.config.fdfs;

import com.github.tobato.fastdfs.FdfsClientConfig;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.context.annotation.Import;
import org.springframework.jmx.support.RegistrationPolicy;

import javax.servlet.MultipartConfigElement;
import java.io.File;

/**
 * fastDFS Config By CHENYB date 2019-08-14
 */
@Configuration
@Import(FdfsClientConfig.class)//引入前辈封装的配置
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)//解决jmx重复注册bean的问题
public class FastDFSConfig {

    /**
     * 设置文件上传临时路径
     */
    @Bean
    MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        String location = System.getProperty("user.dir") +File.separator+ "data"+File.separator+"tmp";
        File tmpFile = new File(location);
        if (!tmpFile.exists()) {
            tmpFile.mkdirs();
        }
        factory.setLocation(location);
        return factory.createMultipartConfig();
    }
}
  • 工具类 
package com.utils;

import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.nio.charset.Charset;

/**
 * FastDFS 工具类 By CHENYB date 2019-08-14
 * fastdfs-client version 1.26.6
 */
@Component
public class FastDFSClient {

    private final Logger logger = LoggerFactory.getLogger(FastDFSClient.class);

    @Autowired
    private FastFileStorageClient storageClient;

    /**
     * 文件上传 (MultipartFile)
     */
    public String uploadFile(MultipartFile file) throws IOException {
        StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(),
                FilenameUtils.getExtension(file.getOriginalFilename()), null);
        return getResAccessUrl(storePath);
    }

    /**
     * 文件上传 (File)
     */
    public String uploadFile(File file) throws IOException {
        FileInputStream inputStream = new FileInputStream(file);
        StorePath path = storageClient.uploadFile(inputStream, file.length(),
                FilenameUtils.getExtension(file.getName()), null);
        return getResAccessUrl(path);
    }

    /**
     * 文件上传 (InputStream)
     */
    public String uploadFile(InputStream is, long size, String fileName) {
        StorePath path = storageClient.uploadFile(is, size, fileName, null);
        return getResAccessUrl(path);
    }

    /**
     * 将一段文本文件写到fastdfs的服务器上
     */
    public String uploadFile(String content, String fileExtension) {
        byte[] buff = content.getBytes( Charset.forName("UTF-8"));
        ByteArrayInputStream stream = new ByteArrayInputStream(buff);
        StorePath path = storageClient.uploadFile(stream, buff.length, fileExtension, null);
        return getResAccessUrl(path);
    }

    /**
     * 返回文件上传成功后的地址名称ַ
     */
    private String getResAccessUrl(StorePath storePath) {
        String fileUrl = storePath.getFullPath();
        return fileUrl;
    }

    /**
     * 下载文件 (文件url 文件路径)
     */
    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;
    }

    /**
     * 删除文件
     */
    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());
        }
    }

    /**
     * 返回后缀名包含.
     */
    public String getSuffixName(MultipartFile file){
        String originalFilename = file.getOriginalFilename();
        return originalFilename.substring( originalFilename.lastIndexOf( "." ),originalFilename.length() );
    }

    /**
     * 返回文件名
     */
    public String getFileName(MultipartFile file){
        String originalFilename = file.getOriginalFilename();
        return originalFilename.substring( 0, originalFilename.lastIndexOf( "." ));
    }
}

 Mr.chenyb 随笔记录,方便自己学习

2019-08-19

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Windows下使用FastDFS需要进行以下步骤: 1. 安装FastDFS依赖的软件: - 安装C语言编译器,如MinGW、Cygwin或者Visual Studio等。 - 安装libevent库,可以从官网下载源码编译安装,也可以从第三方网站下载预编译的版本。 - 安装libfastcommon库,可以从GitHub上下载源码编译安装。 2. 下载FastDFS源码: - 从FastDFS官方网站下载最新版本的源码压缩包,解压到本地。 3. 编译FastDFS: - 打开命令行界面(如cmd),进入FastDFS源码目录。 - 执行命令`make`编译FastDFS。 - 执行命令`make install`安装FastDFS。 4. 配置FastDFS: - 进入FastDFS安装目录,编辑`tracker.conf`和`storage.conf`配置文件。 - 根据实际需求修改配置参数,如Tracker服务器IP、端口等。 - 配置存储节点的IP、端口、存储路径等信息。 - 保存配置文件并退出。 5. 启动FastDFS: - 执行命令`trackerd start`启动Tracker服务器。 - 执行命令`storaged start`启动存储服务器。 6. 测试FastDFS: - 使用FastDFS提供的命令行工具或者API进行文件上传、下载等操作,验证FastDFS是否正常运行。 请注意,FastDFS是一个基于Linux的分布式文件系统,官方并没有提供Windows下的官方支持。上述步骤是通过在Windows环境下安装依赖库以及编译源码来实现的。在Windows环境下使用FastDFS可能会遇到一些兼容性或者功能限制的问题,建议在Linux环境下部署和使用FastDFS以获得更好的稳定性和性能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值