2010-07-07 136 views
10

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

+0

[你如何将图片上传到FTP应用程序中的FTP服务器?](http://stackoverflow.com/questions/6464456/how-do-you-upload-images-to-an-ftp -server-within-an-android-app) – 2011-11-23 18:00:10

+0

尽管链接副本更新,但它在这个问题没有答案的地方有答案。 – 2011-11-23 18:00:41

+1

你问你会如何在android中实现一个FTP客户端?或者你只是想连接到FTP服务器。市场上似乎有几个FTP应用程序,但如果有任何工作或没有,我不知道。 – Falmarri 2010-07-07 05:09:16

回答

6

使用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(); 
} 

这是所有的功能,所以它不会让你下载文件!

+0

@如果我的帮助者有帮助,请接受。如果不是,我们该如何帮助你呢? – RTB 2012-07-19 11:22:53

+0

Upvoted,它帮助知道有一些lib ...你可以让我知道还有什么其他的API可用此Jar/lib \ – AAnkit 2012-07-31 16:30:45

+0

@Ankit对不起,这是我发现的唯一的库... – RTB 2012-08-06 09:47:53

3

下载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(); 
    } 

} 

这将肯定工作。我做了很多次。

+0

这可能有点晚,但使用此方法上传总是会返回错误代码550(拒绝访问)。有什么建议么??? – shreyas 2014-06-13 06:20:36

+0

@shreyas你解决了你的问题吗? – 2015-04-08 08:30:00

+0

链接中断:(:( – 2016-01-30 11:22:59