首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

FTP文件上传和下载文件损坏有关问题

2012-05-23 
FTP文件上传和下载文件损坏问题最近抄了书上的一个FTP的例子,上传和下载是可以了,但是,上传后的文件和下载

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")                {                    //关闭TCP连接并释放与其相关的所有资源                    user.commandSession.close();                    return;                }                else                {                    switch (user.loginOK)                    {                             //等待用户输入用户名                        case 0:                            CommandUser(user, command, param);                            break;                            //等待用户输入密码                        case 1:                            CommandPassword(user, command, param);                            break;                            //用户名和密码正确时登录                        case 2:                            {                                switch (command)                                {                                    case "CWD":                                        CommandCWD(user, param);                                        break;                                    case "PWD":                                        CommandPWD(user);//显示工作目录                                        break;                                    case "PASV":                                        CommandPASV(user);                                        break;                                    case "PORT":                                        CommandPORT(user, param);                                        break;                                    case "LIST":                                    case "NLIST":                                        CommandLIST(user, param);//显示目录和文件                                        break;                                    case "RETR":                                        CommandRETR(user, param);                                        break;                                    case "STOR":                                        CommandSTOR(user, param);                                        break;                                    case "TYPE":                                        CommandTYPE(user, param);                                        break;                                    default:                                        sendString = "502 command is not implemented.";                                        ReplyCommandToUser(user, sendString);                                        break;                                }                            }                            break;                    }                }            }        }        /// <summary>        /// 处理USER命令,但不进行用户验证        /// </summary>        /// <param name="user"></param>        /// <param name="command"></param>        /// <param name="param"></param>        private void CommandUser(User user, string command, string param)        {            string sendString = string.Empty;            if (command == "USER")            {                sendString = "331 USER command OK,password required.";                user.userName = param;                user.loginOK = 1;//表示已接收到用户名,等待接收密码            }            else            {                sendString = "501 USER command syntax error.";            }            ReplyCommandToUser(user, sendString);        }        /// <summary>        /// 输入密码        /// </summary>        /// <param name="user"></param>        /// <param name="command"></param>        /// <param name="param"></param>        private void CommandPassword(User user, string command, string param)        {            string sendString = string.Empty;            if (command == "PASS")            {                string password = null;                if (users.TryGetValue(user.userName, out password))                {                    if (password == param)                    {                        sendString = "230 User logged in success";                        user.loginOK = 2; //2表示登录成功                    }                    else                        sendString = "530 Password incorrect.";                }                else                {                    sendString = "530 User name or password incorrect.";                }            }            else            {                sendString = "530 PASS command Syntax error.";            }            ReplyCommandToUser(user, sendString);            user.CurrentDir = user.WorkDir;        }        /// <summary>        /// 处理CWD命令,改变工作目录        /// </summary>        /// <param name="user"></param>        /// <param name="temp"></param>        private void CommandCWD(User user, string temp)        {            string sendString = string.Empty;            try            {                string dir = user.WorkDir.TrimEnd('/') + temp;                //是当前目录的子目录,且不包含父目录名称                if (Directory.Exists(dir))                {                    user.CurrentDir = dir;                    sendString = "250 Directory changed to '" + dir + "' successfully.";                }                else                {                    sendString = "550 Directory '" + dir + "' does not exist.";                }            }            catch            {                sendString = "502 Directory changed unsuccessfully.";            }            ReplyCommandToUser(user, sendString);        }       



[解决办法]
WebClient myWebClient = new WebClient();
byte[] responseArray = myWebClient.UploadFile(服务器路径,fileName);]

WebClient client = new WebClient();
client.Headers.Add(HttpRequestHeader.Referer, "");
client.DownloadFile("", "");
ftp 操作

热点排行