2010-07-07 137 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