本文實例為大家分享了android傳送照片到FTP服務器的具體代碼,供大家參考,具體內容如下
在安卓環境下可以使用,在java環境下也可以使用,本人先在Java環境下實現了功能,然后移植到了安卓手機上,其它都是一樣的。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
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代碼。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
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 { @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); new uploadThread().start(); } class uploadThread extends Thread { @Override public 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(); } } } } } } |
經過本人測試通過,可正常運行,僅供參考,如有疑問請與我聯系。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/liuzhidong123/article/details/9341269