我正在尝试使用ASync Task通过FTP发送文件,这是我到目前为止找到的但是无法调用它来发送文件:
package com.hdwinc.hdworderentry;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.SocketException;
import java.net.UnknownHostException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import android.os.AsyncTask;
import android.util.Log;
public abstract class HDWFTP_Upload extends AsyncTask{
//void FTP_DATA_UPLOAD(String FULL_PATH_TO_LOCAL_FILE)
protected Long doInBackground(String... FULL_PATH_TO_LOCAL_FILE ) {
// encapsulate FTP inside a A sync task
{
System.out.println("Entered FTP transfer function");
FTPClient ftpClient = new FTPClient();
int reply;
try {
System.out.println("Entered Data Upload loop!");
ftpClient.connect(ServerName,21);
ftpClient.login(Username, Password);
ftpClient.sendCommand("QUOTE SITE NAMEFMT 1");
ftpClient.changeWorkingDirectory("/OPTICON/");
System.out.println("Entered Data Upload loop!");
reply = ftpClient.getReplyCode();
if(FTPReply.isPositiveCompletion(reply)){
System.out.println("Connected Success");
}else {
System.out.println("Connection Failed");
ftpClient.disconnect();
}
if (ftpClient.getReplyString().contains("250")) {
ftpClient.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);
BufferedInputStream buffIn = null;
System.out.println("Created an input stream buffer");
System.out.println(FULL_PATH_TO_LOCAL_FILE.toString());
buffIn = new BufferedInputStream(new FileInputStream(FULL_PATH_TO_LOCAL_FILE[0]));
ftpClient.enterLocalPassiveMode();
System.out.println("Entered binary and passive modes");
//Handler progressHandler=null;
//ProgressInputStream progressInput = new ProgressInputStream(buffIn, progressHandler);
//Code to Extract name from the string.
//http://stackoverflow.com/questions/10549504/obtain-name-from-absolute- path-substring-from-last-slash-java-android
String Picture_File_name = new File(FULL_PATH_TO_LOCAL_FILE[0]).getName();
boolean result = ftpClient.storeFile(Picture_File_name, buffIn); //localAsset.getFileName()
//ProgressInputStream progressInput = new ProgressInputStream(buffIn, progressHandler);
if (result){
System.out.println("Success");
}
//boolean result = ftpClient.storeFile("TEST.jpg", progressInput);
System.out.println("File saved");
//buffIn.close();
ftpClient.logout();
ftpClient.disconnect();
}
} catch (SocketException e) {
Log.e("Citizen Cam FTP", e.getStackTrace().toString());
System.out.println("Socket Exception!");
} catch (UnknownHostException e) {
Log.e("Citizen Cam FTP", e.getStackTrace().toString());
} catch (IOException e) {
Log.e("Citizen Cam FTP", e.getStackTrace().toString());
System.out.println("IO Exception!");
}
return null;
}
}
}
我如何在另一个活动中调用它?我的订单活动创建了一个文件,我需要将其发送到服务器。
答案 0 :(得分:2)
我删除了每个#Squonk的抽象引用,并查看了#Mike M提供的两个页面并提出了解决方案。
谢谢你们。
public class HDWFTP_Upload extends AsyncTask <String, Void, Long>{
protected Long doInBackground(String... FULL_PATH_TO_LOCAL_FILE ) {
{
FTPClient ftpClient = new FTPClient();
int reply;
try {
System.out.println("Entered Data Upload loop!");
ftpClient.connect(servername,21);
ftpClient.login(user, pass);
ftpClient.sendCommand("QUOTE SITE NAMEFMT 1");
ftpClient.changeWorkingDirectory("/OPTICON/");
reply = ftpClient.getReplyCode();
if(FTPReply.isPositiveCompletion(reply)){
System.out.println("Connected Success");
}else {
System.out.println("Connection Failed");
ftpClient.disconnect();
}
if (ftpClient.getReplyString().contains("250")) {
ftpClient.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);
BufferedInputStream buffIn = null;
System.out.println("Created an input stream buffer");
System.out.println(FULL_PATH_TO_LOCAL_FILE.toString());
buffIn = new BufferedInputStream(new FileInputStream(FULL_PATH_TO_LOCAL_FILE[0]));
ftpClient.enterLocalPassiveMode();
System.out.println("Entered binary and passive modes");
String Picture_File_name = new File(FULL_PATH_TO_LOCAL_FILE[0]).getName();
boolean result = ftpClient.storeFile(Picture_File_name, buffIn);
if (result){
System.out.println("Success");
}
System.out.println("File saved");
ftpClient.logout();
ftpClient.disconnect();
}
} catch (SocketException e) {
Log.e("HDW FTP", e.getStackTrace().toString());
System.out.println("Socket Exception!");
} catch (UnknownHostException e) {
Log.e("HDW FTP", e.getStackTrace().toString());
} catch (IOException e) {
Log.e("HDW FTP", e.getStackTrace().toString());
System.out.println("IO Exception!");
}
return null;
}
}
跟:
new HDWFTP_Upload().execute(uploadFile);