15

I have to upload a file to an FTP server. The filename contains special letters, say äöü. On the FTP server, I need the filename to be UTF-8 encoded.

My code is like this:

import org.apache.commons.net.ftp.FTPClient;

FTPClient client = new FTPClient();

...

boolean retval = client.storeFile(fileName, inputStream);

The problem is that after storeFile, the name of the file saved on the FTP server is ISO-8859-1 encoded rather than UTF-8.

How can I tell FTPClient to UTF-8 encode the file names?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
gefei
  • 18,922
  • 9
  • 50
  • 67
  • 1
    I'd say you are looking in the wrong place, this is probably a configuration in the FTP server... – Marcelo Mar 02 '12 at 15:36
  • 1
    Not necessarily. The original FTP protocol spec did not support Unicode at all. In order to use UTF-8 over an FTP connection, both parties have to agree to its use first. The server has to report in the `FEAT` command that it even supports UTF-8 (see RFC 2640, though not all servers support that spec). Some servers require clients to send non-standard `OPTS UTF8 ON` or `OPTS UTF-8 NLST` commands to activate UTF-8. So that is the $1M question - what does `FTPClient` support, and what does the server support? I would use a packet sniffer, like WareShark, to watch the FTP traffic and see. – Remy Lebeau Mar 08 '12 at 01:58

2 Answers2

31

I have not tested it, but you can try this:

client.setControlEncoding("UTF-8");
logoff
  • 3,347
  • 5
  • 41
  • 58
  • 6
    Yes, setControlEncoding is right. It is important, however, to call setControlEncoding before connect, otherwise it does not work. I actually think this is a piece of unfortunate design of the library, see also http://yaseb.wordpress.com/2012/03/07/apache-commons-unfortunate-design-of-ftpclient/ – gefei Mar 27 '12 at 11:52
  • 2
    Also note that if your server is a Windows IIS FTP server, the fact that `UTF8` is allowed at the server will not mean that it will be used in connections unless, after connecting, you additionally issue an `OPTS UTF8 ON` command: `client.sendCommand("OPTS UTF8 ON");` – Daniel Fernández Apr 08 '21 at 22:36
7

Since Apache Commons NET 3.0 one can use ftpClient.setAutodetectUTF8( true ); to enable autodetection of UTF-8 support on the FTP server. Like setControlEncoding it must be called before connection.

See the corresponding javadoc.

eskatos
  • 4,174
  • 2
  • 40
  • 42