android傳送照片到FTP伺服器

來源:互聯網
上載者:User

在安卓環境下可以使用,在java環境下也可以使用,本人先在Java環境下實現了功能,然後移植到了安卓手機上,其它都是一樣的。

package com.photo;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import org.apache.commons.net.ftp.FTPClient;import org.apache.commons.net.ftp.FTPReply;public class FileTool {/** * Description: 向FTP伺服器上傳檔案 *  * @param url *            FTP伺服器hostname * @param port *            FTP伺服器連接埠 * @param username *            FTP登入帳號 * @param password *            FTP登入密碼 * @param path *            FTP伺服器儲存目錄,是linux下的目錄形式,如/photo/ * @param filename *            上傳到FTP伺服器上的檔案名稱,是自己定義的名字, * @param input *            輸入資料流 * @return 成功返回true,否則返回false */public static boolean uploadFile(String url, int port, String username,String password, String path, String filename, InputStream input) {boolean success = false;FTPClient ftp = new FTPClient();try {int reply;ftp.connect(url, port);// 串連FTP伺服器// 如果採用預設連接埠,可以使用ftp.connect(url)的方式直接連接FTP伺服器ftp.login(username, password);//登入reply = ftp.getReplyCode();if (!FTPReply.isPositiveCompletion(reply)) {ftp.disconnect();return success;}ftp.changeWorkingDirectory(path);ftp.storeFile(filename, input);input.close();ftp.logout();success = true;} catch (IOException e) {e.printStackTrace();} finally {if (ftp.isConnected()) {try {ftp.disconnect();} catch (IOException ioe) {}}}return success;}// 測試public static void main(String[] args) {FileInputStream in = null ;File dir = new File("G://pathnew");File files[] = dir.listFiles();if(dir.isDirectory()) {for(int i=0;i<files.length;i++) {try { in = new FileInputStream(files[i]);boolean flag = uploadFile("17.8.119.77", 21, "android", "android","/photo/", "412424123412341234_20130715120334_" + i + ".jpg", in);System.out.println(flag);} catch (FileNotFoundException e) {e.printStackTrace();}}}}}

以上為java代碼,下面是android代碼。

package com.ftp;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import android.os.Bundle;import android.app.Activity;import android.util.Log;import android.view.Menu;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);new uploadThread().start();}class uploadThread extends Thread {@Overridepublic void run() {FileInputStream in = null ;File dir = new File("/mnt/sdcard/DCIM/Camera/test/");File files[] = dir.listFiles();if(dir.isDirectory()) {for(int i=0;i<files.length;i++) {try { in = new FileInputStream(files[i]);boolean flag = FileTool.uploadFile("17.8.119.77", 21, "android", "android","/", "412424123412341234_20130715120334_" + i + ".jpg", in);System.out.println(flag);} catch (FileNotFoundException e) {e.printStackTrace();}}}}}}

經過本人測試通過,可正常運行,僅供參考,如有疑問請與我聯絡。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.