日期:2014-05-18  浏览次数:20965 次

FTP文件上传和下载文件损坏问题
最近抄了书上的一个FTP的例子,上传和下载是可以了,但是,上传后的文件和下载下来的文件都不能打开,文本倒是可以打开,但是,汉字部分不显示,初学网络编程,希望大侠们指点一二。
例子如下
C# code

//服务端
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
using System.Globalization;

namespace ftpServer
{
    public partial class MainForm : Form
    {
        TcpListener myTcpListener;
        Dictionary<string, string> users;

        public MainForm()
        {
            InitializeComponent();

            users = new Dictionary<string, string>();
            users.Add("mytestName", "123456");
            txbDir.Text = "e:/ls/";
        }

        //启动服务
        private void btnFTPStart_Click(object sender, EventArgs e)
        {
            listBox1.Items.Add("FTP服务已启动");
            Thread t = new Thread(ListenClientConnect);
            t.Start();
            btnFTPStart.Enabled = false;
            btnStop.Enabled = true;
        }
        /// <summary>
        /// 监听端口,处理客户端连接
        /// </summary>
        private void ListenClientConnect()
        {
            myTcpListener = new TcpListener(IPAddress.Any, 21);
            myTcpListener.Start();
            while (true)
            {
                try
                {
                    TcpClient client = myTcpListener.AcceptTcpClient();
                    AddInfo(string.Format("{0}和本机{1}建立FTP连接", 
                        client.Client.RemoteEndPoint, myTcpListener.LocalEndpoint));
                    User user = new User();
                    user.commandSession = new UserSession(client);
                   
                    user.WorkDir = txbDir.Text;
                    Thread t = new Thread(userProcessing);
                    t.IsBackground = true;
                    t.Start(user);
                }
                catch
                { break; }
            }
        }
        /// <summary>
        /// 客户连接
        /// </summary>
        /// <param name="user"></param>
        private void userProcessing(object obj)
        {
            User user = (User)obj;
            string sendString = "220 FTP Server V1.0";
            string oldFileName = "";
            ReplyCommandToUser(user, sendString);
            while (true)
            {
                string ReceiveString = null;
                try
                {
                    ReceiveString = user.commandSession.sr.ReadLine();
                }
                catch 
                {
                    if (user.commandSession.client.Connected == false)
                    {
                        AddInfo("客户端断开连接");
                    }
                    else
                    {
                        AddInfo("命令接收失败");
                    }
                    break;
                }
                if (ReceiveString == null)
                {
                    AddInfo("接收字符为null,线程结束");
                    break;
                }

                AddInfo(string.Format("来自[{0}]--{1}", user.commandSession.client.Client.RemoteEndPoint, ReceiveString));

                //分解客户端发过来的控制信息中的命令及参数
                string command = ReceiveString;
                string param = string.Empty;
                int index = ReceiveString.IndexOf(' ');
                if (index != -1)
                {
                    command = ReceiveString.Substring(0, index).ToUpper();
                    param = ReceiveString.Substring(command.Length).Trim();
                }
                //处理不需要登录即可响应的命令(此处仅处理QUIT)
                if (command == "QUIT")