从C#的FTP服务器下载名称包括特殊字符的文件 [英] Download a file with name including special characters from FTP server in C#

查看:478
本文介绍了从C#的FTP服务器下载名称包括特殊字符的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试下载文件,但是无法识别所有带有特殊字符的文件。可以下载其他文件,而不能下载名为 asdf#code @ .pdf 的文件。

I tried to download files, but all files with special character cannot be recognized. Other files can be downloaded, while file named asdf#code@.pdf cannot be downloaded.

错误:


远程服务器返回错误:(550)文件不可用(例如,找不到文件,无法访问)。

The remote server returned an error: (550) File unavailable (e.g., file not found, no access).

在本地创建具有正确名称的文件,但该文件为空。对于文件名中带有的JPG文件,也会发生相同的情况。我该如何识别它们?

In local, the file with correct name is created, but it is empty. The same thing happens on JPG files with # inside of the file names. how can I let them be recognized?

//Download the file from remote path on FTP to local path
private static void Download(string remotePath, string localPath)
{
    FtpWebRequest reqFTP;
    try
    {
        reqFTP = GetWebRequest(WebRequestMethods.Ftp.DownloadFile, remotePath);
        FileStream outputStream = new FileStream(localPath, FileMode.Create);

        FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
        Stream ftpStream = response.GetResponseStream();
        long cl = response.ContentLength;
        int bufferSize = 2048;
        int readCount;
        byte[] buffer = new byte[bufferSize];
        readCount = ftpStream.Read(buffer, 0, bufferSize);
        while (readCount > 0)
        {
            outputStream.Write(buffer, 0, readCount);
            readCount = ftpStream.Read(buffer, 0, bufferSize);
        }
        ftpStream.Close();
        outputStream.Close();
        response.Close();
        Console.WriteLine("File Download: ", remotePath + " is downloaded completely");
        logWriter.WriteLog("File Download: ", remotePath + " is downloaded completely, status " + response.StatusDescription);
    }
    catch (Exception ex)
    {
        logWriter.WriteLog("File Download: ", "Cannot download file from " + remotePath + " to " + localPath + "\n" + " Erro Message: " + ex.Message);
    }
}//End Download

//Web request for FTP
static public FtpWebRequest GetWebRequest(string method, string uri)
{
    Uri serverUri = new Uri(uri);

    if (serverUri.Scheme != Uri.UriSchemeFtp)
    {
        return null;
    }
    try
    {
        var reqFTP = (FtpWebRequest)FtpWebRequest.Create(serverUri);
        reqFTP.Method = method;
        reqFTP.UseBinary = true;
        reqFTP.Credentials = new NetworkCredential(userId, password);
        reqFTP.Proxy = null;
        reqFTP.KeepAlive = false;
        reqFTP.UsePassive = false;
        return reqFTP;
    }
    catch(Exception ex)
    {
        logWriter.WriteLog("Get Web Request: ","Cannot connect to " + uri + "\n" + "Error: " + ex.Message);
        return null;
    }
}


推荐答案

可能是有意设计的:根据 URI标准在URI中不是有效字符。因此, ftp://someServer/somePath/intro_to_c#.pdf 不是有效的URI。

This might be by design: According to the URI standard, # is not a valid character in a URI. Thus, ftp://someServer/somePath/intro_to_c#.pdf is not a valid URI.

您可以做的是在创建 URI时正确转义文件名:

What you could do is to properly escape the file name when creating the URI:

string baseUri = "ftp://someServer/somePath/";
string file = "intro_to_c#.pdf";
string myUri = baseUri + HttpUtility.UrlEncode(file);
// yields ftp://someServer/somePath/intro_to_c%23.pdf

,您可以使用UriBuilder类,该类可以正确处理转义:

Alternatively, you could use the UriBuilder class, which handles escaping properly:

Uri myUri = new UriBuilder("ftp", "someServer", 21, "somePath/intro_to_c#.pdf");
// yields ftp://someServer:21/somePath/intro_to_c%23.pdf

这篇关于从C#的FTP服务器下载名称包括特殊字符的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆