BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / www-technology / #11961同步于 2010/12/23
WWWTechnology机器人发帖

求助asp.net ftp下载的问题

realerge
2010/12/23镜像同步0 回复
我用asp.net实现了一个ftp上传下载功能,现在的问题是,在本机上调试的时候只要是非中文名小于400m的文件都可以顺利下载,但用IIS发布后就无法点击链接下载文件了,求原因,代码如下 public class FTPClient { private string ftpServerIP = String.Empty; private string ftpUser = String.Empty; private string ftpPassword = String.Empty; private string ftpRootURL = String.Empty; public FTPClient(/*string url, string userid, string password*/) { this.ftpServerIP = "59.64.138.149"; this.ftpUser = "root"; this.ftpPassword = "123456"; this.ftpRootURL = "ftp://" + "59.64.138.149" + "/"; } // <summary> // 上传 // </summary> // <param name="localFile">本地文件绝对路径</param> // <param name="ftpPath">上传到ftp的路径</param> // <param name="ftpFileName">上传到ftp的文件名</param> public bool fileCheckExist(string ftpPath, string ftpName) { bool success = false; FtpWebRequest ftpWebRequest = null; WebResponse webResponse = null; StreamReader reader = null; try { string url = ftpRootURL + ftpPath; ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(url)); ftpWebRequest.Credentials = new NetworkCredential(ftpUser, ftpPassword); ftpWebRequest.Method = WebRequestMethods.Ftp.ListDirectory; ftpWebRequest.KeepAlive = false; webResponse = ftpWebRequest.GetResponse(); reader = new StreamReader(webResponse.GetResponseStream()); string line = reader.ReadLine(); while (line != null) { if (line == ftpName) { success = true; break; } line = reader.ReadLine(); } } catch (Exception) { success = false; } finally { if (reader != null) { reader.Close(); } if (webResponse != null) { webResponse.Close(); } } return success; } public bool fileUpload(FileInfo localFile, string ftpPath, string ftpFileName) { bool success = false; FtpWebRequest ftpWebRequest = null; FileStream localFileStream = null; Stream requestStream = null; try { string uri = ftpRootURL + ftpPath + ftpFileName; ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri)); ftpWebRequest.Credentials = new NetworkCredential(ftpUser, ftpPassword); ftpWebRequest.UseBinary = true; ftpWebRequest.KeepAlive = false; ftpWebRequest.Method = WebRequestMethods.Ftp.UploadFile; ftpWebRequest.ContentLength = localFile.Length; int buffLength = 2048; byte[] buff = new byte[buffLength]; int contentLen; localFileStream = localFile.OpenRead(); requestStream = ftpWebRequest.GetRequestStream(); contentLen = localFileStream.Read(buff, 0, buffLength); while (contentLen != 0) { requestStream.Write(buff, 0, contentLen); contentLen = localFileStream.Read(buff, 0, buffLength); } success = true; } catch (Exception) { success = false; } finally { if (requestStream != null) { requestStream.Close(); } if (localFileStream != null) { localFileStream.Close(); } } return success; } // <summary> // 上传文件 // </summary> // <param name="localPath">本地文件地址(没有文件名)</param> // <param name="localFileName">本地文件名</param> // <param name="ftpPath">上传到ftp的路径</param> // <param name="ftpFileName">上传到ftp的文件名</param> public bool fileUpload(string localPath, string localFileName, string ftpPath, string ftpFileName) { bool success = false; try { FileInfo localFile = new FileInfo(localPath + localFileName); if (localFile.Exists) { success = fileUpload(localFile, ftpPath, ftpFileName); } else { success = false; } } catch (Exception) { success = false; } return success; } // <summary> // 下载文件 // </summary> // <param name="localPath">本地文件地址(没有文件名)</param> // <param name="localFileName">本地文件名</param> // <param name="ftpPath">下载的ftp的路径</param> // <param name="ftpFileName">下载的ftp的文件名</param> public bool fileDownload(string localPath, string localFileName, string ftpPath, string ftpFileName) { bool success = false; FtpWebRequest ftpWebRequest = null; FtpWebResponse ftpWebResponse = null; Stream ftpResponseStream = null; FileStream outputStream = null; try { outputStream = new FileStream(localPath + localFileName, FileMode.Create); string uri = ftpRootURL + ftpPath + ftpFileName; ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri)); ftpWebRequest.Credentials = new NetworkCredential(ftpUser, ftpPassword); ftpWebRequest.UseBinary = true; ftpWebRequest.Method = WebRequestMethods.Ftp.DownloadFile; ftpWebResponse = (FtpWebResponse)ftpWebRequest.GetResponse(); ftpResponseStream = ftpWebResponse.GetResponseStream(); long contentLength = ftpWebResponse.ContentLength; int bufferSize = 2048; byte[] buffer = new byte[bufferSize]; int readCount; readCount = ftpResponseStream.Read(buffer, 0, bufferSize); while (readCount > 0) { outputStream.Write(buffer, 0, readCount); readCount = ftpResponseStream.Read(buffer, 0, bufferSize); } success = true; } catch (Exception) { success = false; } finally { if (outputStream != null) { outputStream.Close(); } if (ftpResponseStream != null) { ftpResponseStream.Close(); } if (ftpWebResponse != null) { ftpWebResponse.Close(); } } return success; } // <summary> // 重命名 // </summary> // <param name="ftpPath">ftp文件路径</param> // <param name="currentFilename"></param> // <param name="newFilename"></param> public bool fileRename(string ftpPath, string currentFileName, string newFileName) { bool success = true; FtpWebRequest ftpWebRequest = null; FtpWebResponse ftpWebResponse = null; Stream ftpResponseStream = null; try { string uri = ftpRootURL + ftpPath + currentFileName; ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri)); ftpWebRequest.Credentials = new NetworkCredential(ftpUser, ftpPassword); ftpWebRequest.UseBinary = true; ftpWebRequest.Method = WebRequestMethods.Ftp.Rename; ftpWebRequest.RenameTo = newFileName; ftpWebResponse = (FtpWebResponse)ftpWebRequest.GetResponse(); ftpResponseStream = ftpWebResponse.GetResponseStream(); } catch (Exception) { success = false; } finally { if (ftpResponseStream != null) { ftpResponseStream.Close(); } if (ftpWebResponse != null) { ftpWebResponse.Close(); } } return success; } } protected void Bin_Button_Driv_Command(object sender, CommandEventArgs e)//点击linkbutton事件,将文件下载至E盘 { //TextBox text = new TextBox(); //Page.Controls.Add(text); //OpenFileDialog openDialog = new OpenFileDialog(); Response.Write(e.CommandArgument.ToString()); FTPClient ftp = new FTPClient(); if (ftp.fileCheckExist("/home/ftp/data-2/" + Session["Username"].ToString() + "/", e.CommandArgument.ToString()) == false) { Response.Write(e.CommandArgument.ToString()); return; } int i = 0; for (; i < 3; i++) if (true == ftp.fileDownload("E:\\", e.CommandArgument.ToString(), "/home/ftp/data-2/" + Session["Username"].ToString() + "/", e.CommandArgument.ToString())) { Response.Write("下载成功"); break; } if (i == 3) Response.Write("下载失败"); //ftp } 还有一点应该说的是,发布后ftp并非绝对不能用,因为在上面那个函数执行之前,还有一个下载数据库文件的过程,用的是同一个ftp函数,但是成功下载了。应该是能下到web服务器,但下不到访问web的机器上,求大牛指教[ema23]
订阅后,新回复会通过你的通知中心匿名送达。
0 条回复
暂无回复 · 你可以订阅本帖等待新回复。