WS客户端上传下载方法
来源:互联网 发布:搜狐畅游网络招聘 编辑:程序博客网 时间:2024/06/05 17:19
GWExWebService.AuthHeader header;
GWExWebService.GWExWebServiceSoapClient ws ;
#region 初始化构造函数
public WSClass()
{
SysConfig config = new SysConfig();
ws = new GWExWebService.GWExWebServiceSoapClient();
header = new GWExWebService.AuthHeader();
header.Username = config.WSUserName;
header.Password = config.WSPassWord;
header.UnitNo = config.UnitNo;
}
#endregion
#region 上传附件方法
/// <summary>
/// 上传附件方法
/// </summary>
/// <param name="RemotoFileName">WS文件名</param>
/// <param name="LocalSaveFile">本地文件物理路径</param>
/// <returns></returns>
public bool UpLoadFile(string RemotoFileName, string LocalSaveFile)
{
bool AutoSetChunkSize = true;//自动调整每块数据的大小
bool IncludeHashVerification = true;//检查文件是否完整
int MaxRetries = 50;//尝试最大连接WS的次数
int NumRetries = 0;//当前连接WS的次数
int AverageSimple = 5;//尝试传输 5 次后调整每块数据的大小
int ChunkSize = 16 * 1024;//每块数据的大小
int PerferredTransferDuration = 1500;//每块数据传输所用时间的毫秒数
string LocalFileHash;//本地文件哈希值
string RemoteFileHash;//WS文件哈希值
string LocalFilePath;//本地文件全路径 c:\a\v\s.doc
int numIterations = 0;//文件块个数
DateTime StartTime = DateTime.Now;
LocalFilePath = LocalSaveFile;
string RemotoFilePath = RemotoFileName;
#region 分块传输文件
if (AutoSetChunkSize) ChunkSize = 16 * 1024;
long fileSize = new FileInfo(LocalFilePath).Length;
long SentBytes = 0;
byte[] buffer = new byte[ChunkSize];
using (FileStream fs = new FileStream(LocalFilePath, FileMode.Open, FileAccess.Read))
{
int BytesRead = fs.Read(buffer, 0, ChunkSize);
while (BytesRead > 0)
{
try
{
UpLoadFile(RemotoFilePath, buffer, SentBytes, BytesRead);
if (numIterations == 1)
StartTime = DateTime.Now;
if (AutoSetChunkSize && numIterations == AverageSimple + 1)
{
long timeForInitialChunks = (long)DateTime.Now.Subtract(StartTime).TotalMilliseconds;
long averageChunkTime = Math.Max(1, timeForInitialChunks / AverageSimple);
ChunkSize = (int)Math.Min(4000000, ChunkSize * PerferredTransferDuration / averageChunkTime);
buffer = new byte[ChunkSize];
}
SentBytes += BytesRead;
}
catch (Exception ex)
{
if (NumRetries++ < MaxRetries)
{
fs.Position -= BytesRead;
}
else
{
fs.Close();
throw ex;
}
}
BytesRead = fs.Read(buffer, 0, ChunkSize);
numIterations++;
}
}
#endregion
#region 哈希值对比文件
if (IncludeHashVerification)
{
LocalFileHash = CheckLocalFileHash(LocalFilePath);
RemoteFileHash = GetFileHash(RemotoFilePath);
if (LocalFileHash == RemoteFileHash)
{
return true;
}
else
{
return false;
}
}
#endregion
return false;
}
/// <summary>
/// WS分开上传文件方法
/// </summary>
/// <param name="strFilePath">文件名</param>
/// <param name="buffer">上传的内存值</param>
/// <param name="Offset">文件内存偏移值</param>
/// <param name="BytesRead">已上传的文件内存大小</param>
/// <returns></returns>
public bool UpLoadFile(string strFilePath, byte[] buffer, long Offset, int BytesRead)
{
return ws.UpLoadFile(header,strFilePath,buffer,Offset,BytesRead);
}
#endregion
#region 下载文件方法
/// <summary>
/// 下载文件方法
/// </summary>
/// <param name="RemotoFileName">WS文件名</param>
/// <param name="LocalSaveFile">本地文件物理路径</param>
/// <returns></returns>
public bool DownLoadFile(string RemotoFileName, string LocalSaveFile)
{
bool AutoSetChunkSize = true;//自动调整每块数据的大小
bool IncludeHashVerification = true;//检查文件是否完整
int MaxRetries = 50;//尝试最大连接WS的次数
int NumRetries = 0;//当前连接WS的次数
int AverageSimple = 5;//尝试传输 5 次后调整每块数据的大小
int ChunkSize = 16 * 1024;//每块数据的大小
int PerferredTransferDuration = 1500;//每块数据传输所用时间的毫秒数
string LocalFileHash;//本地文件哈希值
string RemoteFileHash;//WS文件哈希值
string LocalFilePath;//本地文件全路径 c:\a\v\s.doc
int numIterations = 0;//文件块个数
DateTime StartTime = DateTime.Now;
LocalFilePath = LocalSaveFile;
string LocalFileFolder = LocalFilePath.Substring(0, LocalFilePath.LastIndexOf("\\"));
if (!Directory.Exists(LocalFileFolder))
Directory.CreateDirectory(LocalFileFolder);
if (File.Exists(LocalFilePath))
{
File.Create(LocalFilePath).Close();
}
long Filesize = GetFileSize(RemotoFileName);
long ReceivedBytes = 0;
//分块下载文件
using (FileStream fs = new FileStream(LocalFilePath, FileMode.Append, FileAccess.Write))
{
while (ReceivedBytes < Filesize)
{
if (AutoSetChunkSize && numIterations == AverageSimple)
{
long timeForInitialChunks = (long)DateTime.Now.Subtract(StartTime).TotalMilliseconds;
long averageChunkTime = Math.Max(1, timeForInitialChunks / AverageSimple);
ChunkSize = (int)Math.Min(4000000, ChunkSize * PerferredTransferDuration / averageChunkTime);
}
try
{
byte[] Buffer = LoadFile(RemotoFileName, ReceivedBytes, ChunkSize);
fs.Write(Buffer, 0, Buffer.Length);
ReceivedBytes += Buffer.Length;
}
catch (Exception ex)
{
if (NumRetries < MaxRetries)
{
NumRetries++;
continue;
}
else
{
fs.Close();
throw new Exception(string.Format("在下载文件过程中 {0},请修复", ex.Message));
}
}
numIterations++;
}
}
//检验文件是否与服务器相同,进行哈希校验
if (IncludeHashVerification)
{
LocalFileHash = CheckLocalFileHash(LocalFilePath);
RemoteFileHash = GetFileHash(RemotoFileName);
if (LocalFileHash == RemoteFileHash)
{
return true;
}
else
{
return false;
}
}
return false;
}
/// <summary>
/// WS分块下载文件方法
/// </summary>
/// <param name="strFilePath">文件名</param>
/// <param name="offset">文件内存偏移值</param>
/// <param name="BufferSize">每次上传的内存大小</param>
/// <returns></returns>
public byte[] LoadFile(string strFilePath, long offset, int BufferSize)
{
return ws.LoadFile(header, strFilePath, offset, BufferSize);
}
/// <summary>
/// 获取WS文件大小
/// </summary>
/// <param name="RemotoFileName">WS文件名</param>
/// <returns></returns>
public long GetFileSize(string RemotoFileName)
{
return ws.GetFileSize(header, RemotoFileName);
}
#endregion
#region 公共方法
/// <summary>
///获取本地文件hash值
/// </summary>
/// <param name="LocalFilePath">本地文件物理路径</param>
/// <returns></returns>
public string CheckLocalFileHash(string LocalFilePath)
{
SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
byte[] hash;
using (FileStream fs = new FileStream(LocalFilePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096))
hash = sha1.ComputeHash(fs);
return BitConverter.ToString(hash);
}
/// <summary>
/// 获取WS服务器指定文件的hash值
/// </summary>
/// <param name="strFilePath">WS服务器指定文件名</param>
/// <returns></returns>
public string GetFileHash(string strFilePath)
{
return ws.GetFileHash(header, strFilePath);
}
#endregion
- WS客户端上传下载方法
- WS上传下载方法
- webservices系列(二)——JAX-WS文件上传下载
- axis,jax-ws,xfire客户端调用
- JAX-WS 学习二:创建客户端
- (三) CXF 客户端调用WS服务
- jax-ws 获取客户端相关信息
- Intellij Idea 下 生成WebServiceClient (WS客户端)
- jax-ws 创建服务端和客户端
- ws
- WS
- 一个简单的FTP客户端 实现上传下载
- java实现的FTP上传下载客户端
- PowerShell FTP客户端上传下载模块的编写
- jersey上传下载文件及客户端调用代码
- VC实现文件的上传下载 客户端
- java实现的FTP上传下载客户端
- C#关于文件上传下载方法
- 黑马程序员_静态初始化器的定义(initializer)
- JAVA SFTP工具类
- CentOS下常用软件
- Python 中的lambda
- dq变换终解
- WS客户端上传下载方法
- 八数码的八境界
- centos6.0桌面配置
- 谈工程师的价值和发展
- 招聘移动开发,老板必问的10个问题
- Android Activity传递自定义对象
- 一个PDF合并操作实现类,利用iTextSharp控件实现
- Oracle中dmp文件的导入导出总结
- 第十一篇:赋值,表达式,print