通过WebService ASMX传输字节阵列

发布于 2025-02-08 21:10:33 字数 3289 浏览 1 评论 0原文

我正在研究用于在服务器之间传输ZIP文件的旧网络服务,直到最近,文件大小变大,超过700 MB。这会导致例外,因为我的控制台应用程序(客户端)无法一次接收大型字节阵列,因此我试图通过块发送和接收字节数组,但是zip文件最终被损坏,无法解压缩。 ,下面是我尝试的,我对WebService有点不熟悉,因此,如果你们可以指出我的错误,或者也许我将我指向更好的解决方案:

Console App(Original):

 string saveto = MdlMain.FileName;
                object file = (object)serviceSoapClient.GetFile( saveto);
                FileStream fileStream = new FileStream(GlobalVariable.FileDirectory + str2, FileMode.Create);
                fileStream.Write((byte[])file, 0, Conversions.ToInteger(NewLateBinding.LateGet(file, (Type)null, "Length", new object[0], (string[])null, (Type[])null, (bool[])null)));
                fileStream.Flush();
                fileStream.Close();

WebService(Original):

        public byte[] GetFile( string strFilename)
    {
        FileStream fileStream = new FileStream(this.DJDownloadPath + strFilename, FileMode.Open, FileAccess.Read);
        long length = fileStream.Length;
        byte[] array = new byte[checked((int)(length + 1L - 1L) + 1)];

        fileStream.Read(array, 0, checked((int)length));
        fileStream.Close();
        HttpContext.Current.Response.BufferOutput = false;
        HttpContext.Current.Response.Buffer = false;

        return array;

    }

Console App(Modified ) ):

 FileStream fileStream= new FileStream(@"D:\example.zip", FileMode.Create);

                    int totalchunkNo = targetfilesize / 2000000;
                    int remainder = targetfilesize % 2000000;

                    if (remainder > 0 && remainder != totalchunkNo)
                        totalchunkNo++;

                    for (int i = 1; i <= totalchunkNo; i++)
                    {

                        byte[] bytetobeWritten = (byte[])serviceSoapClient.GetFileByChunk(MdlMain.FileName, i);

                        fileStream.Write(bytetobeWritten, 0, Conversions.ToInteger(NewLateBinding.LateGet(bytetobeWritten, (Type)null, "Length", new object[0], (string[])null, (Type[])null, (bool[])null)));
                       
                    }

                    fileStream.Flush();
                    fileStream.Close();

WebService(修改):

[WebMethod]
    public byte[] GetFileByChunk(string strFilename, int requestChunkNo)
    {
        FileStream fileStream = new FileStream(this.DJDownloadPath + strFilename, FileMode.Open, FileAccess.Read);

        long length = fileStream.Length;
        byte[] array = new byte[length];
        int incomingOffset = 0;
        int chunkSize = 2000000;

        fileStream.Read(array, 0, (int)length);
        fileStream.Close();

        int currentchunkNo = 0;
        byte[] outboundBuffer = new byte[chunkSize];

        while (incomingOffset < array.Length)
        {
            int lengthh = Math.Min(outboundBuffer.Length, array.Length - incomingOffset);

            Buffer.BlockCopy(array, incomingOffset,
                             outboundBuffer, 0,
                             lengthh);

            incomingOffset += lengthh;

            currentchunkNo++;

            if (currentchunkNo == requestChunkNo)
            {
                return outboundBuffer;
            }
        }

        return null;
    }

I am working on a legacy webservice that are used to transfer zip files between server, it works fine until recently where the size of files become larger, more than 700 mb. This causes exceptions as my console application (client) are not able to receive large byte array send by the webservice at once, so i tried to send and receive the byte array by chunk, but the zip file ended up being corrupt and cannot be unzipped, below is what i have tried, i am a bit unfamiliar with webservice so would appreciate if you guys can point out my mistake or maybe point me to a better solution :

console app(original) :

 string saveto = MdlMain.FileName;
                object file = (object)serviceSoapClient.GetFile( saveto);
                FileStream fileStream = new FileStream(GlobalVariable.FileDirectory + str2, FileMode.Create);
                fileStream.Write((byte[])file, 0, Conversions.ToInteger(NewLateBinding.LateGet(file, (Type)null, "Length", new object[0], (string[])null, (Type[])null, (bool[])null)));
                fileStream.Flush();
                fileStream.Close();

webservice (original) :

        public byte[] GetFile( string strFilename)
    {
        FileStream fileStream = new FileStream(this.DJDownloadPath + strFilename, FileMode.Open, FileAccess.Read);
        long length = fileStream.Length;
        byte[] array = new byte[checked((int)(length + 1L - 1L) + 1)];

        fileStream.Read(array, 0, checked((int)length));
        fileStream.Close();
        HttpContext.Current.Response.BufferOutput = false;
        HttpContext.Current.Response.Buffer = false;

        return array;

    }

Console app (Modified):

 FileStream fileStream= new FileStream(@"D:\example.zip", FileMode.Create);

                    int totalchunkNo = targetfilesize / 2000000;
                    int remainder = targetfilesize % 2000000;

                    if (remainder > 0 && remainder != totalchunkNo)
                        totalchunkNo++;

                    for (int i = 1; i <= totalchunkNo; i++)
                    {

                        byte[] bytetobeWritten = (byte[])serviceSoapClient.GetFileByChunk(MdlMain.FileName, i);

                        fileStream.Write(bytetobeWritten, 0, Conversions.ToInteger(NewLateBinding.LateGet(bytetobeWritten, (Type)null, "Length", new object[0], (string[])null, (Type[])null, (bool[])null)));
                       
                    }

                    fileStream.Flush();
                    fileStream.Close();

webservice (Modified):

[WebMethod]
    public byte[] GetFileByChunk(string strFilename, int requestChunkNo)
    {
        FileStream fileStream = new FileStream(this.DJDownloadPath + strFilename, FileMode.Open, FileAccess.Read);

        long length = fileStream.Length;
        byte[] array = new byte[length];
        int incomingOffset = 0;
        int chunkSize = 2000000;

        fileStream.Read(array, 0, (int)length);
        fileStream.Close();

        int currentchunkNo = 0;
        byte[] outboundBuffer = new byte[chunkSize];

        while (incomingOffset < array.Length)
        {
            int lengthh = Math.Min(outboundBuffer.Length, array.Length - incomingOffset);

            Buffer.BlockCopy(array, incomingOffset,
                             outboundBuffer, 0,
                             lengthh);

            incomingOffset += lengthh;

            currentchunkNo++;

            if (currentchunkNo == requestChunkNo)
            {
                return outboundBuffer;
            }
        }

        return null;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文