0

I am using org.apache.commons.net.ftp.FTPClient.retriveFile(String,OutputStream) to download file from FTP to windows machine. When I download one text file it's format is missing. When I open downloaded file in notepad it's not looking like original one. Please find the difference in attached screenshots. Please find my below code and please anyone let me know solution for the same.

FileOutputStream fos = new FileOutputStream(tempPath+File.separator+files[i].getName());
this.ftpClient.retrieveFile(files[i].getName(), fos);
fos.close();

Downloaded file when i open it in Notepad-it's looking in incorrect format

Original file when i open it on Notepad-looking Good like table format

Spidy
  • 1,137
  • 3
  • 28
  • 48
Madhu Sudhan Reddy
  • 607
  • 5
  • 15
  • 22
  • Looks like it's downloading the file as binary, where you want it as text so it'll convert the line endings to your platform's format. Search the documentation for something like "transfer mode text". – CodeCaster Mar 10 '14 at 11:31
  • Yes,The original file ins in ASCII format and we are trying to download it in Binary format.Can you please help me how to download file in ASCII format – Madhu Sudhan Reddy Mar 10 '14 at 11:36
  • See [Apache Commons FTP problems](http://stackoverflow.com/questions/6651158/apache-commons-ftp-problems): `boolean success = client.setFileType(FTP.ASCII_FILE_TYPE);`. But it seems the default type already is ASCII. – CodeCaster Mar 10 '14 at 11:45

2 Answers2

0

The file has not been changed, transport protocols like ftp never really change the content of the file itself. At most they modify some header informations.

What you observe most likely is the result of different visualization of the whitespaces contained in the file. For example tab characters may be visualized using a different amount of spaces, thus resulting in a different indentation.

This case might also be the result of a different line width: apparently your first screenshot simply shows the lines being wrapped at a certain length. Missing linebreaks are also a typical result of the different way how different systems code linebreaks. MS-Windows uses a different line encoding as other systems do (incompatibility by purpose). Try using an editor with intelligent linebreak handling. Or one where you can chose a linebreak in an explicit manner.

So this issue is not an ftp problem, it (most likely) has to do with different software or settings you use on both machines.

arkascha
  • 41,620
  • 7
  • 58
  • 90
  • Thanks for your reply,Here what i found is some line breaks are missing. – Madhu Sudhan Reddy Mar 10 '14 at 11:32
  • FTP _does_ [change line endings](http://stackoverflow.com/questions/554960/how-can-i-stop-filezilla-changing-my-linebreaks). Also you can see the white box in the output, indicating only `\n` was found, where Windows expects `\r\n` for newlines. – CodeCaster Mar 10 '14 at 11:33
  • Actually original file is ASCII format and we are trying to download it in Binary format.i think this is the reason for the issue.Please let me know how to download file in ASCII format. – Madhu Sudhan Reddy Mar 10 '14 at 11:34
  • @CodeCaster No! The linebreaks are _not_ missing, neither have they been altered. FTP does not do such thing! There simply is not a common standard for _how_ linebreaks are coded. `\n` is the perfectly correct linebreak for unixoid systems. – arkascha Mar 10 '14 at 11:35
  • Yes it does, [read the RFC](http://tools.ietf.org/html/rfc959): _"In accordance with the NVT standard, the (`\r\n`) sequence should be used where necessary to denote the end of a line of text"_. It is up to the client to convert the CRLFs (`\r\n`) to the client's platform's line endings. If a source file only contains `\n` for newlines and it is sent as _binary_, the line endings aren't altered. – CodeCaster Mar 10 '14 at 11:42
0

Not sure what is going wrong, i never did save files directly to disk but it looks like it makes one line and your word wrapper breaks it again.

You could try to read the file line by line and then write that to disc. What language are you using? I can hand you a C# solution.

string[] text;

using (StreamReader sr = new StreamReader("LoadFile.txt"))

        {
            string line;
            while ((line = sr.ReadLine()) != null) 
            {
                text += line + "\n";
            }
        }

using (StreamWriter sw = new StreamWriter("WriteFile.txt"))

        {
            foreach (string line in text)
            {
                sw.WriteLine(line);

            }
        }

This atleast makes sure each line in the file to load gets his own line in the file you write.

Madmenyo
  • 8,389
  • 7
  • 52
  • 99