我创建了一个小型 Windows 窗体应用程序,用于将文件上传到我们客户的 ftp 站点之一。但我遇到的问题是,当我在本地计算机上运行此应用程序时,它会成功上传文件。但是如果我在我们的服务器上运行这个程序,我会收到此错误消息;
远程服务器返回错误:(550) 文件不可用(例如,未找到文件,无法访问文件),在此行“objFTPRequest.GetRequestStream();”。
有人知道为什么吗?我需要配置防火墙什么的吗?这是我的代码;
FileInfo objFile = new FileInfo(filename);
FtpWebRequest objFTPRequest;
// Create FtpWebRequest object
objFTPRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/outbox/" + objFile.Name));
// Set Credintials
objFTPRequest.Credentials = new NetworkCredential(ftpUserName, ftpPassword);
// By default KeepAlive is true, where the control connection is
// not closed after a command is executed.
objFTPRequest.KeepAlive = false;
// Set the data transfer type.
objFTPRequest.UseBinary = true;
// Set content length
objFTPRequest.ContentLength = objFile.Length;
// Set request method
objFTPRequest.Method = WebRequestMethods.Ftp.UploadFile;
// Set buffer size
int intBufferLength = 16 * 1024;
byte[] objBuffer = new byte[intBufferLength];
// Opens a file to read
FileStream objFileStream = objFile.OpenRead();
// Get Stream of the file
Stream objStream = objFTPRequest.GetRequestStream();
int len = 0;
while ((len = objFileStream.Read(objBuffer, 0, intBufferLength)) != 0)
{
// Write file Content
objStream.Write(objBuffer, 0, len);
}
objStream.Close();
objFileStream.Close();
导致此错误的原因有多种,例如服务器上不存在文件、文件的安全权限等。
首先你需要找出错误的确切原因。
这可以通过使用以下代码来实现 -
try
{
//Your code
}
catch(WebException e)
{
String status = ((FtpWebResponse)e.Response).StatusDescription;
}
一旦获得错误的确切原因,您就可以继续解决它。
以下是一些您可以参考的链接
http://forums.asp.net/t/1777881.aspx/1 http://forums.asp.net/t/1777881.aspx/1
http://nickstips.wordpress.com/2010/10/25/c-ftp-upload-error-the-remote-server-returned-an-error-550-file-unavailable-e-g-file-not-found-无法访问/ http://nickstips.wordpress.com/2010/10/25/c-ftp-upload-error-the-remote-server-returned-an-error-550-file-unavailable-e-g-file-not-found-no-access/
http://www.dreamincode.net/forums/topic/76361-file-upload-to-server/ http://www.dreamincode.net/forums/topic/76361-file-upload-to-server/
http://forums.asp.net/t/1374306.aspx/1 http://forums.asp.net/t/1374306.aspx/1
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)