在Android上使用FTP上传图像

12

如何在Android上使用FTP上传图像?


1
你是在询问如何在Android中实现FTP客户端吗?还是只是想连接到FTP服务器。市场上似乎有几个FTP应用程序,但我不知道它们是否有效。 - Falmarri
我想通过FTP将图片上传到服务器,但是我没有使用Android SDK的代码来上传它。 - Amit
虽然链接的重复问题是较新的,但它有一个答案,而这个问题没有。 - Brad Larson
2个回答

7

使用SimpleFTP,只需将simpleftp.jar添加到您的类路径中,并在将要使用它的任何类中导入该包:在此下载

import org.jibble.simpleftp.*;

上传图像等文件时,请确保使用二进制模式,否则可能会损坏文件。

try
{
    SimpleFTP ftp = new SimpleFTP();

    // Connect to an FTP server on port 21.
    ftp.connect("ftp.somewhere.net", 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("webcam.jpg"));
    ftp.stor(new File("comicbot-latest.png"));

    // You can also upload from an InputStream, e.g.
    ftp.stor(new FileInputStream(new File("test.png")), "test.png");
    ftp.stor(someSocket.getInputStream(), "blah.dat");

    // Quit from the FTP server.
    ftp.disconnect();
}
catch (IOException e)
{
    e.printStackTrace();
}

这只是所有功能,所以它不允许您下载文件!

@Amit 如果我的回答有帮助,请接受它。如果没有,我们还能如何帮助您? - RTB
点赞了,知道有一些库很有帮助...您能告诉我这个Jar/lib还提供了哪些其他API吗? - AAnkit
SimpleFTP是根据GNU GPL许可的。他们还提供商业许可证。 - Yar

3

从这里下载FTP Jar库

public void sendFileViaFTP() {

    FTPClient ftpClient = null;

    try {
        ftpClient = new FTPClient();
        ftpClient.connect(InetAddress.getByName("ftp.myserver.com"));

        if (ftpClient.login("myftpusername", "myftppass")) {

            ftpClient.enterLocalPassiveMode(); // important!
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            String Location = Environment.getExternalStorageDirectory()
                    .toString();
            String data = Location + File.separator + "FileToSend.txt";
            FileInputStream in = new FileInputStream(new File(data));
            boolean result = ftpClient.storeFile("FileToSend.txt", in);
            in.close();
            if (result)
                Log.v("upload result", "succeeded");
            ftpClient.logout();
            ftpClient.disconnect();

        }
    } catch (Exception e) {
        Log.v("count", "error");
        e.printStackTrace();
    }

}

这肯定会奏效。我已经做过很多次了。

可能有点晚了,但是使用这种方法上传总是返回错误代码550(访问被拒绝)。有什么建议吗? - shreyas

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接