C#文件传输

本文关键字:传输 文件 | 更新日期: 2023-09-27 17:58:01

我有一个关于用C#语言发送文件和接收文件的方法的问题。我创建了一个简单的文件传输窗口表单应用程序,但它只支持.txt文件格式。如果我尝试发送一个图像文件或ms-words文档文件,它可以在接收端被完全接收。但是,收到的文件不可读,无法打开。我已经做了一个类似的应用程序Java,但它适用于任何文件格式,并且我在C#应用程序中使用相同的逻辑来实现。有人能给我一些建议吗?

SENDING SIDE:
// establish connection
                int port = Convert.ToInt32(txtLocalPort.Text) - 5;
                TcpListener listener = new TcpListener(IPAddress.Parse(txtLocalIP.Text), port);
                listener.Start();
                // get file size
                byte[] data = File.ReadAllBytes(downFile);
                int fSize = data.Length;
                // calculate block numbers, 1024 bytes each block
                int block = fSize / 1024;
                // leftover file bytes, less than 1024 bytes
                int byteLeft = fSize % 1024;
                // convert String to byte
                String cmd = "SEND_FILE" + fSize.ToString();
                buff = new byte[1024];
                buff = Encoding.ASCII.GetBytes(cmd);
                // send message in byte
                msgSocket.Send(buff);
                // accept connection
                TcpClient client = listener.AcceptTcpClient();
                // remote host connected
                if (client.Connected == true)
                {
                    // medium to read file bytes from file chosen
                    BinaryReader readByte = new BinaryReader(File.Open(downFile, FileMode.Open));
                    // medium to send file bytes
                    NetworkStream dataOUT = client.GetStream();
                    // send file bytes based on calculated block numbers
                    for (int i = 0; i < block; i++)
                    {
                        // buffer size
                        buff = new byte[1024];
                        // read file bytes from file chosen
                        readByte.Read(buff, 0, buff.Length);
                        // send file bytes
                        dataOUT.Write(buff, 0, buff.Length);
                        dataOUT.Flush();
                    }
                    // leftover file bytes
                    // buffer size
                    buffLeft = new byte[byteLeft];
                    // read leftover file bytes from file chosen
                    readByte.Read(buffLeft, 0, buffLeft.Length);
                    // send leftover file bytes
                    dataOUT.Write(buff, 0, buffLeft.Length);
                    dataOUT.Flush();
                    MessageBox.Show("File sent successfully.", "File Share", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    // close the medium
                    readByte.Close();
                    dataOUT.Close();
                    client.Close();
                    listener.Stop();

接收端:

// establish connection
        int port = Convert.ToInt32(txtRemotePort.Text) - 5;
        TcpClient client = new TcpClient(Dns.GetHostEntry(IPAddress.Parse(txtRemoteIP.Text)).HostName.ToString(), port);
        // connected to remote host
        if (client.Connected == true)
        {
            // get invalid character
            String invalid = new String(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());
            // remove invalid character
            foreach (char c in invalid)
            {
                fileName = fileName.Replace(c.ToString(), "");
            }
            String downFile = Path.Combine(@"D:'", fileName);
            // file size
            int fSize = fileSize;
            // calculate block numbers, 1024 bytes each block
            int block = fSize / 1024;
            // leftover file bytes, less than 1024 bytes
            int byteLeft = fSize % 1024;
            // medium to receive file bytes
            NetworkStream dataIN = client.GetStream();
            // medium to write file bytes to new file
            BinaryWriter writeByte = new BinaryWriter(File.Open(downFile, FileMode.Create));
            // receive file bytes based on calculated block numbers
            for (int i = 0; i < block; i++)
            {
                // buffer size
                buff = new byte[1024];
                // receive file bytes
                dataIN.Read(buff, 0, buff.Length);
                dataIN.Flush();
                // write file bytes to new file
                writeByte.Write(buff, 0, buff.Length);
                writeByte.Flush();
            }
            // receive leftover file bytes
            // buffer size
            buffLeft = new byte[byteLeft];
            // receive file bytes
            dataIN.Read(buffLeft, 0, buffLeft.Length);
            dataIN.Flush();
            // write file bytes to new file
            writeByte.Write(buffLeft, 0, buffLeft.Length);
            writeByte.Flush();
            MessageBox.Show("File downloaded successfully.", "File Share", MessageBoxButtons.OK, MessageBoxIcon.Information);
            // close the medium
            writeByte.Close();
            dataIN.Close();
            client.Close();

C#文件传输

我想我已经发现你做错了什么:而不是
BinaryWriter writeByte = new BinaryWriter(File.Open(downFile, FileMode.Create));

使用BinaryWriter writeByte = new BinaryWriter(File.Open(downFile, FileMode.Append));