开发者

Uploading a file from Android to a server through FTP? [closed]

开发者_开发技巧 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

I'm trying to upload the file to a server. What is the way for uploading a file to a server through FTP?


Solution from this site

import org.apache.commons.net.ftp.FTPClient;



FTPClient ftpClient = new FTPClient();

try {
    ftpClient.connect(InetAddress.getByName(SERVER));
    ftpClient.login(USERNAME, PASSWORD);
    ftpClient.changeWorkingDirectory(PATH);

    if (ftpClient.getReplyString().contains("250")) {
        ftpClient.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);
        BufferedInputStream buffIn = null;
        buffIn = new BufferedInputStream(new FileInputStream(FULL_PATH_TO_LOCAL_FILE));
        ftpClient.enterLocalPassiveMode();
        ProgressInputStream progressInput = new ProgressInputStream(buffIn, progressHandler);

        boolean result = ftpClient.storeFile(localAsset.getFileName(), progressInput);
        buffIn.close();
        ftpClient.logout();
        ftpClient.disconnect();
    }

} catch (SocketException e) {
    Log.e(SorensonApplication.TAG, e.getStackTrace().toString());
} catch (UnknownHostException e) {
    Log.e(SorensonApplication.TAG, e.getStackTrace().toString());
} catch (IOException e) {
    Log.e(SorensonApplication.TAG, e.getStackTrace().toString());
}


Add this external jar to your project, and start coding

http://www.jibble.org/simpleftp/

and also check this,

http://www.javabeat.net/tips/36-file-upload-and-download-using-java.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜