fastdfs实现文件上传下载的通用方法

首先这里依赖的是:

 <dependency>
            <groupId>net.oschina.zcx7878</groupId>
            <artifactId>fastdfs-client-java</artifactId>
            <version>1.27.0.0</version>
        </dependency>

直接controller层:

    public Result down() {
        String path = "http://10.1.101.97:8888/group1/M00/00/00/CgFlYWKBxI2AFqmpAAATxT3V0rE142.jpg";
        String groupName = "group1";
        String remoteName = "M00/00/00/CgFlYWKBxI2AFqmpAAATxT3V0rE142.jpg";


        try {
            InputStream inputStream = FastDFSClient.downFile(groupName, remoteName);
            FileOutputStream outputStream = new FileOutputStream("D:/1.jpg");
            byte[] buffer = new byte[1024];

            while (inputStream.read(buffer) != -1) {
                outputStream.write(buffer);
            }
            if(inputStream!=null){
                inputStream.close();
            }

            if(outputStream!=null){
                outputStream.flush();
                outputStream.close();
            }



        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(true, StatusCode.OK, "下载成功");
    }

over

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为了实现C++的FastDFS下载,需要使用FastDFS C客户端API。以下是实现FastDFS下载的步骤: 1.安装FastDFS C客户端API 可以从FastDFS官方网站下载FastDFS C客户端API。下载后,将其解压缩并安装。 2.上文件 使用FastDFS C客户端API上文件的步骤如下: ```c++ #include "fastdfs_client.h" #include "fastdfs_client_ex.h" int main(int argc, char *argv[]) { char *conf_filename = "/etc/fdfs/client.conf"; char *local_filename = "/path/to/local/file"; char group_name[FDFS_GROUP_NAME_MAX_LEN + 1]; char remote_filename[4096]; ConnectionInfo *pTrackerServer; int result; int64_t file_size; FDFSFileInfo file_info; // 初始化FastDFS客户端 if ((result = fdfs_client_init(conf_filename)) != 0) { return result; } // 获取Tracker服务器 pTrackerServer = tracker_get_connection(); if (pTrackerServer == NULL) { fdfs_client_destroy(); return errno != 0 ? errno : ECONNREFUSED; } // 上文件 result = fdfs_upload_by_filename(pTrackerServer, NULL, local_filename, NULL, 0, remote_filename, sizeof(remote_filename)); if (result != 0) { printf("upload file fail, error no: %d, error info: %s\n", result, STRERROR(result)); tracker_disconnect_server_ex(pTrackerServer, true); fdfs_client_destroy(); return result; } // 获取上文件的信息 result = fdfs_get_file_info_ex(pTrackerServer, NULL, remote_filename, strlen(remote_filename), &file_info); if (result != 0) { printf("get file info fail, error no: %d, error info: %s\n", result, STRERROR(result)); tracker_disconnect_server_ex(pTrackerServer, true); fdfs_client_destroy(); return result; } // 打印上文件的信息 printf("group name: %s\n", file_info.group_name); printf("remote filename: %s\n", file_info.filename); printf("file size: %ld\n", file_info.file_size); printf("create time: %s\n", formatDatetime(file_info.create_timestamp, "%Y-%m-%d %H:%M:%S", NULL, 0)); printf("source ip address: %s\n", file_info.source_ip_addr); // 断开Tracker服务器连接 tracker_disconnect_server_ex(pTrackerServer, true); // 销毁FastDFS客户端 fdfs_client_destroy(); return 0; } ``` 3.下载文件 使用FastDFS C客户端API下载文件的步骤如下: ```c++ #include "fastdfs_client.h" #include "fastdfs_client_ex.h" int main(int argc, char *argv[]) { char *conf_filename = "/etc/fdfs/client.conf"; char *local_filename = "/path/to/local/file"; char group_name[FDFS_GROUP_NAME_MAX_LEN + 1]; char remote_filename[4096]; ConnectionInfo *pTrackerServer; int result; int64_t file_size; FDFSFileInfo file_info; // 初始化FastDFS客户端 if ((result = fdfs_client_init(conf_filename)) != 0) { return result; } // 获取Tracker服务器 pTrackerServer = tracker_get_connection(); if (pTrackerServer == NULL) { fdfs_client_destroy(); return errno != 0 ? errno : ECONNREFUSED; } // 下载文件 result = fdfs_download_file_ex(pTrackerServer, NULL, remote_filename, strlen(remote_filename), 0, &file_size, local_filename); if (result != 0) { printf("download file fail, error no: %d, error info: %s\n", result, STRERROR(result)); tracker_disconnect_server_ex(pTrackerServer, true); fdfs_client_destroy(); return result; } // 断开Tracker服务器连接 tracker_disconnect_server_ex(pTrackerServer, true); // 销毁FastDFS客户端 fdfs_client_destroy(); return 0; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值