Search code examples
javaftpftp-clientapache-commons-net

Upload File containing latin characters


I'm using latest Apache Commons Net to make use of FTP functionality.

My goal is to upload CSV files (based on ;), which might contain latin characters, such as ñ, á or Ú. The thing is that when I upload them to the FTP Server, those characters are transformed to another.

The following line:

12345678A;IÑIGO;PÉREZ;JIMÉNEZ;X

gets transformed into this:

12345678A;IÑIGO;PÉREZ;JIMÉNEZ;X

My code seems something like that:

   // pFile is passed as parameter to the current method
   InputStream is = new FileInputStream(pFile);
   ftp.setFileType(FTP.BINARY_FILE_TYPE);
   ftp.setControlEncoding("UTF-8");
   if (ftp.storeFile("some\\path", is)) {
       is.close();
       ...
   }

I've digged some hours to find a solution (I thought setFileType() and/or setControlEncoding() would work), but nope...

I've tried to print to the standard output (screen, with logger and System.out), and I've realised that it's InputStream who doesn't read those characters. Executing the following code printed the mentioned characters in a right way:

   InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
   BufferedReader in = new BufferedReader(isr);
   String line = null;
   while((line = in.readLine()) != null){
       System.out.print(line);
       logger.debug(line);
   }
   in.close();
   isr.close();

But how to tell FTP client or storeFile() to make use of UTF-8?

Thank you all.


Solution

  • Sorry, but I've got the answer.

    When I've told you that I see transformed some characters

    12345678A;IÑIGO;PÉREZ;JIMÉNEZ;X
    

    I meant that those characters were seen on a FTP Client application (I use WinSCP). The issue is that the default character encoding was selected and it wasn't UTF-8-

    Now, after realising it, I select the proper encoding (UTF-8), and the text seem to be well-formed.

    Thanks for your help.