mardi 25 août 2015

How to upload a sqlite db file to ftp server from android program

APP crashed every time I invoke ftp write operation.

I have below 2 methods in a "fragment".

public void uploadData () {

    String domain = "10.142.42.10";  // <<< will it work as it is private ip?
    String user = "xxxx";
    String password = "xxxx";
    String serverRoad = "Documents";

    File file = new File("/sdcard/bluetooth/NRC_jiten.pdf");
    FTPClient ftp = new FTPClient();
    try {
        ftp.connect(InetAddress.getByName(domain));
        //ftp.connect(domain,21);
        ftp.login(user, password);

        ftp.changeWorkingDirectory(serverRoad);
        ftp.setFileType(FTP.BINARY_FILE_TYPE);
        FileInputStream is = new FileInputStream(file);
        BufferedInputStream buffIn = new BufferedInputStream(is);
        ftp.enterLocalPassiveMode();
        ftp.storeFile("jiten_pdf.pdf", buffIn);
        buffIn.close();

        ftp.logout();
        ftp.disconnect();
    } catch (Exception e) {
        Toast.makeText(getActivity(), "err:" + e,
                Toast.LENGTH_SHORT).show();
    }

}

public void uploadFileBackground () {

    new Thread(new Runnable() {
        public void run() {

            uploadData();

        }
    }).start();


}

Now, from an activity I trigger uploadFileBackground upon a mouse click on a button.

Is there anything wrong with the code? I have referred this site, but not able to succeed.

TIA.

Aucun commentaire:

Enregistrer un commentaire