2013-07-23 96 views
1

我试图下载文件,但所有具有特殊字符的文件都无法识别。其他文件可以下载,而名为“asdf#code @ .pdf”的文件不能下载。错误:远程服务器返回错误:(550)文件不可用(例如,文件未找到,无法访问)。在本地,具有正确名称的文件被创建,但它是空的。在文件名中使用“#”的JPG文件也会发生同样的情况。我怎么能让他们被认可?c#文件名特殊字符

//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; 
     } 
    } 
+2

什么你的意思是“无法识别”?你会得到什么错误?当您尝试使用'WebMethods.Ftp.ListDirectory'获取该FTP目录的目录列表时,输出是什么? – Heinzi

+0

可下载其他文件,而无法下载名为“asdf#code @ .pdf”的文件。错误:远程服务器返回错误:(550)文件不可用(例如,文件未找到,无法访问)。 – Quentin

+0

WebMethods.Ftp.ListDirectory中的文件名是正确的。 – Quentin

回答

2

可能是由设计:根据the URI standard#不在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 
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; 
     } 
    }