package com.ftp;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.LinkedList;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
public class FTP {
/**
* 服务器名.
*/
private String hostName;
/**
* 端口号
*/
private int serverPort;
/**
* 用户名.
*/
private String userName;
/**
* 密码.
*/
private String password;
/**
* FTP连接.
*/
private FTPClient ftpClient;
public FTP() {
this.hostName = "192.168.1.101";
this.serverPort = 21;
this.userName = "admin";
this.password = "1234";
this.ftpClient = new FTPClient();
}
// -------------------------------------------------------文件上传方法------------------------------------------------
/**
* 上传单个文件.
*
* @param localFile
* 本地文件
* @param remotePath
* FTP目录
* @param listener
* 监听器
* @throws IOException
*/
public void uploadSingleFile(File singleFile, String remotePath,
UploadProgressListener listener) throws IOException {
// 上传之前初始化
this.uploadBeforeOperate(remotePath, listener);
boolean flag;
flag = uploadingSingle(singleFile, listener);
if (flag) {
listener.onUploadProgress(MainActivity.FTP_UPLOAD_SUCCESS, 0,
singleFile);
} else {
listener.onUploadProgress(MainActivity.FTP_UPLOAD_FAIL, 0,
singleFile);
}
// 上传完成之后关闭连接
this.uploadAfterOperate(listener);
}
/**
* 上传多个文件.
*
* @param localFile
* 本地文件
* @param remotePath
* FTP目录
* @param listener
* 监听器
* @throws IOException
*/
public void uploadMultiFile(LinkedList<File> fileList, String remotePath,
UploadProgressListener listener) throws IOException {
// 上传之前初始化
this.uploadBeforeOperate(remotePath, listener);
boolean flag;
for (File singleFile : fileList) {
flag = uploadingSingle(singleFile, listener);
if (flag) {
listener.onUploadProgress(MainActivity.FTP_UPLOAD_SUCCESS, 0,
singleFile);
} else {
listener.onUploadProgress(MainActivity.FTP_UPLOAD_FAIL, 0,
singleFile);
}
}
// 上传完成之后关闭连接
this.uploadAfterOperate(listener);
}
/**
* 上传单个文件.
*
* @param localFile
* 本地文件
* @return true上传成功, false上传失败
* @throws IOException
*/
private boolean uploadingSingle(File localFile,
UploadProgressListener listener) throws IOException {
boolean flag = true;
// 不带进度的方式
// // 创建输入流
// InputStream inputStream = new FileInputStream(localFile);
// // 上传单个文件
// flag = ftpClient.storeFile(localFile.getName(), inputStream);
// // 关闭文件流
// inputStream.close();
// 带有进度的方式
BufferedInputStream buffIn = new BufferedInputStream(
new FileInputStream(localFile));
ProgressInputStream progressInput = new ProgressInputStream(buffIn,
listener, localFile);
flag = ftpClient.storeFile(localFile.getName(), progressInput);
buffIn.close();
return flag;
}
/**
* 上传文件之前初始化相关参数
*
* @param remotePath
* FTP目录
* @param listener
* 监听器
* @throws IOException
*/
private void uploadBeforeOperate(String remotePath,
UploadProgressListener listener) throws IOException {
// 打开FTP服务
try {
this.openConnect();
listener.onUploadProgress(MainActivity.FTP_CONNECT_SUCCESSS, 0,
null);
} catch (IOException e1) {
e1.printStackTrace();
listener.onUploadProgress(MainActivity.FTP_CONNECT_FAIL, 0, null);
return;
}
// 设置模式
ftpClient.setFileTransferMode(org.apache.commons.net.ftp.FTP.STREAM_TRANSFER_MODE);
// FTP下创建文件夹
ftpClient.makeDirectory(remotePath);
// 改变FTP目录
ftpClient.changeWorkingDirectory(remotePath);
// 上传单个文件
}
/**
* 上传完成之后关闭连接
*
* @param listener
* @throws IOException
*/
private void uploadAfterOperate(UploadProgressListener listener)
throws IOException {
this.closeConnect();
listener.onUploadProgress(MainActivity.FTP_DISCONNECT_SUCCESS, 0, null);
}
// -------------------------------------------------------文件下载方法------------------------------------------------
/**
* 下载单个文件,可实现断点下载.
*
* @param serverPath
* Ftp目录及文件路径
* @param localPath
* 本地目录
* @param fileName
* 下载之后的文件名称
* @param listener
* 监听器
* @throws IOException
*/
public void downloadSingleFile(String serverPath, String localPath, String fileName, DownLoadProgressListener listener)
throws Exception {
// 打开FTP服务
try {
this.openConnect();
listener.onDownLoadProgress(MainActivity.FTP_CONNECT_SUCCESSS, 0, null);
} catch (IOException e1) {
e1.printStackTrace();
listener.onDownLoadProgress(MainActivity.FTP_CONNECT_FAIL, 0, null);
return;
}
// 先判断服务器文件是否存在
FTPFile[] files = ftpClient.listFiles(serverPath);
if (files.length == 0) {
listener.onDownLoadProgress(MainActivity.FTP_FILE_NOTEXISTS, 0, null);
return;
}
//创建本地文件夹
File mkFile = new File(localPath);
if (!mkFile.exists()) {
mkFile.mkdirs();
}
localPath = localPath + fileName;
// 接着判断下载的文件是否能断点下载
long serverSize = files[0].getSize(); // 获取远程文件的长度
File localFile = new File(localPath);
long localSize = 0;
if (localFile.exists()) {
localSize = localFile.length(); // 如果本地文件存在,获取本地文件的长度
if (localSize >= serverSize) {
File file = new File(localPath);
file.delete();
}
}
// 进度
long step = serverSize / 100;
long process = 0;
long c