看看下面的方法,哪些需要改进?
/// <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 (WebException ex) { if (ex.Message.IndexOf("404")>=0) { errorstatus = ErrorInformation.NotFindError; } else if (ex.Message.IndexOf("500")>=0) { errorstatus = ErrorInformation.InternalError; } else { errorstatus = ErrorInformation.Othererror; } } finally { if (request != null) { request.Abort(); } if (response != null) { response.Close(); } if (filestream != null) { filestream.Close(); } if (stream != null) { stream.Close(); } } } /// <summary> ///远程登陆 /// </summary> /// <param name="strIP">传入IP地址</param> ///<param name="strUserName">账号(默认账号:administrator)</param> /// <param name="strPassword">密码</param> /// <returns></returns> public static bool LoginRemote(string strIP, string strUserName, string strPassword) { try { if (string.IsNullOrEmpty(strUserName)) { strUserName = "administrator"; } Process process = new Process(); process.StartInfo.FileName = "cmd.exe"; // 这里是关键点,不用Shell启动/重定向输入/重定向输出/不显示窗口 process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardInput = true; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.CreateNoWindow = true; process.Start(); process.StandardInput.WriteLine(@"net use \\" + strIP + " " + strPassword + " /user:" + strUserName + ""); process.StandardInput.WriteLine("exit"); process.WaitForExit(); process.Close(); return true; } catch { return false; } } private static string GetFileName(string strFile, string strNewName) { string strFileFormat = strFile.Substring(strFile.LastIndexOf(".")); if (!strFile.ToLower().StartsWith("http")) { if (strNewName.Length == 0) { strNewName = strFile.Substring(strFile.LastIndexOf("\\") + 1); } else { strNewName += strFileFormat; } } else { if (strNewName.Length == 0) { strNewName = strFile.Substring(strFile.LastIndexOf("/") + 1); } else { strNewName += strFileFormat; } } return strNewName; } /// <summary> /// 自定义错误信息 /// </summary> public enum ErrorInformation { /// <summary> ///执行成功 /// </summary> GetOK = 0, /// <summary> /// 图片不存在 /// </summary> NotFindError = 404, /// <summary> /// 服务器内部错误 /// </summary> InternalError = 500, Othererror, }//弱弱的问一句:如果我需要批量上传5张图片,那么我是不是得连接5次ftp服务器啊???还有如何把远程的图片传送到ftp服务器?我这里是先保存到本地然后上传。。。。