FastDFS-使用篇

一、FastDFS操作-linux

①、上传指令

格式: f d f s _ u p l o a d _ f i l e   < c o n f i g _ f i l e >   < l o c a l _ f i l e n a m e >   [ s t o r a g e _ i p : p o r t ] [ s t o r e _ p a t h _ i n d e x ] \textcolor{Cyan}{fdfs\_upload\_file\ <config\_file>\ <local\_filename>\ [storage\_ip:port] [store\_path\_index]} fdfs_upload_file <config_file> <local_filename> [storage_ip:port][store_path_index]

  • 返回文件id:file_id:group1/M00/00/00/wKhCQmNvZfSATcjYAA-isSgmG5o439.jpg

参数含义:

  1. <config_file> :配置文件路径(/etc/fdfs/client.conf:上面配置的路径)
  2. <local_filename> :本地文件路径(./文件名)
  3. [storage_ip:port] :(可选参数)
  4. [store_path_index] :(可选参数)

②、下载指令

格式: f d f s _ d o w n l o a d _ f i l e   < c o n f i g _ f i l e >   < f i l e _ i d >   [ l o c a l _ f i l e n a m e ] [ < d o w n l o a d _ o f f s e t >   < d o w n l o a d _ b y t e s > ] \textcolor{Cyan}{fdfs\_download\_file\ <config\_file>\ <file\_id>\ [local\_filename] [<download\_offset>\ <download\_bytes>]} fdfs_download_file <config_file> <file_id> [local_filename][<download_offset> <download_bytes>]

参数含义:

  1. <config_file> :配置文件路径(/etc/fdfs/client.conf:上面配置的路径)
  2. <file_id> :文件在FastDFS中的唯一文件标识,即卷名+文件名(文件上传返回的id)
  3. [local_filename] :(可选参数)文件下载地址
  4. <download_offset> :(可选参数)文件下载开始时间
  5. <download_bytes> :(可选参数)文件下载的字节数

③、查看文件信息指令

格式: f d f s _ f i l e _ i n f o   < c o n f i g _ f i l e >   < f i l e _ i d > \textcolor{Cyan}{fdfs\_file\_info\ <config\_file>\ <file\_id>} fdfs_file_info <config_file> <file_id>

参数含义:

  1. <config_file> :配置文件路径(/etc/fdfs/client.conf:上面配置的路径)
  2. <file_id> :文件在FastDFS中的唯一文件标识,即卷名+文件名(文件上传返回的id)

④、删除指令

格式: f d f s _ d e l e t e _ f i l e   < c o n f i g _ f i l e >   < f i l e _ i d > \textcolor{Cyan}{fdfs\_delete\_file\ <config\_file>\ <file\_id>} fdfs_delete_file <config_file> <file_id>

参数含义:

  1. <config_file> :配置文件路径(/etc/fdfs/client.conf:上面配置的路径)
  2. <file_id> :文件在FastDFS中的唯一文件标识,即卷名+文件名(文件上传返回的id)

二、FastDFS操作-Springboot

version1.0

导入依赖

    <dependencies>
        <!-- 起步依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!-- Junit 起步依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- springmvc 起步依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- thymeleaf起步依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <!-- MyBatisPlus 起步依赖-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.1</version>
        </dependency>
        <!-- mysql驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.29</version>
        </dependency>
        <!-- druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.8</version>
        </dependency>
        <!-- lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <!-- fastdfs 客户端 -->
        <dependency>
            <groupId>com.github.tobato</groupId>
            <artifactId>fastdfs-client</artifactId>
            <version>1.26.5</version>
        </dependency>
    </dependencies>

    <!-- 阿里云仓库配置 -->
    <repositories>
        <repository>
            <id>maven-ali</id>
            <url>http://maven.aliyun.com/nexus/content/groups/public//</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
                <updatePolicy>always</updatePolicy>
                <checksumPolicy>fail</checksumPolicy>
            </snapshots>
        </repository>
    </repositories>

配置文件

在applacation.yml配置fastdfs地址

# 日志格式
logging:
  pattern:
    console: '[%yellow(%date{yyyy-MM-dd HH:mm:ss.SSS}) %highlight(%-5level)] [%cyan(%X{traceid}) %magenta(%-15thread)] [%green(%-50logger){50}] : %.4000m%n'

################################ 分布式文件系统的配置 ################################
fdfs:
  # 超时时间
  connect-timeout: 600
  # 连接时间
  so-timeout: 1500
  #缩略图生成参数
  thumb-image:
   width: 150
   height: 150
  # 服务器地址
  tracker-list:
    - 192.168.xx.xx:22122

使用FastDFS

controller编写、上传、下载

  • fastFileStorageClient对象
①、上传
  1. uploadFile:上传

    • 参数一:传输文件内容的输入流;
    • 参数二:文件的size;
    • 参数三:文件扩展名;
    • 参数四:描述文件的元数据;

    注意 \textcolor{red}{注意} 注意:返回值:上传文件在存储节点的唯一标识(卷名 + 文件名)

@Controller
@RequestMapping("file")
public class FileController {
    //fastdfs存储节点的客户端对象
    @Autowired
    private FastFileStorageClient fastFileStorageClient;
    @Autowired
    private IFiletestService fileService;

    /**
     * 文件上传
     * @param files
     * @return
     * @throws Exception
     */
    @RequestMapping("/fileUpload")
    public String upload(MultipartFile files[]) throws Exception {
        for (MultipartFile file : files) {
            /**
             * 文件上传:
             * 参数一:传输文件内容的输入流;
             * 参数二:文件的size;
             * 参数三:文件扩展名;
             * 参数四:描述文件的元数据;
             * 返回值:上传文件在存储节点的唯一标识(卷名 + 文件名)
             */
            StorePath storePath = fastFileStorageClient.uploadFile(
                    file.getInputStream(),
                    file.getSize(),
                    file.getOriginalFilename()
                            .substring(file
                                    .getOriginalFilename()
                                    .lastIndexOf(".")+1),
                    null);
            // 保存文件信息
            fileService.uploadMassage(Filetest.builder()
                    .name(file.getOriginalFilename())
                    .tag(storePath.getGroup())
                    .url(storePath.getPath())
                    .build());
        }
        return "redirect:/file/showFiles";
    }
}
②、下载
  1. downloadFile:下载

    • 参数一:文件处于存储节点的卷名;
    • 参数二:文件在存储节点的文件名;
    • 参数三:下载的回调函数;

    注意 1 \textcolor{red}{注意1} 注意1:返回值:文件内容的字节数组;

    注意 2 \textcolor{red}{注意2} 注意2:乱码问题:

    // 设置响应头
    response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName,"UTF-8"));
    
@Controller
@RequestMapping("file")
public class FileController {
    //fastdfs存储节点的客户端对象
    @Autowired
    private FastFileStorageClient fastFileStorageClient;
    @Autowired
    private IFiletestService fileService;

    // 文件下载
    @RequestMapping("/download")
    public void fileDown(String fileName, HttpServletResponse response, HttpServletRequest request) throws IOException {
        // 设置响应头
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName,"UTF-8"));
        Filetest file = fileService.getFileByName(fileName);
        /**
         * 下载文件:
         * 参数一:文件处于存储节点的卷名;
         * 参数二:文件在存储节点的文件名;
         * 参数三:下载的回调函数;
         * 返回值:文件内容的字节数组
         */
        byte[] bytes = fastFileStorageClient.downloadFile(
                file.getTag(),
                file.getUrl(),
                new DownloadByteArray());

        // 获取字节输出流
        ServletOutputStream os = response.getOutputStream();
        // 使用输出流写出文件
        os.write(bytes);
        os.flush();
        os.close();
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值