WCF传输大数据 --断点续传(upload、download)

 using System;
using System.IO;
using System.Runtime.Serialization;
using System.ServiceModel; namespace WcfServer
{
internal class Program
{
private static void Main()
{
using (var host = new ServiceHost(typeof (StreamServices)))
{
host.Opened += (a, b) => Console.WriteLine("...");
host.Open(); Console.ReadKey();
}
}
} [ServiceContract(Name = "IStreamServices",
SessionMode = SessionMode.Required,
Namespace = "http://www.msdn.com/IStreamServices/14/04/11")]
public interface IStreamServices
{
[OperationContract(Name = "Upload")]
FileInformation Upload(FileInformation fileInfo); [OperationContract(Name = "Download")]
FileInformation Download(FileInformation fileInfo);
} [ServiceBehavior(Name = "StreamServices",
Namespace = "http://www.msdn.com/StreamServices/14/04/11"
, InstanceContextMode = InstanceContextMode.PerCall,
ConcurrencyMode = ConcurrencyMode.Multiple)]
public class StreamServices : IStreamServices
{
private static readonly string AttachmentPath = AppDomain.CurrentDomain.BaseDirectory + "Attachments//"; #region IServices 成员 public FileInformation Upload(FileInformation fileInfo)
{
try
{
if (fileInfo == null)
return new FileInformation {Error = "FileInformation对象不能为空"};
if (string.IsNullOrEmpty(fileInfo.FileNameNew))
fileInfo.FileNameNew = Guid.NewGuid() + fileInfo.FileSuffix;
var savePath = AttachmentPath + fileInfo.FileNameNew;
using (var fs = new FileStream(savePath, FileMode.OpenOrCreate))
{
long offset = fileInfo.Offset;
using (var write = new BinaryWriter(fs))
{
write.Seek((int) offset, SeekOrigin.Begin);
write.Write(fileInfo.Data);
fileInfo.Offset = fs.Length;
}
}
return fileInfo;
}
catch (IOException ex)
{
return new FileInformation {Error = ex.Message};
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
return new FileInformation {Error = "wcf内部错误"};
}
} public FileInformation Download(FileInformation fileInfo)
{
try
{
if (fileInfo == null)
return new FileInformation {Error = "FileInformation对象不能为空"};
var readFileName = AttachmentPath + fileInfo.FileName;
if (!File.Exists(readFileName))
return new FileInformation {Error = "DirectoryNotFoundException"};
var stream = File.OpenRead(readFileName);
fileInfo.Length = stream.Length;
if (fileInfo.Offset.Equals(fileInfo.Length))
return new FileInformation {Offset = fileInfo.Offset, Length = stream.Length};
var maxSize = fileInfo.MaxSize > * ? * : fileInfo.MaxSize;
fileInfo.Data =
new byte[fileInfo.Length - fileInfo.Offset <= maxSize ? fileInfo.Length - fileInfo.Offset : maxSize];
stream.Position = fileInfo.Offset;
stream.Read(fileInfo.Data, , fileInfo.Data.Length);
return fileInfo;
}
catch (IOException ex)
{
return new FileInformation {Error = ex.Message};
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
return new FileInformation {Error = "wcf内部错误"};
}
} #endregion
} [DataContract(Name = "MyStreamInfo")]
public class FileInformation
{
[DataMember(Name = "Length", IsRequired = true)]
public long Length { get; set; } [DataMember(Name = "FileName", IsRequired = true)]
public string FileName { get; set; } [DataMember(Name = "Data")]
public byte[] Data { get; set; } [DataMember(Name = "FileSuffix")]
public string FileSuffix { get; set; } [DataMember(Name = "Offset", IsRequired = true)]
public long Offset { get; set; } [DataMember(Name = "Error")]
public string Error { get; set; } private int maxSize = *; //200k [DataMember(Name = "MaxSize")]
public int MaxSize
{
get { return maxSize; }
set { maxSize = value; }
} [DataMember(Name = "KeyToken")]
public string KeyToken { get; set; } [DataMember(Name = "FileNameNew")]
public string FileNameNew { get; set; }
}
}
 using System;
using System.IO;
using WcfClientApp.ServiceReference1; namespace WcfClientApp
{
internal class Program
{
private static void Main()
{
Upload();
Download();
Console.ReadKey();
} /// <summary>
/// 上传
/// </summary>
private static void Upload()
{
try
{
var filePath = AppDomain.CurrentDomain.BaseDirectory + "UploadFiles//张国荣 - 共同度过.mp3";
const string fileName = "张国荣 - 共同度过.mp3";
const int maxSize = *;
if (!File.Exists(filePath))
{
Console.WriteLine("DirectoryNotFoundException");
return;
}
FileStream stream = File.OpenRead(filePath);
var fileInfo = new MyStreamInfo {Length = stream.Length, FileName = fileName,FileSuffix=".mp3"};
using (var client = new StreamServicesClient())
{
while (fileInfo.Length != fileInfo.Offset)
{
fileInfo.Data =
new byte[
fileInfo.Length - fileInfo.Offset <= maxSize
? fileInfo.Length - fileInfo.Offset
: maxSize];
stream.Position = fileInfo.Offset;
stream.Read(fileInfo.Data, , fileInfo.Data.Length);
fileInfo = client.Upload(fileInfo);
if (!string.IsNullOrEmpty(fileInfo.Error))
{
Console.WriteLine(fileInfo.Error);
break;
}
}
if (fileInfo.Length.Equals(fileInfo.Offset))
Console.WriteLine("Upload successful!");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
/// <summary>
/// 下载
/// </summary>
private static void Download()
{
try
{
var filePath = AppDomain.CurrentDomain.BaseDirectory +
"DownloadFiles//c228d4df-8bdc-468b-96ec-46860c2f026a.mp3";
const string fileName = "c228d4df-8bdc-468b-96ec-46860c2f026a.mp3";
var fileInfo = new MyStreamInfo {FileName = fileName, Length = , MaxSize = *};
using (var client = new StreamServicesClient())
{
while (fileInfo.Length != fileInfo.Offset)
{
fileInfo = client.Download(fileInfo);
if (!string.IsNullOrEmpty(fileInfo.Error))
{
Console.WriteLine(fileInfo.Error);
break;
}
using (var fs = new FileStream(filePath, FileMode.OpenOrCreate))
{
long offset = fileInfo.Offset;
using (var write = new BinaryWriter(fs))
{
write.Seek((int) offset, SeekOrigin.Begin);
write.Write(fileInfo.Data);
fileInfo.Offset = fs.Length;
}
}
}
if (fileInfo.Length.Equals(fileInfo.Offset))
Console.WriteLine("download successful!");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}

使用MaxSize控制下载、上传大小(k)

一开始设计的时候,我考虑使用双工模式,但是觉得有不太好,使用双工模式反而增加了代码开发量。

不知道各位有没有什么更好的解决办法?是否愿意分享一下,谢谢各位的指点。

上一篇:MongoDB 用MongoTemplate查询指定时间范围的数据


下一篇:ceph主要数据结构解析3-Ceph_fs.h文件