C# 无法连接到 FTP: (553) 文件名不允许
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9418404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't connect to FTP: (553) File name not allowed
提问by NomadicDeveloper
I need to FTP a file to a directory. In .Net I have to use a file on the destination folder to create a connection so I manually put Blank.dat on the server using FTP. I checked the access (ls -l) and it is -rw-r--r--. But when I attempt to connect to the FTP folder I get: "The remote server returned an error: (553) File name not allowed" back from the server. The research I have done says that this may arrise from a permissions issue but as I have said I have permissions to view the file and can run ls from the folder. What other reasons could cause this issue and is there a way to connect to the folder without having to specify a file?
我需要将文件通过 FTP 传输到目录。在 .Net 中,我必须使用目标文件夹上的文件来创建连接,因此我使用 FTP 手动将 Blank.dat 放在服务器上。我检查了访问权限(ls -l),它是 -rw-r--r--。但是,当我尝试连接到 FTP 文件夹时,我从服务器返回:“远程服务器返回错误:(553) 文件名不允许”。我所做的研究表明,这可能是由权限问题引起的,但正如我所说,我有权查看该文件并且可以从该文件夹运行 ls。还有哪些其他原因可能导致此问题,有没有办法无需指定文件即可连接到文件夹?
byte[] buffer;
Stream reqStream;
FileStream stream;
FtpWebResponse response;
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(new Uri(string.Format("ftp://{0}/{1}", SRV, DIR)));
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(UID, PASS);
request.UseBinary = true;
request.Timeout = 60000 * 2;
for (int fl = 0; fl < files.Length; fl++)
{
request.KeepAlive = (files.Length != fl);
stream = File.OpenRead(Path.Combine(dir, files[fl]));
reqStream = request.GetRequestStream();
buffer = new byte[4096 * 2];
int nRead = 0;
while ((nRead = stream.Read(buffer, 0, buffer.Length)) != 0)
{
reqStream.Write(buffer, 0, nRead);
}
stream.Close();
reqStream.Close();
response = (FtpWebResponse)request.GetResponse();
response.Close();
}
回答by Fen
I saw something similar to this a while back, it turned out to be the fact that I was trying to connect to an internal iis ftp server that was secured using Active Directory.
不久前我看到了类似的东西,事实证明是我试图连接到使用 Active Directory 保护的内部 iis ftp 服务器。
In my network credentials I was using new NetworkCredential(@"domain\user", "password"); and that was failing. Switching to new NetworkCredential("user", "password", "domain"); worked for me.
在我的网络凭据中,我使用的是 new NetworkCredential(@"domain\user", "password"); 那是失败的。切换到新的 NetworkCredential("user", "password", "domain"); 为我工作。
回答by user1131926
Although replying to an old post just thought it might help someone.
虽然回复旧帖子只是认为它可能对某人有所帮助。
When you create your ftp url make sure you are not including the default directory for that login.
当您创建 ftp url 时,请确保您没有包含该登录的默认目录。
for example this was the path which I was specifying and i was getting the exception 553 FileName not allowed exception
例如,这是我指定的路径,我收到异常 553 FileName not allowed 异常
ftp://myftpip/gold/central_p2/inbound/article_list/jobs/abc.txt
The login which i used had the default directory gold/central_p2.so the supplied url became invalid as it was trying to locate the whole path in the default directory.I amended my url string accordingly and was able to get rid of the exception.
我使用的登录具有默认目录 gold/central_p2.so 提供的 url 变得无效,因为它试图在默认目录中找到整个路径。我相应地修改了我的 url 字符串并能够摆脱异常。
my amended url looked like
我修改后的网址看起来像
ftp://myftpip/inbound/article_list/jobs/abc.txt
Thanks,
谢谢,
Sab
萨布
回答by Darth Jurassic
This may help for Linux FTP server.
这可能对 Linux FTP 服务器有所帮助。
So, Linux FTP servers unlike IIS don't have common FTP root directory. Instead, when you log on to FTP server under some user's credentials, this user's root directory is used. So FTP directory hierarchy starts from /root/ for root user and from /home/username for others.
因此,Linux FTP 服务器与 IIS 不同,没有通用的 FTP 根目录。相反,当您使用某些用户的凭据登录到 FTP 服务器时,将使用该用户的根目录。所以 FTP 目录层次结构从 /root/ 开始,对于 root 用户,从 /home/username 开始。
So, if you need to query a file not relative to user account home directory, but relative to file system root, add an extra / after server name. Resulting URL will look like:
因此,如果您需要查询的文件不是与用户帐户主目录相关,而是与文件系统根目录相关,请在服务器名称后添加额外的 /。生成的 URL 将如下所示:
ftp://servername.net//var/lalala
回答by RRM
You must be careful with names and paths:
您必须小心名称和路径:
string FTP_Server = @"ftp://ftp.computersoft.com//JohnSmith/";
string myFile="text1.txt";
string myDir=@"D:/Texts/Temp/";
if you are sending to ftp.computersoft.com/JohnSmitha file caled text1.txtlocated at d:/texts/temp
如果您要发送到ftp.computersoft.com/JohnSmith文件caled text1.txt位于d:/文本/ TEMP
then
然后
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(FTP_Server+myFile);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(FTP_User, FTP_Password);
StreamReader sourceStream = new StreamReader(TempDir+myFile);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
notice that at one moment you use as destination
请注意,在某一时刻您用作目的地
ftp://ftp.computersoft.com//JohnSmith/text1.txt
ftp://ftp.computersoft.com//JohnSmith/text1.txt
which contains not only directory but the new file name at FTP server as well (which in general can be different than the name of file on you hard drive)
它不仅包含目录,还包含 FTP 服务器上的新文件名(通常可以与硬盘驱动器上的文件名不同)
and at other place you use as source
并在其他地方用作来源
D:/Texts/Temp/text1.txt
D:/Texts/Temp/text1.txt
回答by aliastitan
your directory has access limit.
delete your directory and then create again with this code:
您的目录有访问限制。
删除您的目录,然后使用以下代码再次创建:
//create folder
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create("ftp://mpy1.vvs.ir/Subs/sub2");
request.Method = WebRequestMethods.Ftp.MakeDirectory;
request.Credentials = new NetworkCredential(username, password);
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = true ;
using (var resp = (FtpWebResponse)request.GetResponse())
{
}
回答by Bibaswann Bandyopadhyay
Although it's an older post I thought it would be good to share my experience. I came faced the same problem however I solved it myself. The main 2 reasons of this problem is Error in path (if your permissions are correct) happens when your ftp path is wrong. Without seeing your path it is impossible to say what's wrong but one must remember the things
a. Unlike browser FTP doesn't accept some special characters like ~
b. If you have several user accounts under same IP, do not include username or the word "home" in path
c. Don't forget to include "public_html" in the path (normally you need to access the contents of public_html only) otherwise you may end up in a bottomless pit
虽然这是一篇较旧的帖子,但我认为分享我的经验会很好。我遇到了同样的问题,但我自己解决了。此问题的主要原因有两个原因是路径错误(如果您的权限正确)在您的 ftp 路径错误时发生。没有看到你的道路就不可能说出什么是错的,但人们必须记住这些事情
a. Unlike browser FTP doesn't accept some special characters like ~
b. If you have several user accounts under same IP, do not include username or the word "home" in path
c. Don't forget to include "public_html" in the path (normally you need to access the contents of public_html only) otherwise you may end up in a bottomless pit
回答by Evan Barke
Another reason for this error could be that the FTP server is case sensitive. That took me good while to figure out.
此错误的另一个原因可能是 FTP 服务器区分大小写。我花了很长时间才弄清楚。
回答by CAK2
I had this problem when I tried to write to the FTP site's root directory pro grammatically. When I repeated the operation manually, the FTP automatically rerouted me to a sub-directory and completed the write operation. I then changed my code to prefix the target filename with the sub-directory and the operation was successful.
当我尝试以编程方式写入 FTP 站点的根目录时,我遇到了这个问题。当我手动重复操作时,FTP自动将我重新路由到一个子目录并完成写入操作。然后我更改了我的代码以使用子目录作为目标文件名的前缀,并且操作成功。
回答by Znaneswar
I hope this will be helpful for someone
我希望这对某人有帮助
if you are using LINUX server, replace your request path from
如果您使用的是 LINUX 服务器,请将您的请求路径从
FtpWebRequest req= (FtpWebRequest)WebRequest.Create(@"ftp://yourdomain.com//yourpath/" + filename);
to
到
FtpWebRequest req= (FtpWebRequest)WebRequest.Create(@"ftp://yourdomain.com//public_html/folderpath/" + filename);
the folder path is how you see in the server(ex: cpanel)
文件夹路径是您在服务器中看到的方式(例如:cpanel)
回答by Jeffrey Roy
Mine was as simple as a file name collision. A previous file hadn't been sent to an archive folder so we tried to send it again. Got the 553 because it wouldn't overwrite the existing file.
我的就像文件名冲突一样简单。以前的文件尚未发送到存档文件夹,因此我们尝试再次发送。得到 553,因为它不会覆盖现有文件。

