Search code examples
javacharacter-encodingapache-commons-net

FTP File Upload - Filename Encoding Error


I'm using apache's commons-net 3.5 to upload files to a remote FTP server, and setting the connection encoding to UTF-8 like below, before openning the connection.

ftpClient.setAutodetectUTF8(true);
ftpClient.setControlEncoding("UTF-8");

And this is the part that sends the file

private void uploadFile(byte[] data, String path, String fileName, FTPClient ftpClient) throws IOException {
    ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
    ftpClient.setFileTransferMode(FTPClient.BINARY_FILE_TYPE);
    boolean uploadSuccess = ftpClient.storeUniqueFile(fileName, new  ByteArrayInputStream(data));

Here is the problem; when sending files whose name contains Turkish characters, the result file is named incorrectly. Like,

Kimlikş.pdf -> KimlikÅ.pdf

But if I use ftp4j, everything works fine. Did anyone have this before? The data ships incrorrectly from my side. I monitored the traffic with Microsoft Network Monitor. Here is the raw request:

FTP FTP:Request from Port 62642,'STOR KimlikÅ.pdf'  {TCP:5879, IPv4:134}

Solution

  • I guess there was a bug with apache's commons-net, so I migrated to ftp4j and the problem was no more.

    <dependency>
        <groupId>it.sauronsoftware</groupId>
        <artifactId>ftp4j</artifactId>
        <version>1.6</version>
    </dependency>