【问题标题】:Uploading images with FTP on Android在 Android 上使用 FTP 上传图像
【发布时间】:2011-03-12 15:29:11
【问题描述】:

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

【问题讨论】:

  • 您是在问如何在android 中实现FTP 客户端?或者您只是想连接到 FTP 服务器。市场上似乎有几个 FTP 应用程序,但我不知道是否有效。
  • 我想通过 FTP 将图像上传到服务器,但我没有使用 Android SDK 的代码如何上传。
  • 虽然链接的副本较新,但它有一个这个问题没有的答案。

标签: android ftp


【解决方案1】:

使用 SimpleFTP,只需将 simpleftp.jar 添加到您的类路径并将包导入任何将使用它的类中:Download here

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 如果我的回答有帮助,请接受。如果没有,我们如何进一步帮助您?
  • 赞成,它有助于知道有一些库...你能告诉我这个 Jar/lib 还有哪些其他 API 可用\
  • SimpleFTP 在 GNU GPL 下获得许可。他们还提供商业许可。
【解决方案2】:

下载FTP Jar Library from Here

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(拒绝访问)。有什么建议???
最近更新 更多