2016-12-19 1 views
-1

C#을 사용하여 로컬 컴퓨터에서 FTP 서버로 파일을 복사하려고합니다. 아래 코드를 사용하면 파일이 FTP 서버로 완전히 복사되지만 원본 줄은 512 바이트 만 잘라내어 1152,1126 또는 1024 바이트 길이로 잘라냅니다.Stream은 C#을 사용하여 FTP 서버의 파일에 512 바이트 만 씁니다.

buffer.length : 1,152

버퍼 ** I가 사용되는 예시적인 파일은 이제 일례 대신 파일의 Console.writeline 7.

public void uploadLOTFILE(string username, string password) 
    { 
     FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://mysite.mine/mypathandfilename"); 
     request.Method = WebRequestMethods.Ftp.UploadFile; 
     request.Credentials = new NetworkCredential(username, password); 
     request.KeepAlive = true; 
     System.IO.Stream rs = request.GetRequestStream(); 

     var lines = File.ReadLines(@"myLocalFile.txt"); 
     foreach (var line in lines) 
     { 
      byte[] buffer = Encoding.ASCII.GetBytes(line);     
      Console.WriteLine("buffer.length:" + buffer.Length.ToString()); 
      rs.Write(buffer,0,buffer.Length); 
     } 
     rs.Close(); 

     FtpWebResponse response = (FtpWebResponse)request.GetResponse(); 

     Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription); 

     response.Close(); 
     } 

출력 라인 16을 갖는다. 길이 : 1126

buffer.length : 1152

buffer.length : 1152

buffer.length : 1152

buffer.length : 1152

buffer.length : 1024 나는 또한 MSDN에서 정확한 사본 (https://msdn.microsoft.com/en-us/library/ms229715(v=vs.110).aspx)를 사용했지만이 같은했다

결과.

편집 : 은 또한 다음과 같은 코드를 시도 : 문자열적인 filePath = @ "myFilePath을"; var fileName = Path.GetFileName (filePath); var request = (FtpWebRequest) WebRequest.Create ("ftp://myftp");

 request.Method = WebRequestMethods.Ftp.UploadFile; 
     request.Credentials = new NetworkCredential(username, password); 
     request.UsePassive = true; 
     request.UseBinary = true; 
     request.KeepAlive = false; 

     using (var fileStream = File.OpenRead(filePath)) 
     { 
      using (var requestStream = request.GetRequestStream()) 
      { 
       fileStream.CopyTo(requestStream); 
       requestStream.Close(); 
      } 
     } 

동일한 결과를 제공합니다. 파일은 완전히 복사되지만 512 바이트마다 개행 문자가 추가됩니다.

FileZilla를 사용하면 동일한 유형의 파일을 올바르게 FTP 전송할 수 있습니다.

+0

* "파일이 FTP 서버에 완전히 복사되었지만 행의 길이는 512 바이트입니다."* - 이해가되지 않습니다. 그래서 파일이 완전히 복사 되었습니까? –

+0

라인이 512 라인 길이인지 어떻게 확인합니까? –

+0

실제로 무엇을하려고합니까? 파일을 줄 단위로 왜 업로드합니까? –

답변

0

cmd를 사용하여 액세스 할 수있는 빌드 윈도우 FTP를 사용하여 문제를 해결했습니다.

  string[] lines = { username, password, "remote directory", "put " + "\"" + filePath + "\"" , "bye"}; 
     // WriteAllLines creates a file, writes a collection of strings to the file, 
     // and then closes the file. You do NOT need to call Flush() or Close(). 
     System.IO.File.WriteAllLines("full file path of your script", lines); 
     Process cmd = new Process(); 
     cmd.StartInfo.FileName = "cmd.exe"; 
     cmd.StartInfo.RedirectStandardInput = true; 
     cmd.StartInfo.RedirectStandardOutput = true; 
     cmd.StartInfo.CreateNoWindow = true; 
     cmd.StartInfo.UseShellExecute = false; 
     cmd.Start(); 

     cmd.StandardInput.WriteLine(@"ftp -s:" + "\"" + "full file path of your script" + "\"" + " remote device"); 
     cmd.StandardInput.Flush(); 
     cmd.StandardInput.Close(); 
     cmd.WaitForExit();