2015-05-20 32 views
2

我使用命令行(視窗)將文件發送到IBM主機和工作正常,它是這樣的:使用C#FTP文件到大型機,包括數據集 - 翻譯FTP腳本的FtpWebRequest代碼

Open abc.wyx.state.aa.bb 
User 
Pass 
lcd c:\Transfer> 
Put examplefile 'ABCD.AA.C58FC.ABC1FD.ZP3ABC' 
close 
bye 

我需要將其轉換爲C#。我一直在嘗試使用FtpWebRequest,但沒有運氣。我無法弄清楚如何包含我猜測的數據集。當我運行應用程序,我得到了以下錯誤:

((System.Exception)(ex)).Message "The remote server returned an error: (550) File unavailable (e.g., file not found, no access)." 550 Unable to store The remote server returned an error: (550) File unavailable (e.g., file not found, no access).

((FtpWebResponse)ex.Response).StatusDescription "550 Unable to store /'ABCD.AA.C58FC.ABC1FD.ZP3ABC/examplefile'\r\n"

以下是我在C#

string user = "user"; 
string pwd = "password"; 

string ftpfullpath = @"ftp://abc.wyx.state.aa.bb//'ABCD.AA.C58FC.ABC1FD.ZP3ABC'/examplefile'"; 

try 
{ 
    FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath); 
    ftp.Credentials = new NetworkCredential(user, pwd); 

    ftp.KeepAlive = true; 
    ftp.UseBinary = false; //Use ascii.    

    ftp.Method = WebRequestMethods.Ftp.UploadFile; 

    FileStream fs = File.OpenRead(inputfilepath); 
    byte[] buffer = new byte[fs.Length]; 
    fs.Read(buffer, 0, buffer.Length); 
    fs.Close(); 

    Stream ftpstream = ftp.GetRequestStream(); 
    ftpstream.Write(buffer, 0, buffer.Length); 
    ftpstream.Close(); 
} 
catch (WebException ex) 
{ 
    String status = ((FtpWebResponse)ex.Response).StatusDescription; 
    throw new Exception(status); 
} 
+1

您是否考慮過查看'Process.Start'來運行批處理文件,或者您想使用嚴格的C#代碼發送它?你有沒有使用Google搜索如何通過FTP連接和發送文件..有大量的在線例子 – MethodMan

+0

是的,它必須在C#上,是的,我看的鏈接,但我找不到它對我有幫助。我正在使用與我的批處理文件相同的憑據,它們在上傳文件時沒有問題。 –

+0

我只是更新錯誤消息並用虛擬名稱替換x。 examplefiles是我在cmd腳本上使用的真實名稱並且可以工作。 –

回答

1

沒有指定運行在什麼平臺在ftp腳本。我假設Windows。

當您使用Windows ftp命令put像:

put localpath remotepath 

它導致FTP服務器上的以下調用:

STOR remotefile 

同樣,如果你使用FtpWebRequest與URL像

ftp://example.com/remotepath 

是導致FTP服務器上的以下(下同)電話:

STORE remotepath 

注意主機名後的第一個斜槓(example.com)被省略。


這意味着您在ftp腳本命令

Open abc.wyx.state.aa.bb 
... 
Put examplefile 'ABCD.AA.C5879.ABC123.123ABC' 

轉化爲FtpWebRequest網址,如:

string ftpfullpath = @"ftp://abc.wyx.state.aa.bb/'ABCD.AA.C5879.ABC123.123ABC'"; 

兩個結果在FTP服務器上的這一呼籲:

STOR 'ABCD.AA.C5879.ABC123.123ABC' 

相反,你的ftp代碼

string ftpfullpath = @"ftp://abc.wyx.state.aa.bb//'ABCD.AA.C5879.ABC123.123ABC'/examplefile'"; 

結果:

STOR /'ABCD.AA.C5879.ABC123.123ABC'/examplefile' 

它不會對主機看起來是正確的。


一個談話內容由我的C#代碼:

USER user 
331 Password required for user 
PASS password 
230 Logged on 
OPTS utf8 on 
200 UTF8 mode enabled 
PWD 
257 "/" is current directory. 
TYPE A 
200 Type set to A 
PASV 
227 Entering Passive Mode (zzz,zzz,zzz,zzz,193,162) 
STOR 'ABCD.AA.C5879.ABC123.123ABC' 
150 Connection accepted 
226 Transfer OK 

的談話內容被我ftp腳本:

USER user 
331 Password required for user 
PASS password 
230 Logged on 
PORT zzz,zzz,zzz,zzz,193,186 
200 Port command successful 
STOR 'ABCD.AA.C5879.ABC123.123ABC' 
150 Opening data channel for file transfer. 
226 Transfer OK 
QUIT 
221 Goodbye 

我測試過這種反對的FileZilla FTP服務器,所以顯然FTP服務器的響應在主機FTP上會有所不同。但客戶端的FTP命令應該是相同的。

+0

也許我錯過了一些東西,但代碼上的位置是我想要上傳的文件名稱的引用'examplefile' –

+0

事實上,在Windows命令提示符下運行示例文件'ABCD.AA.C5879.ABC123.123ABC'。如果您的代碼不包含文件名,您的代碼如何與我的ftp代碼相當?或者也許它是以一種不同的方式,我只是不明白。如何編寫c#代碼將文件示例文件上傳到數據集「ABCD.AA.C5879.ABC123.123ABC」? –

+0

文件名在'inputfilepath'中(代碼中沒有顯示的值,但我認爲它指的是一個名爲'examplefile'的本地文件=等同於'Put'命令的第一個參數)。 –