使用ftp上传Android文件

时间:2015-03-09 06:05:08

标签: android file-upload server sync

Android文件上传无效,未显示任何错误或任何异常。

我想添加一些东西,例如检查互联网连接,只有在它工作时它才会尝试上传文件,并且还希望在服务器和应用程序之间添加一个更新的东西,这个代码将是在互联网连接正常工作的时间间隔后自动呼叫。

我尝试过很多jar文件上传文件并建立ftp连接,但没有任何效果。 jar文件链接http://www.jibble.org/files/simpleftp.jar

import org.jibble.simpleftp.SimpleFTP;

File file = new File("/sdcard/CreativeDroid/test.txt");

这样我调用MyAsyncTask类来上传一个txt文件

new MyAsyncTask().execute(file.toString());

private class MyAsyncTask extends AsyncTask <String, Integer, Double> {

    protected Double doInBackground(String... params) {
        try {


            SimpleFTP ftp = new SimpleFTP();

            // Connect to an FTP server on port 21.
            ftp.connect("www.creativetabs.co", 21, "username", "password");

            // Set binary mode.
            ftp.bin();

            // Change to a new working directory on the FTP server.
            //ftp.cwd("web");

            // Upload some files.
            ftp.stor(new File(params[0]));

            // Quit from the FTP server.
            ftp.disconnect();

        } catch (IOException e) {
            Log.d("error", e.toString());
            // TODO Auto-generated catch block
        }
        return null;
    }
}

1 个答案:

答案 0 :(得分:1)

尝试使用ftp4j:

http://www.sauronsoftware.it/projects/ftp4j/

我3年来一直在使用它,它完美无瑕。

private int uploadFile() throws Exception {
    long fileSize = 0;
    boolean supported = ftp.isResumeSupported();
    String fileName = getFileName(file_path);
    String server_path = data.getServer_folder() + fileName;
    try {
        fileSize = ftp.fileSize(server_path);
    } catch (Exception e) {
        fileSize = 0;
    }
    if (fileSize == 0) {
        ftp.upload(new java.io.File(file_path), new FTPDataTransferListener() {
            @Override
            public void transferred(int arg0) {
                uploaded = uploaded + arg0;
                Utilities.showDLog(Tag, "uploaded: " + uploaded);
            }

            @Override
            public void started() {
                Utilities.showDLog(Tag, "uplaod started");
            }

            @Override
            public void failed() {
                Utilities.showDLog(Tag, "upload failed");
                disconnectFTP();
                outputCode = FTPOutputCodes.FAILED;
            }

            @Override
            public void completed() {
                outputCode = FTPOutputCodes.SUCCESSFULLY_UPLOADED;
                Utilities.showDLog(Tag, "upload completed");
            }

            @Override
            public void aborted() {
                outputCode = FTPOutputCodes.ABORTED;
                disconnectFTP();
                Utilities.showDLog(Tag, "upload aborted");
            }
        });
    } else if (supported) { // resume file upload
        ftp.upload(new java.io.File(file_path), fileSize, new FTPDataTransferListener() {
            @Override
            public void transferred(int arg0) {
                Utilities.showDLog(Tag, "Resume data transfered: " + arg0);
            }

            @Override
            public void started() {
                Utilities.showDLog(Tag, "Resume started");
            }

            @Override
            public void failed() {
                outputCode = FTPOutputCodes.FAILED;
                disconnectFTP();
                Utilities.showDLog(Tag, "Resume failed");
            }

            @Override
            public void completed() {
                outputCode = FTPOutputCodes.SUCCESSFULLY_UPLOADED;
            }

            @Override
            public void aborted() {
                outputCode = FTPOutputCodes.ABORTED;
                disconnectFTP();
                Utilities.showDLog(Tag, "resume aborted");
            }
        });
    }
    return outputCode;
}


protected void disconnectFTP() {
    try {
        ftp.disconnect(true);
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (FTPIllegalReplyException e) {
        e.printStackTrace();
    } catch (FTPException e) {
        e.printStackTrace();
    }
}