c#语言 ftp上传到linux上去
客户端是使用c#写的。
使用c#进行ftp上传,如果我是在windows上搭建ftp服务器的话,则
是可以上传成功的。
但是我在linux上是使用vsftp服务器的,但却无法上传成功。
c#程序发出ftp请求,但是无法上传成功。不知是为何?
是否要修改vsftp的一些配置。
我是使用root用户上传的,默认是传到root用户对应的home目录。
也即是说默认会上传到/root目录上,如果我不使用c#程序,而是直接
在windows上起一个命令行,然后建立ftp连接,使用put命令是可以传到linux上的。
但是一旦我使用c#的方式来上传文件的话,则上传不成功的。
c#代码如下:
===================================================================================
ftpServer = "192.168.75.128";
user = "root";
pwd = "root";
port = "21";
FtpTools client = new FtpTools(ftpServer, "/root/", user, pwd, int.Parse(conf.port));
client.Connect();//这一步是成功的
//调用下面的方法后,文件没有上传成功的
client.Put2("F:\\test.txt", "test.txt", 0);
public void Put2(string strFilePath,string strFileName, int offSet)
{
Uri uri = new Uri("ftp://" + strRemoteHost + strRemotePath + strFileName);
FtpWebRequest request = CreateFtpWebRequest(uri, WebRequestMethods.Ftp.UploadFile);
request.ContentOffset = offSet;
Stream requestStream = request.GetRequestStream(); //就是这句报错了
string path = strFilePath.Replace('\\', '/');
if (!File.Exists(path))
{
return;
}
FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);//创建存储文件的流
int sourceLength = (int)fileStream.Length;
offSet = CopyDataToDestination(fileStream, requestStream, offSet);
WebResponse response = request.GetResponse();
response.Close();
requestStream.Close();
fileStream.Close();
if (offSet != 0)
{
Put2(strFilePath,strFileName, offSet);
}
}
===================================================================================
错误信息如下:
--------------------------------------------------------------------
未处理的 WebException
远程服务器返回错误: (550) 文件不可用(例如,未找到文件,无法访问文件)。
---------------------------------------------------------------------
我自己想可能原因:
1、uri错了。
2、权限不足(应该不是这个原因,我已经修改了vsftp的相关配置,root是可以ftp上传文件的)
所以uri错误的可能是最大的原因,请高手赐教,我错在哪里了?