C#上传文件到FTP服务器的问题,提示:应 PASV 命令的请求,服务器返回了一个与 FTP 连接地址不同的地址

ParisT 2015-02-06 03:16:42
先贴上传部分代码,如下
 /// 上传文件
/// </summary>
/// <param name="fileinfo">需要上传的文件</param>
/// <param name="targetDir">目标路径</param>
/// <param name="hostname">ftp地址</param>
/// <param name="username">ftp用户名</param>
/// <param name="password">ftp密码</param>
public string UploadFile(FileInfo fileinfo, string targetDir, string hostname, string username, string password)
{
//1. check target

string target;
if (targetDir.Trim() == "")
{
return "没有目标路径";
}

try
{
// 判断文件是否存在
if (ExistFile(fileinfo, targetDir, hostname, username, password))
{
DeleteFile(fileinfo, targetDir, hostname, username, password);
}
}
catch (Exception ex)
{
return ex.Message;
}

target = Guid.NewGuid().ToString(); //使用临时文件名

string URI = @targetDir + "/" + target;
///WebClient webcl = new WebClient();
System.Net.FtpWebRequest ftp = GetRequest(URI, username, password);

//设置FTP命令 设置所要执行的FTP命令,
//ftp.Method = System.Net.WebRequestMethods.Ftp.ListDirectoryDetails;//假设此处为显示指定路径下的文件列表
ftp.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
//指定文件传输的数据类型
ftp.UseBinary = true;
ftp.UsePassive = false;

//告诉ftp文件大小
ftp.ContentLength = fileinfo.Length;
//缓冲大小设置为2KB
const int BufferSize = 2048;
byte[] content = new byte[BufferSize - 1 + 1];
int dataRead;

//打开一个文件流 (System.IO.FileStream) 去读上传的文件
using (FileStream fs = fileinfo.OpenRead())
{
try
{
//把上传的文件写入流
using (Stream rs = ftp.GetRequestStream())
{
do
{
//每次读文件流的2KB
dataRead = fs.Read(content, 0, BufferSize);
rs.Write(content, 0, dataRead);
} while (!(dataRead < BufferSize));
rs.Close();
}

}
catch (Exception ex)
{
return ex.Message;
}
finally
{
fs.Close();
}

}

//设置FTP命令
ftp = null;
ftp = GetRequest(URI, username, password);
ftp.Method = System.Net.WebRequestMethods.Ftp.Rename; //改名
ftp.RenameTo = fileinfo.Name;
try
{
ftp.GetResponse();
}
catch (Exception ex)
{
ftp = GetRequest(URI, username, password);
ftp.Method = System.Net.WebRequestMethods.Ftp.DeleteFile; //删除
ftp.GetResponse();
throw ex;
}
finally
{
//fileinfo.Delete();
}

// 可以记录一个日志 "上传" + fileinfo.FullName + "上传到" + "FTP://" + hostname + "/" + targetDir + "/" + fileinfo.Name + "成功." );
ftp = null;

#region
/*****
*FtpWebResponse
* ****/
//FtpWebResponse ftpWebResponse = (FtpWebResponse)ftp.GetResponse();
#endregion

return "";
}





问题描述如下:
我的FTP服务器架设在一个专网上,同时也有一个内网,我在客户端通过专网访问时会提示:应 PASV 命令的请求,服务器返回了一个与 FTP 连接地址不同的地址
该如何解决呢?求各位大神帮忙!
...全文
331 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
tcmakebest 2015-02-06
  • 打赏
  • 举报
回复
返回的地址应该是有来历的, 可能是在FTP服务端配置的,找到设置的地方,改成合理的值.

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧