我正在试着测试我的.NET应用程序通过FTP协议上传文件的功能。此函数使用.NET的内置FtpWebResponse类(如果用户的服务器不支持SSH连接)。我使用以下代码尝试在我的用户目录中的Ubuntu服务器上创建"test up1/archive: * OR | or $ or < and >."目录:
//First call succeeds
string strPath = "ftp://webhost.com/%2F/UserDir/" +
Uri.EscapeDataString("test up1") + "/";
createDirViaFTP(strPath);
//but then this call fails
strPath += Uri.EscapeDataString("archive: * OR | or $ or < and >.") + "/";
createDirViaFTP(strPath);
static bool createDirViaFTP(string strURI)
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(strURI);
request.EnableSsl = bUseSsl; //can be either false or true
request.Credentials = = new NetworkCredential(strUsrName, secUsrPwd);
request.UseBinary = true;
request.Timeout = -1;
request.Method = WebRequestMethods.Ftp.MakeDirectory;
try
{
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
{
if (response.StatusCode == FtpStatusCode.PathnameCreated)
{
//Created OK
return true;
}
}
}
catch(Exception ex)
{
//Failed
Console.WriteLine("EXCEPTION: Path=" + strURI + "\n" + ex.Message);
}
return false;
}当我尝试创建createDirViaFTP dir时,对"archive: * OR | or $ or < and >."的第二个调用引发以下异常:
EXCEPTION: Path=ftp://webhost.com/%2F/UserDir/test%20up1/archive%3A%20*%20OR%20%7C%20or%20%24%20or%20%3C%20and%20%3E./
The remote server returned an error: (550) File unavailable (e.g., file not found, no access).但是为什么呢?我在这里做错什么了?第二个目录名中的所有符号都应该是合法的Linux文件名。我可以通过shell创建同一个目录。
发布于 2017-10-04 06:01:38
我不能在*nix ProFTPD FTP服务器上创建这样的文件夹,即使我可以在同一个系统中创建这样的文件夹。
在FTP中,我得到
550存档:* OR \或$或<和>.:无效目录名
这可能就是FtpWebRequest也能得到的。它只是盲目地将任何550错误翻译为“文件不可用”。
因此,我认为这不是代码的问题,而是FTP服务器对文件/目录名称的限制。
值得注意的是,ProFTPD无条件地不允许目录名中的星号。
请参阅 function
MODRET core_mkd(cmd_rec *cmd) {
int res;
char *decoded_path, *dir;
CHECK_CMD_MIN_ARGS(cmd, 2);
/* XXX Why is there a check to prevent the creation of any directory
* name containing an asterisk?
*/
if (strchr(cmd->arg, '*')) {
pr_response_add_err(R_550, _("%s: Invalid directory name"), cmd->arg);
pr_cmd_set_errno(cmd, EINVAL);
errno = EINVAL;
return PR_ERROR(cmd);
}
...实际上,如果我从目录名中删除*,创建就会成功。所以我想,您也可以使用ProFTPD FTP服务器进行测试。
https://stackoverflow.com/questions/46552961
复制相似问题