package com.gzc.ftpuploadfile;
import android.util.Log;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class FTPUtils {
//ftp服务器地址
private String hostname = "";
//ftp服务器端口号
private Integer port = 0;
//ftp登录账号
private String username = "";
//ftp登录密码
private String password = "";
private FTPClient ftpClient = null;
private static FTPUtils mFTPUtils = null;
private FTPUtils() {
}
public static FTPUtils getInstance() {
if (mFTPUtils == null) {
synchronized (FTPUtils.class) {
if (mFTPUtils == null) {
mFTPUtils = new FTPUtils();
}
}
}
return mFTPUtils;
}
/**
* 初始化配置 全局只需初始化一次
* @param hostname ftp服务器地址
* @param port ftp服务器端口号默认为21
* @param username ftp登录账号
* @param password ftp登录密码
*/
public void initFtpClient(String hostname, int port, String username, String password) {
this.hostname = hostname;
this.port = port;
this.username = username;
this.password = password;
ftpClient = new FTPClient();
ftpClient.setControlEncoding("utf-8");
}
/**
* 连接并登陆ftp
* @return
*/
private boolean connectFtp(){
boolean flag = false;
try {
Log.e("------------>", "connecting...ftp服务器:" + this.hostname + ":" + this.port);
ftpClient.connect(hostname, port); //连接ftp服务器
ftpClient.login(username, password); //登录ftp服务器
int replyCode = ftpClient.getReplyCode(); //是否成功登录服务器
if (!FTPReply.isPositiveCompletion(replyCode)) {
Log.e("error", "连接...FTP服务器..失败: " + this.hostname + ":" + this.port + " ");
}
flag = true;
Log.e("------------>", "connect successfu...ftp服务器:" + this.hostname + ":" + this.port);
} catch (Exception e) {
Log.e("------------>", "connect ftp exception ::" + e.getMessage() + " " + e);
}
return flag;
}
/**
* 上传文件
*
* @param ftpSavePath ftp服务保存地址 (不带文件名)
* @param ftpSaveFileName 上传到ftp的文件名
* @param originFile 待上传文件
* @return
*/
public boolean uploadFile(String ftpSavePath, String ftpSaveFileName, File originFile) {
boolean flag = false;
try {
FileInputStream inputStream = new FileInputStream(originFile);
flag = uploadFile(ftpSavePath, ftpSaveFileName, inputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.e("------------>", e.getMessage() + " " + e);
}
return flag;
}
/**
* 上传文件
*
* @param ftpSavePath ftp服务保存地址 (不带文件名)
* @param ftpSaveFileName 上传到ftp的文件名
* @param originFileName 待上传文件的名称(绝对地址) *
* @return
*/
public boolean uploadFile(String ftpSavePath, String ftpSaveFileName, String originFileName) {
boolean flag = false;
try {
FileInputStream inputStream = new FileInputStream(new File(originFileName));
flag = uploadFile(ftpSavePath, ftpSaveFileName, inputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.e("------------>", e.getMessage() + " " + e);
}
return flag;
}
/**
* 上传文件
*
* @param ftpSavePath ftp服务保存地址
* @param ftpSaveFileName 上传到ftp的文件名
* @param inputStream 输入文件流
* @return
*/
public boolean uploadFile(String ftpSavePath, String ftpSaveFileName, InputStream inputStream) {
boolean flag = false;
try {
boolean connect = connectFtp();
if(connect == false) {
flag = false;
return flag;
}
Log.e("------------>", "FTP :: 开始上传文件");
ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE);
CreateDirecroty(ftpSavePath);
ftpClient.makeDirectory(ftpSavePath);
ftpClient.changeWorkingDirectory(ftpSavePath);
flag = ftpClient.storeFile(ftpSaveFileName, inputStream);
inputStream.close();
ftpClient.logout();
Log.e("------------>", flag == true ? "FTP 上传文件成功" : "FTP 上传文件失败");
} catch (Exception e) {
Log.e("------------>", "FTP :: 上传文件失败" + e.getMessage() + " " + e);
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException e) {
Log.e(e.getMessage(), e + " ");
}
}
if (null != inputStream) {
try {
inputStream.close();
} catch (IOException e) {
Log.e(e.getMessage(), e + " ");
}
}
}
return flag;
}
/**
* 下载文件 *
*
* @param pathname FTP服务器文件目录 *
* @param filename 文件名称 *
* @param localpath 下载后的文件路径 *
* @return
*/
public boolean downloadFile(String pathname, String filename, String localpath) {
boolean flag = false;
OutputStream os = null;
try {
boolean connect = connectFtp();
if(connect == false) {
flag = false;
return flag;
}
// 设置类型为二进制
ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE);
//切换FTP目录
ftpClient.changeWorkingDirectory(pathname);
FTPFile[] ftpFiles = ftpClient.listFiles();
for (FTPFile file : ftpFiles) {
if (filename.equalsIgnoreCase(file.getName())) {
File localFile = new File(localpath + "/" + file.getName());
os = new FileOutputStream(localFile);
ftpClient.retrieveFile(file.getName(), os);
os.close();
}
}
ftpClient.logout();
flag = true;
} catch (Exception e) {
e.printStackTrace();
Log.e(e.getMessage(), e + " ");
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException e) {
Log.e(e.getMessage(), e + " ");
}
}
if (null != os) {
try {
os.close();
} catch (IOException e) {
Log.e(e.getMessage(), e + " ");
}
}
}
Log.e("------------>","FTP 下载文件" + (flag == true ? "成功 " : "失败"));
return flag;
}
/**
* 删除文件 *
*
* @param pathname FTP服务器保存目录 *
* @param filename 要删除的文件名称 *
* @return
*/
public boolean deleteFile(String pathname, String filename) {
boolean flag = false;
try {
boolean connect = connectFtp();
if(connect == false) {
flag = false;