简体   繁体   中英

Sending a databse via ftp - Getting a different file

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM