**
* 类描述: 文件断点续传工具类
* @author:赵博
* @since:2017-6-14 下午13:41
* @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
*
*
*/
public class ContinueFTP2 {
public FTPClient ftpClient = new FTPClient();
private String ftpURL,username,pwd,ftpport,file1,file2;
/******
*
* @param _ftpURL 文件服务器长传地址
* @param _username 用户名
* @param _pwd 密码
* @param _ftpport 端口
* @param _file1 要传输的文件
* @param _file2 目的文件夹
*/
public ContinueFTP2(String _ftpURL,String _username,String _pwd,String _ftpport,String _file1,String _file2 ){
//设置将过程中使用到的命令输出到控制台
ftpURL = _ftpURL;
username = _username;
pwd = _pwd;
ftpport = _ftpport;
file1 = _file1;
file2 = _file2;
ftpClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
}
/**
* 连接到FTP服务器
* @param hostname 主机名
* @param port 端口
* @param username 用户名
* @param password 密码
* @return 是否连接成功
* @throws IOException
*/
public boolean connect(String hostname,int port,String username,String password) throws IOException{
ftpClient.connect(hostname, port);
ftpClient.setControlEncoding("GBK");
if(FTPReply.isPositiveCompletion(ftpClient.getReplyCode())){
if(ftpClient.login(username, password)){
return true;
}
}
disconnect();
return false;
}
/** *//**
* 断开与远程服务器的连接
* @throws IOException
*/
public void disconnect() throws IOException{
if(ftpClient.isConnected()){
ftpClient.disconnect();
}
}
/** *//**
* 递归创建远程服务器目录
* @param remote 远程服务器文件绝对路径
* @param ftpClient FTPClient 对象
* @return 目录创建是否成功
* @throws IOException
*/
public UploadStatus CreateDirecroty(String remote,FTPClient ftpClient) throws IOException{
UploadStatus status = UploadStatus.Create_Directory_Success;
String directory = remote.substring(0,remote.lastIndexOf("/")+1);
if(!directory.equalsIgnoreCase("/")&&!ftpClient.changeWorkingDirectory(new String(directory.getBytes("GBK"),"iso-8859-1"))){
//如果远程目录不存在,则递归创建远程服务器目录
int start=0;
int end = 0;
if(directory.startsWith("/")){
start = 1;
}else{
start = 0;
}
end = directory.indexOf("/",start);
while(true){
String subDirectory = new String(remote.substring(start,end).getBytes("GBK"),"iso-8859-1");