2

FTP サーバーからファイルを取得しています。ファイルは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());

そして、別の場所で入力ストリームを読んでいます

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

しかし、ファイルは UTF-8 エンコードとして読み取られません!

試しftpClient.setAutodetectUTF8(true);ましたが、まだ動作しません。

何か案は?

編集: たとえば、元のファイルの行は ...00248090041KENAN SARÐIN 00000000015.993FAC... です。

FTPClient 経由でダウンロードした後、それを解析して Java オブジェクトにロードします。Java オブジェクトのフィールドの 1 つは name で、この行では「KENAN SAR�IN」として読み取られます。

ディスクに直接ダンプしようとしました:

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

2 つのファイル (FTP サーバーのファイルとディスクにダンプされたファイル) の MD5 チェックサムを比較したところ、同じでした。

4

2 に答える 2

2

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.

于 2012-10-16T06:19:08.537 に答える
0

InputStreamReaderの代わりにInputStreamとOutputStreamを使用して、ファイルの内容を文字ではなくバイトとしてダウンロードしてみてください。このようにして、転送中にファイルが変更されないようにします。

于 2012-10-16T09:42:02.303 に答える