android傳送照片到FTP服務(wù)器的實(shí)現(xiàn)代碼
本文實(shí)例為大家分享了android傳送照片到FTP服務(wù)器的具體代碼,供大家參考,具體內(nèi)容如下
在安卓環(huán)境下可以使用,在java環(huán)境下也可以使用,本人先在Java環(huán)境下實(shí)現(xiàn)了功能,然后移植到了安卓手機(jī)上,其它都是一樣的。
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服務(wù)器上傳文件
*
* @param url
* FTP服務(wù)器hostname
* @param port
* FTP服務(wù)器端口
* @param username
* FTP登錄賬號
* @param password
* FTP登錄密碼
* @param path
* FTP服務(wù)器保存目錄,是linux下的目錄形式,如/photo/
* @param filename
* 上傳到FTP服務(wù)器上的文件名,是自己定義的名字,
* @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服務(wù)器
// 如果采用默認(rèn)端口,可以使用ftp.connect(url)的方式直接連接FTP服務(wù)器
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 {
@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();
}
}
}
}
}
}
經(jīng)過本人測試通過,可正常運(yùn)行,僅供參考,如有疑問請與我聯(lián)系。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Android系統(tǒng)中跨應(yīng)用數(shù)據(jù)分享功能的實(shí)現(xiàn)
這篇文章主要介紹了Android系統(tǒng)中跨應(yīng)用數(shù)據(jù)分享功能的實(shí)現(xiàn),文中分為發(fā)送文字、二進(jìn)制內(nèi)容和圖片三種情況來講,需要的朋友可以參考下2016-04-04
Android中ContentProvider和ContentResolver詳解
這篇文章主要介紹了Android中ContentProvider和ContentResolver詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04
Android App實(shí)現(xiàn)監(jiān)聽軟鍵盤按鍵的三種方式
本篇文章主要介紹Android App實(shí)現(xiàn)監(jiān)聽軟鍵盤按鍵的三種方式,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-03-03
關(guān)于Kotlin的自動類型轉(zhuǎn)換詳解
這篇文章主要給大家介紹了關(guān)于Kotlin的自動類型轉(zhuǎn)換的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
android 無須root截圖方案的實(shí)現(xiàn)
這篇文章主要介紹了android 無須root截圖方案的實(shí)現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-03-03

