In my application I upload a byte[] (serialized Object) to my FTP server, which is working perfectly. However when I try to download it only the first part (like 3000 bytes) of the array are correct, the rest is filled with zeros.
I can’t seem to figure out what is wrong, any help would be appreciated.
I am using the package
org.apache.commons.net.*
public static byte[] downloadBoard( String host, int port, String usr, String pwd) throws IOException {
FTPClient ftpClient = new FTPClient();
byte[] buf = new byte[20000];
try {
ftpClient.connect( host, port );
ftpClient.login( usr, pwd );
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
InputStream is = ftpClient.retrieveFileStream("asdf.board");
is.read(buf);
is.close();
ftpClient.completePendingCommand();
ftpClient.logout();
} finally {
ftpClient.disconnect();
}
return buf;
}
is.read() may not return the full content. You’ll need to put read() into a loop similar to this:
P.S.:
If you know the size of the file, you can use a DataInputStream to read the buffer without a loop: