缺少文件的最后字节

本文关键字:最后 字节 文件 | 更新日期: 2023-09-27 18:34:18

我是pograming的新手,我有一个无法解决的问题。我创建了一个共享文件的程序,例如保管箱。 我有一个服务器,工作正常,还有一个客户端。问题是,当我尝试从服务器下载文件时,流中似乎缺少一些字节。

clientSocket = new TcpClient(Program.host, Program.port);
NetworkStream networkStream = clientSocket.GetStream();
StreamWriter write = new StreamWriter(networkStream);
write.WriteLine(need);
write.Flush();
write.WriteLine(loca);
write.Flush();
StreamReader read = new StreamReader(networkStream);
Int64 size = Convert.ToInt64(read.ReadLine());
long blocks = size / 1024;
long blocksDown = 0;
try
{
    string fileName = string.Empty;
    int qtd = 0;
    int blockSize = 1024;
    double progress = 0;
    Int64 qtdDown = 0;
    Byte[] dataByte = new Byte[blockSize];
    bool download = true;
    lock (this)
    {
        networkStream.Read(dataByte, 0, dataByte.Length);
        int fileNameLen = BitConverter.ToInt32(dataByte, 0);
        fileName = Encoding.ASCII.GetString(dataByte, 4, fileNameLen);
        if (File.Exists(Program.loc + fileName))
            switch (MessageBox.Show("Ficheiro já existente!'r'nDeseja Substituir?", "Aviso", MessageBoxButtons.YesNo,
                MessageBoxIcon.Warning))
            {
                case DialogResult.Yes:
                    File.Delete(Program.loc + fileName);
                    download = true;
                    break;
                case DialogResult.No:
                    download = false;
                    break;
            }
        if (download)
        {
            Stream fileStream = File.OpenWrite(Program.loc + fileName);
            fileStream.Write(dataByte, 4 + fileNameLen, (1024 - (4 + fileNameLen)));
            bool progChanged = false;
            backWork.WorkerReportsProgress = true;
            double progAnt = 0;
            while (true)
            {
                qtd = networkStream.Read(dataByte, 0, blockSize);
                qtdDown += qtd;
                progress = (double)qtdDown * (double)100 / (double)size;
                if (Math.Round(progress) > Math.Round(progAnt))
                {
                    progChanged = true;
                    progAnt = progress;
                }
                if (Math.Round(progress) > 0 && progChanged)
                {
                    backWork.ReportProgress(((int)Math.Round(progress)));
                    progChanged = false;
                }
                fileStream.Write(dataByte, 0, qtd);
                fileStream.Flush();
                blocksDown++;
                if (qtd == 0)
                {
                    backWork.ReportProgress(100);
                    qtd = networkStream.Read(dataByte, 0, blockSize);
                    networkStream.Close();
                    fileStream.Close();
                    break;
                }
            }
            MessageBox.Show("O ficheiro " + fileName + " foi guardado em " + Program.loc + ".", "Download",
                MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}

这就是我正在做的。我正在后台工作人员上进行下载。首先,我在流上写下我正在执行的操作,然后打开文件的流,然后开始读取流并写入文件。


@Ňuf 我试过你说的,但仍然不起作用代码如下所示,来自 qtdDown大小的字节不相等。抱歉,如果葡萄牙语的某些部分,我的母语是葡萄牙语。

    btnAct.Enabled = false;
    btnBro.Enabled = false;
    btnDown.Enabled = false;
    btnSend.Enabled = false;
    menuStrip1.Enabled = false;
    pBar.Value = 0;
    clientSocket = new TcpClient(Program.host, Program.port);
    NetworkStream networkStream = clientSocket.GetStream();
    StreamWriter write = new StreamWriter(networkStream);
    write.WriteLine(need);
    write.Flush();
    write.WriteLine(loca);
    write.Flush();
    int blockSize = 1024;
    byte[] fileSizeBytes = new byte[blockSize];
    networkStream.Read(fileSizeBytes, 0, blockSize);
    string fileSizeString = Encoding.ASCII.GetString(fileSizeBytes, 0, fileSizeBytes.Length);
    fileSize = Convert.ToInt64(fileSizeString);
    qtdDown = 0;
    try
    {
        string fileName = string.Empty;
        int qtd = 0;
        double progress = 0;
        Byte[] dataByte = new Byte[blockSize];
        bool download = true;
        lock (this)
        {
            networkStream.Read(dataByte, 0, dataByte.Length);
            int fileNameLen = BitConverter.ToInt32(dataByte, 0);
            fileName = Encoding.ASCII.GetString(dataByte, 4, fileNameLen);
            if (File.Exists(Program.loc + fileName))
                switch (MessageBox.Show("Ficheiro já existente!'r'nDeseja Substituir?", "Aviso", MessageBoxButtons.YesNo,
                    MessageBoxIcon.Warning))
                {
                    case DialogResult.Yes:
                        File.Delete(Program.loc + fileName);
                        download = true;
                        break;
                    case DialogResult.No:
                        download = false;
                        break;
                }
            if (download)
            {
                Stream fileStream = File.OpenWrite(Program.loc + fileName);
                fileStream.Write(dataByte, 4 + fileNameLen, (1024 - (4 + fileNameLen)));
                networkStream.ReadTimeout = 60;
                backWork.WorkerReportsProgress = true;
                double progAnt = 0;
                while (true)
                {
                    qtd = networkStream.Read(dataByte, 0, blockSize);
                    qtdDown += qtd;
                    progress = (double)qtdDown * (double)100 / (double)fileSize;
                    if (Math.Round(progress) > Math.Round(progAnt))
                    {
                        progAnt = progress;
                        backWork.ReportProgress(((int)Math.Round(progress)));
                    }
                    fileStream.Write(dataByte, 0, qtd);
                    if (qtdDown == fileSize)
                    {
                        backWork.ReportProgress(100);
                        fileStream.Close();
                        networkStream.Close();
                        break;
                    }
                }
                MessageBox.Show("O ficheiro " + fileName + " foi guardado em " + Program.loc + ".", "Download",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
    catch (Exception ex)
    {
        menuStrip1.Enabled = true;
        btnAct.Enabled = true;
        btnBro.Enabled = true;
        btnDown.Enabled = true;
        btnSend.Enabled = true;
        MessageBox.Show("Ocorreu um erro!" + "'r'nBytes Downloaded: "
            + qtdDown.ToString() + "''" + fileSize.ToString() + "'r'nDetalhes do Erro: " + ex.Message, "Download",
            MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    menuStrip1.Enabled = true;
    btnAct.Enabled = true;
    btnBro.Enabled = true;
    btnDown.Enabled = true;
    btnSend.Enabled = true;

缺少文件的最后字节

您通过调用 read 丢失了字节。ReadLine()。尽管此函数仅从一行返回数据,但它在内部将更多数据从网络流读取到内部缓冲区,并保留剩余数据以供后续读取。Read() 调用,这在你的代码中永远不会发生。

使用 StreamReader 读取数据或直接从 NetworkStrem 读取数据,但不要将两者的读数混合在一起。

我在最后一个if中添加了这一行,* ||(文件大小 - qtdDown) <1024* 并且文件现在可以工作,我不知道最后的字节是否重要,但文件现在可以工作了。