Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filename encoding in Apache Commons Net FTPClient

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?

like image 569
gefei Avatar asked Mar 02 '12 15:03

gefei


People also ask

What is FTPClient in Java?

FTPClient encapsulates all the functionality necessary to store and retrieve files from an FTP server. This class takes care of all low level details of interacting with an FTP server and provides a convenient higher level interface.

Does Apache FTP client support SFTP?

There's a good reason for it too; SFTP is something entirely different from FTP. Its pretty much a 'Java is not Javascript' deal :) The Apache Commons Net project that produces FTPClient DOES support SFTP though.


2 Answers

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

client.setControlEncoding("UTF-8");
like image 127
logoff Avatar answered Oct 16 '22 02:10

logoff


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.

like image 35
eskatos Avatar answered Oct 16 '22 03:10

eskatos