3

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

错误:

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

在本地,创建了具有正确名称的文件,但它是空的。同样的事情发生在#文件名内部的 JPG 文件上。我怎样才能让他们被认出来?

//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;
    }
}
4

2 回答 2

5

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

您可以做的是在创建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 类,它可以正确处理转义:

Uri myUri = new UriBuilder("ftp", "someServer", 21, "somePath/intro_to_c#.pdf");
// yields ftp://someServer:21/somePath/intro_to_c%23.pdf
于 2013-07-24T04:54:10.360 回答
1

我添加了一些代码并修复了它。使用 hexEscape 转义“#”,但它并不像样。任何人都知道转义 URI 中的特殊字符吗?

    // Get the request using a specific URI 
    static public FtpWebRequest GetWebRequest(string method, string uri)
    {
        Uri serverUri = new Uri(uri);

        **if (serverUri.ToString().Contains("#"))
        {
            serverUri = new Uri(serverUri.ToString().Replace("#", Uri.HexEscape('#')));
        }**
        Console.WriteLine(serverUri.ToString());
        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;
        }
    }
于 2013-07-24T03:52:02.423 回答