webservice大文件传输

来自: http://blog.sina.com.cn/s/blog_5245a6580100vw5r.html

            和   http://blog.sina.com.cn/s/blog_5245a6580100w7wd.html 


好吧,我想我的写写了。我用的方法很简单,首先先把大文件转换成byte数组,然后调用webservice将byte数组转换成文件。但是如果文件大小超过25M的时候会超时,所以我将这个数组分批传输,每一批都是一个20M的byte数组,然后把这些数组追加,最后形成一个完整的文件。注意在形成文件的时候要判断要生成的文件是否存在,否则如果文件已经存在的话,该文件不会被覆盖,而是被追加。



using System;
using System.Collections.Generic;

using System.Text;
using System.IO;

namespace controtest
{
    classProgram
    {
       static byte[] buffer;
       static void Main(string[] args)
       {
           string rst =string.Empty ;
           PartnerWebservice.PartnerServices pa = newPartnerWebservice.PartnerServices();
           string path =System.Configuration.ConfigurationSettings.AppSettings["path"].ToString();//文件的存放目录
           string filename =System.Configuration.ConfigurationSettings.AppSettings["name"].ToString();//文件名称
           Console.WriteLine("ConvertToBinary start at"+DateTime.Now.ToString());
           buffer = ConvertToBinary(path);//将文件转换成byte数组
           int   index = buffer.Length /20971520;//20971520bite就是20M,1*1024*1024*20
           index += buffer.Length % 20971520 == 0 ? 0 : 1;
           bool ifEnd=false;//是否为最后一组
           for (int ii = 0; ii < index; ii++)
           {
               if (ii == index - 1)
                   ifEnd = true;
               Console.WriteLine("ConvertToBinary end at " +DateTime.Now.ToString());
               Console.WriteLine("Trans start at " +DateTime.Now.ToString());
               pa.TransFile(ConvertToBinary(ii, ifEnd),"XXXX.XXX");//分批传输数据,追加到XXXX.XXX文件,如果不存在会自动创建
               Console.WriteLine(rst);
               Console.WriteLine("Trans end at " + DateTime.Now.ToString());
           }
         
         
         Console.ReadLine();
       }
       public static byte[] ConvertToBinary(string Path)
       {
           FileStream stream = new FileInfo(Path).OpenRead();
           byte[] buffer = new byte[stream.Length];
           Console.WriteLine("The lenght of the file is"+buffer.Length);
           stream.Read(buffer, 0, Convert.ToInt32(stream.Length));
           return buffer;
       }
       public static byte[] ConvertToBinary(int index,boolifEnd)//index是第几批,从0开始这个方法名跟上边那个重名了注意区别,两个方法意义是不一样的,我懒得改了
      {
           //20971520
         
       
           //index = buffer.Length / 20971520;
           //index += buffer.Length % 20971520 == 0 ? 0 : 1;
           byte[] bys;//临时二进制数组,最大20M
           if (ifEnd == false)
           {
               bys = new byte[20971520];
               for (int ii = 20971520 * index; ii < 20971520 *(index + 1); ii++)
               {
                   bys[ii - 20971520 * index] = buffer[ii];
               }
           }
           else
           {
               bys = new byte[buffer.Length - 20971520 * (index )];
               for (int ii = 20971520 * index; ii < buffer.Length;ii++)
               {
                   bys[ii - 20971520 * index] = buffer[ii];
               }

           }
           Console.WriteLine("The length of the current buffer is " +bys.Length);
          return bys;
        
       }
       public static byte[] ChangeFileToByte(string path)
       {
           FileStream stream = new FileInfo(path).OpenRead();
           byte[] Filebuffer = new byte[stream.Length];
           stream.Read(Filebuffer, 0, Convert.ToInt32(stream.Length));
           return Filebuffer;
       }
    }
}
以下是webservice部分的方法

[WebMethod(Description = "TransFile")]
    publicstring  TransFile(byte[] fileBt, stringfileName)
    {
       string rst = "successful";
       if (fileBt.Length == 0)
           return "The length of the File"+fileName +"is 0";
       string filePath =System.Configuration.ConfigurationManager.AppSettings["CSVPath"].ToString();  //文件路径

       //FileStream fstream = File.Create(filePath + fileName,fileBt.Length);
       //FileStream fstream = File.AppendAllText(
       FileStream fstream = new FileStream(filePath + fileName,FileMode.Append);
       try
       {
           //MemoryStream m = new MemoryStream(fileBt);
           //m.WriteTo(fstream);
           fstream.Write(fileBt, 0,fileBt.Length);  //二进制转换成文件
         
           fstream.Close();
           //rst += "\r\n";
           rst += "File Name is:" + fstream.Name + "\r\n";
           rst += "File Lenght is:" + fstream.Length + "\r\n";
           rst += "File Position is:" + fstream.Position  +"\r\n";
       }
       catch (Exception ex)
       {
           //抛出异常信息
           rst = ex.ToString();
       }
       finally
       {
          
           fstream.Close();
       }
       StringBuilder sbd = new StringBuilder();
       sbd.AppendLine(rst);
       return sbd.ToString();

    }



webservice大文件传输(优化)

控制台程序,没有注释,在我本机10G数据量没有问题

using System;
using System.Collections.Generic;

using System.Text;
using System.IO;

namespace controtest
{
    classProgram
    {
     
       static void Main(string[] args)
       {
           string rst = string.Empty;
           PartnerWebservice.PartnerServices pa = newPartnerWebservice.PartnerServices();
           string path =System.Configuration.ConfigurationSettings.AppSettings["path"].ToString();
           string filename =System.Configuration.ConfigurationSettings.AppSettings["name"].ToString();
           Console.WriteLine("ConvertToBinary start at " +DateTime.Now.ToString());
         
           long fileLength = LengthOfFile(path);
           long  countOfPk =fileLength /Convert.ToInt64(20971520);
           countOfPk += fileLength % 20971520 == 0 ? 0 : 1;
           Console.WriteLine("The count of the pakeg is " + countOfPk);
           bool ifEnd = false;
           Console.WriteLine("Start");
           Console.WriteLine();
           for (long ii = 0; ii < countOfPk; ii++)
           {
               if (ii == countOfPk - 1)
                   ifEnd = true;
           
               Console.WriteLine("Trans start at " +DateTime.Now.ToString());
               Console.WriteLine("The index is "+(ii+1)+"/"+countOfPk);
               if (ii == 0)
                   pa.TransFile(ConvertToBinary(ii, ifEnd, fileLength), "test.CSV",true);
               else
                   pa.TransFile(ConvertToBinary(ii, ifEnd, fileLength), "test.CSV",false);
               Console.Write(rst);
               Console.WriteLine("Trans end at " + DateTime.Now.ToString());
               Console.WriteLine();
           }

           Console.WriteLine(LengthOfFile(path));
           //Console.WriteLine(ConvertToBinary(path).Length);
         Console.ReadLine();
       }
       public static byte[] ConvertToBinary(string Path)
       {
           FileStream stream = new FileInfo(Path).OpenRead();
           byte[] buffer = new byte[stream.Length];
           Console.WriteLine("The lenght of the file is"+buffer.Length);
           stream.Read(buffer, 0, Convert.ToInt32(stream.Length));
           return buffer;
       }
       /// <summary>
       /// 获取文件大小
       /// </summary>
       /// <paramname="path"></param>
       ///<returns></returns>
       public static  long LengthOfFile(stringpath)
       {
           FileInfo fi = new FileInfo(path);
           return fi.Length;
          
       }

       public static byte[] ConvertToBinary(long index,bool ifEnd,longfileLenght)
       {
           //20971520

           byte[] bysFile;//临时二进制数组,最大20M
           //index = buffer.Length / 20971520;
           //index += buffer.Length % 20971520 == 0 ? 0 : 1;
           //byte[] bys;//临时二进制数组,最大20M
           if (ifEnd == false)
           {
            
             bysFile = GetFileBloc(20971520 * index, 20971520);
            
           }
           else
           {

               bysFile = GetFileBloc(20971520 * index, fileLenght - 20971520 *(index));

           }
           Console.WriteLine("The length of the current buffer is " +bysFile.Length);
           return bysFile;
        
       }
       public static byte[] ChangeFileToByte(string path)
       {
           FileStream stream = new FileInfo(path).OpenRead();
           byte[] Filebuffer = new byte[stream.Length];
           stream.Read(Filebuffer, 0, Convert.ToInt32(stream.Length));
           return Filebuffer;
       }
       /// <summary>
       /// 获取文件二进制块
       /// </summary>
       /// <paramname="path"></param>
       /// <paramname="byteIndex"></param>
       /// <paramname="length"></param>
       ///<returns></returns>
       public static byte[] GetFileBloc( long byteIndex, longlength)
       {
           string path =System.Configuration.ConfigurationSettings.AppSettings["path"].ToString();
           FileStream stream = new FileInfo(path).OpenRead();
           stream.Position = byteIndex;
           byte[] Filebuffer = new byte[length];
           stream.Read(Filebuffer,0,Convert.ToInt32( length));
           return Filebuffer;
       }
    }
}
以下是webservice部分

[WebMethod(Description = "TransFile")]
    publicstring  TransFile(byte[] fileBt, stringfileName,bool ifCreate)
    {
       string rst = "0";
       if (fileBt.Length == 0)
           return rst ;
       string filePath =System.Configuration.ConfigurationManager.AppSettings["CSVPath"].ToString();  //文件路径

       //FileStream fstream = File.Create(filePath + fileName,fileBt.Length);
       //FileStream fstream = File.AppendAllText(
       FileStream fstream;
       if(ifCreate)
           fstream = new FileStream(filePath + fileName,FileMode.Create);
       else
           fstream = new FileStream(filePath + fileName, FileMode.Append);
       try
       {
           //MemoryStream m = new MemoryStream(fileBt);
           //m.WriteTo(fstream);
           fstream.Write(fileBt, 0,fileBt.Length);  //二进制转换成文件
         
        
           //rst += "\r\n";
        
           rst = fstream.Length.ToString() ;
       
           fstream.Close();
       }
       catch (Exception ex)
       {
           //抛出异常信息
           rst = ex.ToString();
       }
       finally
       {
          
           fstream.Close();
       }

       return rst;

    }

猜你喜欢

转载自xiaowei-qi-epro-com-cn.iteye.com/blog/2187370