Filename encoding in Apache Commons Net FTPClient
Asked Answered
B

2

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?

Blossomblot answered 2/3, 2012 at 15:31 Comment(2)
I'd say you are looking in the wrong place, this is probably a configuration in the FTP server...Evocative
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.Peroration
F
31

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

client.setControlEncoding("UTF-8");
Firehouse answered 27/3, 2012 at 11:42 Comment(2)
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 yaseb.wordpress.com/2012/03/07/…Blossomblot
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");Forwards
P
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.

Pedology answered 30/11, 2015 at 11:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.