这是web服务中的方法
[WebMethod(EnableSession = true)]
public void Receive1(string file, long begin, long len, byte[] data, int i, int threadNum)
{
string UploadPath = "D:\\";
//线程下载的临时文件名
string tmpFileBlock = UploadPath + "\\" + file + ".temp" + i.ToString();
File.Create(tmpFileBlock).Close();
using (FileStream fs = new FileStream(tmpFileBlock, FileMode.Open, FileAccess.ReadWrite, FileShare.Read))
{
fs.Seek(0, SeekOrigin.Begin);
fs.Write(data, 0, data.Length);
Thread.sleep(500);//刚增加
}
if (i == threadNum - 1)
{
Complete(UploadPath, file, threadNum);
}
}
客户端是多线程调用Run
这是一个类中的方法,在类构造器中已经把end,begin, this.explore(为提供web服务的类)
public void Run()
{
Stream fs = File.OpenRead(this.path+"\\"+this.fielName);
long len =this.end-this.begin;
byte[] data = new byte[len];
fs.Position = this.begin;
fs.Read(data, 0, (int)len);
Console.WriteLine("------------------Run---------------------");
//call webservice to receive the data. this can be a socket also.
lock (data)
{
this.explore.Receive1("juanzi.jpg", this.begin, len, data, this.no, this.threadNum);//报错:System.Web.Services.Protocols.SoapException: 服务器无法处理请求。 ---> System.NullReferenceException: 未将对象引用设置到对象的实例
Thread.Sleep(300);
}
}
怎么回事,老报这个错误。求高手。
原来是
Complete(UploadPath, file, threadNum);中 List<string> _tempFiles = null;
按你的要求改了List<string> _tempFiles =new List<string>();
又出现另一个问题:
/// <summary>
/// 将下载的临时文件进行合并
/// </summary>
private void Complete(string path, string fileName, int threadNum)
{
Stream mergeFile = new FileStream(path + "\\" + fileName, FileMode.Create);
BinaryWriter AddWriter = new BinaryWriter(mergeFile);
List<string> _tempFiles = new List<string>();
for (int i = 0; i < threadNum; i++)
{
_tempFiles.Add(fileName + ".tmp" + i.ToString()); }
foreach (string file in _tempFiles)
{
using (FileStream fs = new FileStream(file, FileMode.Open))//报错未能找到文件.juanzi.jpg.temp0
{
BinaryReader TempReader = new BinaryReader(fs);
AddWriter.Write(TempReader.ReadBytes((int)fs.Length));
TempReader.Close();
}
File.Delete(file);
}
AddWriter.Close();
Receive1中
string tmpFileBlock = UploadPath + "\\" + file + ".temp" + i.ToString();
--------------------------------------------------------------------------------------
Complete
_tempFiles.Add(fileName + ".tmp" + i.ToString());
-------------------------------------------------------------------------------------------
思路是,对于传输文件都是根据文件大小制定线程个数。现在发现一个问题:现在有一个问题,几十M的文件能传输。几百M的报内存不够等错误。
是不是byte[]的长度问题?
谢谢。