FTP added extra character
I have a instream that takes in raw binary data from Bluetooth on Android 2.2 phone. The data coming in goes into a buffer 1024 in size. I read() the data and then take that and write to a file. I send that file via FTP to my computer. I noticed a disturbing pattern when ftp'ing that an extra character gets inserted every once in a while. So I printed out the buffer first to LogCat and noticed the character was not there. Here is my read write code.
FTPClient con = new FTPClient();
File file = new File(Environment.getExternalStorageDirectory() + "/ftp/new/" + "testdata.bin");
try {
con.connect("someIPAddress");
if (con.login("anonymous", "anonymous@anon.com")) {
con.enterLocalPassiveMode(); // important!
FileInputStream in = new FileInputStream(file);
boolean result = con.storeFile("testdata.bin", in);
in.close();
if (result) {
Log.v("upload result", "succeeded");
}
}
} catch (Exception e) {e.printStackTrace();}
Here is an example of the output from the logcat:
09 15 D0 0D 17 0A 06 08 07
and here is what is in the file after ftp'ing:
09 15 D0 0D 17 0D 0A 06 08 07
Well I thought that hmm 0A something is injecting 0D to make (C开发者_如何学CRLF) but it doesn't happen at every 0A. I can write the same program in C# and this doesn't happen at all. So any ideas or help?
On further investigation I found it occurs when the data going in is 17 0A and the file shows 17 0D 0A.
Solution: The FTPClient will send the file by default as ASCII. Set the fileType to Binary file by using this command:
con.setFileType(FTP.BINARY_FILE_TYPE);
Windows, unix and mac all have different line endings.
FTP "fixes" this for you in ASCII mode.
精彩评论