package com.gaoftp.test;
import java.io.File;
import java.io.IOException;
import com.enterprisedt.net.ftp.FTPClient;
import com.enterprisedt.net.ftp.FTPException;
import com.enterprisedt.net.ftp.FTPFile;
import com.enterprisedt.net.ftp.FTPTransferType;
public class FtpService {
public String ftpServer=null;
public String ftpPort=null;
public String ftpUserName=null;
public String ftpPassword=null;
public Boolean isLogin;
public FTPClient ftpClient=null;
public FtpService(String pFtpServer, String pFtpPort, String pFtpUserName,String pFtpPassword) throws Exception
{
this.ftpServer = pFtpServer;
if (pFtpPort.trim().equals(""))
this.ftpPort = "21";
else
this.ftpPort = pFtpPort;
if (pFtpUserName.trim().equals(""))
this.ftpUserName = "Anonymous";
else
this.ftpUserName = pFtpUserName;
this.ftpPassword = pFtpPassword;
try {
ftpClient = new FTPClient(); //ftpServer, Integer.parseInt(ftpPort)
ftpClient.setRemoteHost(ftpServer);
ftpClient.setRemotePort(Integer.parseInt(ftpPort));
ftpClient.setControlEncoding("gbk"); //加上这一句后在 edtftpj 2.0.1 下就可以传中文文件名了
System.out.println("开始登录");
ftpClient.connect();
ftpClient.login(ftpUserName, ftpPassword);
System.out.println("登录成功");
ftpClient.chdir("//"); //在有的ftp服务器运行会出错,用ftpClient.chdir("/")又可以了
System.out.println("已转入根目录");
isLogin = true;
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}
public void uploadAllFilesInFolder(String folderName, String ftpPath) throws
Exception {
if (isLogin) {
String strMsg = "";
try {
File file = new File(folderName);
if (file.isDirectory()) {
ftpClient.chdir("//");
ftpClient.setType(FTPTransferType.BINARY);
if (checkFolderIsExist(ftpPath)) {
ftpClient.chdir(ftpPath);
} else {
createFolder(ftpPath);
}
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) {
try {
ftpClient.put(files[i].getPath(),
files[i].getName());
} catch (Exception ee) {
strMsg += "upload file<<:" + files[i].getPath() +
">> error!Message:" + ee.getMessage() +
"/r/n";
}
}
}
} else {
throw new Exception(folderName + " is not a folder'name!");
}
} catch (Exception e) {
throw new Exception(e.getMessage());
}
} else {
throw new Exception("you didnot login remote ftp server!");
}
}
public void uploadFile(String clientFileName, String ftpPath) throws
Exception {
if (isLogin) {
try {
//获取文件名
String filename = "";
int index = clientFileName.lastIndexOf("//");
filename = clientFileName.substring(index + 1);
ftpClient.chdir("//");
ftpClient.setType(FTPTransferType.BINARY);
if (checkFolderIsExist(ftpPath)) {
ftpClient.chdir(ftpPath);
} else {
createFolder(ftpPath);
}
ftpClient.put(clientFileName, filename);
} catch (Exception ex) {
throw new Exception(ex.getMessage());
}
} else {
throw new Exception("you didnot login remote ftp server!");
}
}
public void downloadFolder(String ftppath, String outdir, String ftpdir) throws
IOException {
try {
/
public void downloadFile(String remoteFileName,String localFileName){
try{
ftpClient.get(localFileName, remoteFileName);
}catch(Exception e){
e.printStackTrace();
}finally{
}
}
public String deleteFile(String ftpPath) throws
Exception {
if (isLogin) {
String strMsg = "";
try {
ftpClient.delete(ftpPath);
strMsg+="删除文件成功";
} catch (FTPException ex) {
strMsg += "删除文件错误:" + ex.getMessage();
} catch (IOException ex) {
strMsg += "操作文件错误:" + ex.getMessage();
}
return strMsg;
} else {
throw new Exception("you didnot login remote ftp server!");
}
}
public String deleteFolder(String ftpPath) throws
Exception {
if (isLogin) {
String strMsg = "";
ftpClient.chdir("//"); //进入目录
//列出目录下所有文件和文件夹
FTPFile[] ftpfiles = ftpClient.dirDetails(ftpPath);
for (int i = 0; i < ftpfiles.length; i++) {
FTPFile tempftpfile = ftpfiles[i];
System.out.println("===================================");
System.out.println("文件名:"+tempftpfile.getName());
System.out.println("路径:"+tempftpfile.getPath());
System.out.println("是否是止录:"+tempftpfile.isDir());
System.out.println("最后修改日录:"+tempftpfile.lastModified());
System.out.println("===================================");
if (tempftpfile.isDir()) {
ftpPath = ftpPath + "//" + tempftpfile.getName();
System.out.println("1:" + ftpPath);
deleteFolder(ftpPath);
} else {
ftpClient.delete(ftpPath + "//" + tempftpfile.getName());
}
}
ftpClient.cdup();
ftpClient.rmdir(ftpPath);
return strMsg;
} else {
throw new Exception("you didnot login remote ftp server!");
}
}
public static boolean isDirExist(String dirname, String[] files) {
for (int i = 0; i < files.length; i++) {
if (files[i].indexOf("
") > -1 &&
files[i].indexOf(dirname) > -1) {
return true;
}
}
return false;
}
/**
* 在FTP服务上建立目录