Question

I'm retrieving a file from a FTP Server. The file is encoded as UTF-8

ftpClient.connect(props.getFtpHost(), props.getFtpPort());
ftpClient.login(props.getUsername(), props.getPassword());
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
inputStream = ftpClient.retrieveFileStream(fileNameBuilder
                    .toString());

And then somewhere else I'm reading the input stream

bufferedReader = new BufferedReader(new InputStreamReader(
                    inputStream, "UTF-8"));

But the file is not getting read as UTF-8 Encoded!

I tried ftpClient.setAutodetectUTF8(true); but still doesn't work.

Any ideas?

EDIT: For example a row in the original file is ...00248090041KENAN SARÐIN 00000000015.993FAC...

After downloading it through FTPClient, I parse it and load in a java object, one of the fields of the java object is name, which for this row is read as "KENAN SAR�IN"

I tried dumping to disk directly:

File file = new File("D:/testencoding/downloaded-file.txt");
FileOutputStream fop = new FileOutputStream(file);
ftpClient.retrieveFile(fileName, fop);
if (!file.exists()) {
    file.createNewFile();
}

I compared the MD5 Checksums of the two files(FTP Server one and the and the one dumped to disk), and they're the same.

Was it helpful?

Solution

I would separate out the problems first: dump the file to disk, and compare it with the original. If it's the same as the original, the problem has nothing to do with UTF-8. The FTP code looks okay though, and if you're saying you want the raw binary data, I'd expect it not to mess with anything.

If the file is the same after transfer as before, then the problem has nothing to do with FTP. You say "the file is not getting read as UTF-8 Encoded" but it's not clear what you mean. How certain are you that it's UTF-8 text to start with? If you could edit your question with the binary data, how it's being read as text, and how you'd expect it to be read as text, that would really help.

OTHER TIPS

Try to download the file content as bytes and not as characters using InputStream and OutputStream instead of InputStreamReader. This way you are sure that the file is not changed during transfer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top