4

I'm currently using this code to send a database over ftp (Using apache commons)

File file = getDatabasePath("database");

FTPClient ftp = new FTPClient();

try {
    ftp.connect(InetAddress.getByName(domain));
    ftp.login(username, password);
    ftp.setFileType(FTP.BINARY_FILE_TYPE);
    FileInputStream is = new FileInputStream(file);
    BufferedInputStream buffIn = new BufferedInputStream(is);
    ftp.enterLocalPassiveMode();
    ftp.storeFile("database", buffIn);
    buffIn.close();
    ftp.logout();
    ftp.disconnect();
} catch (Exception e) {
    // TODO: handle exception
}

I've used it to send a text file and it works. However, I've tried it with my database and a file of the same size is put on the server but SQLite browser displays nothing when I open it. It works on a really small database but as soon as the database is larger I get this problem.

I was wondering if it could be to do with the buffer size? Could anyone shed some light on why this is happening?

Thanks

michaeltpb
  • 53
  • 1
  • 5
  • check out the official doc: https://commons.apache.org/net/api-3.1/org/apache/commons/net/ftp/FTPClient.html maybe you'll find what you have missed (for example you never check the reply code from the server, which may give you information). If you have access to the FTP directly, check out the log files - they will tell you everything. – hovanessyan Mar 31 '12 at 15:04

2 Answers2

0

The code you posted will not work for files which don't fit to the buffered input stream's buffer. What you need to do is read repeatedly from the input stream until its end:

ftp.enterLocalPassiveMode();
ftp.storeFile("database", buffIn);
byte[] buffer = new byte[8192];
OutputStream os = ftp.storeFileStream("database");
int readCount = 0;
while ((readCount = buffIn.read(buffer)) > 0) {
    os.write(buffer, 0, readCount);
}
os.close();
buffIn.close();

The important thing is the use of storeFileStream() instead of storeFile(). Also, as commenters suggest, you need to check return codes from the server and do proper error handling.

0

SQLite Database Copy Appears Corrupted When Generated by Device and not Emulator

Opening the database file in a different program seems to work. See above link.

Community
  • 1
  • 1
michaeltpb
  • 53
  • 1
  • 5