public class UploadFileUtils {
private static boolean flag;
/**
* 需在在子線程下執行
* pathname 為:服務器存儲地址 ,remote 為:上傳到ftp服務器之后,文件的名字 ,path 為:要上傳文件的地址
* @param username
* @param password
* @param pathname
* @param remote
* @param path
* @return
*/
public static boolean uploadfile(String username,String password,String pathname,String remote,String path ) {
Log.e("TAG","loginASDAAAAAAAAAAAAAAAAAA:");
//服務器域名
String host = " ";
int port = 21;
// String username = "";
// String password = "";
// String pathname = "E:/ftp"; //上傳到服務器之后存放的位置
// String remote = "9.mp4"; //上傳到ftp服務器之后,文件的名字
InputStream is = null;
try {
Log.e("TAG","11111:");
is = new FileInputStream(path);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//uploadFile(host, port, username, password, pathname, remote,is);
try {
Log.e("TAG","2222:");
//創建FTPClient對象
FTPClient client = new FTPClient();
Log.e("TAG","3333:");
//鏈接ftp服務
client.connect(host, port);
Log.e("TAG","555:");
//登錄ftp服務器
boolean login = client.login(username, password);
// System.out.println("login:" + login);
Log.e("TAG","login:" + login);
//判斷pathname是否存在,//切換ftp服務的工作目錄
Log.e("TAG","666:");
if (!client.changeWorkingDirectory(pathname)) {
client.makeDirectory(pathname); //創建工作目錄
client.changeWorkingDirectory(pathname);
}
//設置上傳的文件類型
client.setFileType(FTP.BINARY_FILE_TYPE);
//創建InputStream對象
//InputStream is=new FileInputStream("F:/2.png");
//完成文件上傳
flag = client.storeFile(remote, is);
Log.e("TAG","flag:" + flag);
client.logout(); //退出
client.disconnect(); //斷開鏈接
} catch (Exception ex) {
ex.printStackTrace();
}
return flag;
}
}