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

看看下面的方法,哪些需要改进?
C# code
    /// <summary>
    /// 将本地的文件上传到FTP服务器
    /// </summary>
    /// <param name="strServer">服务器地址</param>
    /// <param name="strFile">需要上传的文件</param>
    /// <param name="strFolder">文件夹(如 myfolder/)</param>
    /// <param name="strNewName">新的文件名,如果为空则使用文件原来的名称</param>
    /// <param name="strUserName">账号</param>
    /// <param name="strPassword">密码</param>
    /// <returns></returns>
    public static bool UPLoadFile(string strServer, string strFile, string strFolder, string strNewName, string strUserName, string strPassword)
    {
        bool realOK = true;
        strNewName = GetFileName(strFile, strNewName);
        FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + strServer + "/" + strFolder + strNewName));
        request.Credentials = new NetworkCredential(strUserName, strPassword);
        request.Method = WebRequestMethods.Ftp.UploadFile;
        request.UseBinary = true;
        Stream stream = null;
        FileStream filestream = new FileStream(strFile, FileMode.Open);
        request.ContentLength = filestream.Length;
        // 缓冲大小设置为2kb
        const int bufflength = 2048;
        byte[] buff = new byte[bufflength];
        // 打开一个文件流 (System.IO.FileStream) 去读上传的文件
        try
        {
            // 把上传的文件写入流
            stream = request.GetRequestStream();

            // 每次读文件流的2kb
            int contentLen = filestream.Read(buff, 0, bufflength);
            // 流内容没有结束
            while (contentLen > 0)
            {
                stream.Write(buff, 0, contentLen);
                contentLen = filestream.Read(buff, 0, bufflength);
            }
        }
        catch
        {
            realOK = false;
        }

        finally
        {
            if (request != null)
            {
                request.Abort();
            }

            if (filestream != null)
            {
                filestream.Close();
            }
            if (stream != null)
            {
                stream.Close();
            }
        }
        return realOK;
    }

    /// <summary>
    ///传入图片地址得到图片并且保存到磁盘 
    /// </summary>
    /// <param name="strFile">图片地址</param>
    /// <param name="strFolder">文件夹地址</param>
    /// <param name="strNewName">传入文件名</param>
    /// <param name="errorstatus">返回错误编码</param>
    public static void GetURLToSave(string strFile, string strFolder, string strNewName,out ErrorInformation errorstatus)
    {
        HttpWebRequest request = null;
        WebResponse response = null;
        Stream stream = null;
        FileStream filestream = null;
        errorstatus = ErrorInformation.GetOK;
        try
        {
            request = (HttpWebRequest)WebRequest.Create(strFile);
            request.UserAgent = "Mozilla/5.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1;.NET CLR 2.0.50727)";
            request.AllowAutoRedirect = false;
            strNewName = GetFileName(strFile, strNewName);
            response = request.GetResponse();
            stream = response.GetResponseStream();
            string strsss = strFolder + "\\" + strNewName;
            filestream = new FileStream(strFolder + "\\" + strNewName, FileMode.Create, FileAccess.Write);
            byte[] bt = new byte[1024];
            int index;
            do
            {
                index = stream.Read(bt, 0, bt.Length);
                if (index > 0)
                {
                    filestream.Write(bt, 0, index);
                }
            }
            while (index > 0);
        }
        catch (WebExc