FastDFS.Client操作文件服务器
2024-10-09 07:31:47
1、配置文件设置
<configSections>
<section name="fastdfs" type="FastDFS.Client.Config.FastDfsConfigurationSectionHandler, FastDFS.Client" />
</configSections> <fastdfs>
<FastDfsConfig GroupName="group1">
<FastDfsServer IpAddress="192.168.88.103" Port="22122" />
</FastDfsConfig>
</fastdfs>
2、FastDFS.Client API调用
上传:
//文件保存到FastDFS服务器
var config = FastDfsManager.GetConfigSection();
ConnectionManager.InitializeForConfigSection(config);
StorageNode storageNode = FastDFSClient.GetStorageNode(config.GroupName); string filePath = FastDFSClient.UploadFile(storageNode, file.Data, file.Extension.Replace(".", ""));
下载:
private Task<byte[]> DownloadFileAsyn(string filePath)
{
return Task.Run(() =>
{
List<byte> content = new List<byte>();
var config = FastDfsManager.GetConfigSection();
ConnectionManager.InitializeForConfigSection(config);
StorageNode storageNode = FastDFSClient.GetStorageNode(config.GroupName);
FDFSFileInfo fileInfo = FastDFSClient.GetFileInfo(storageNode, filePath);
if (fileInfo.FileSize >= ) //文件内容大于1024字节时,需要分批下载
{
long offset = , len = ;
while (len > )
{
byte[] buffer = FastDFSClient.DownloadFile(storageNode, filePath, offset, len);
content.AddRange(buffer);
offset += len;
len = Math.Min(fileInfo.FileSize - offset, );
}
}
else
{
content.AddRange(FastDFSClient.DownloadFile(storageNode, filePath));
} return content.ToArray();
});
}
删除:
// 从FastDFS服务器删除
var config = FastDfsManager.GetConfigSection();
ConnectionManager.InitializeForConfigSection(config);
FastDFSClient.RemoveFile(config.GroupName, path);
最新文章
- bzoj 4003
- 【JAVA之泛型】
- android 判断屏幕是否亮着
- 18种CSS3loading效果完整版,兼容各大主流浏览器,提供在线小工具使用
- 管理员必备的20个Linux系统监控工具
- android webview js alert对话框 不能弹出 解决办法
- cloud-utils cloud-utils-growpart cloud-init
- 【AngularJS】——0.分析
- 了解HTML的代码注释
- 11G在线重建索引
- (转载)iscroll.js的使用
- Android布局_LinearLayout布局
- CSS3制作日历
- TestNG简介与安装步骤
- Bootstrap全局CSS样式之排版
- ubuntu12.04:Mysql数据库:自动安装
- 浅谈OA系统与Portal门户的区别
- Spring学习之旅(一)极速创建Spring框架java工程项目
- Spark思维导图之性能优化
- Python3+Selenium环境配置