jar包:log4j.jar commoms-net-1.4.1.jar jakarta-oro-2.0.5.jar
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.SocketException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.log4j.Logger;
public class FtpUtil {
private static Logger log=Logger.getLogger(FtpUtil.class.getName());
private static String charSet="UTF-8";
/**
* 连接ftp
*/
public static FTPClient getFtpClient(String ftpHost, String ftpUserName, String ftpPassword, int ftpPort) {
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(ftpHost, ftpPort);
ftpClient.login(ftpUserName, ftpPassword);
ftpClient.setControlEncoding(charSet);
if(!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
log.debug("未连接到FTP,用户名或密码错误!");
ftpClient.disconnect();
}else {
log.info("FTP连接成功!");
}
} catch (SocketException e) {
// TODO Auto-generated catch block
log.error("IP地址可能错误,请正确配置!");
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
log.error("FTP端口错误,请正确配置!");
e.printStackTrace();
}
return ftpClient;
}
/**
* 下载文件
*/
public static boolean downloadFile(String ftpHost, String ftpUserName, String ftpPassword, int ftpPort, String ftpPath, String localPath, String fileName) {
boolean downloadResult = false;
FTPClient ftpClient = null;
try {
ftpClient=getFtpClient(ftpHost, ftpUserName, ftpPassword, ftpPort);
ftpClient.setControlEncoding(charSet);
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
ftpClient.changeWorkingDirectory(ftpPath);
FTPFile[] files=ftpClient.listFiles(ftpPath);
boolean fileExist=false;
for(FTPFile file:files) {
if(file.getName().equals(fileName)) {
fileExist=true;
}else {
fileExist=false;
}
}
if(fileExist) {
File loadFile = new File(localPath + File.separator + fileName);
OutputStream out = new FileOutputStream(loadFile);
ftpClient.retrieveFile(fileName, out);
out.close();
//删除ftp文件
ftpClient.deleteFile(ftpPath + File.separator + fileName);
ftpClient.logout();
downloadResult=true;
}else {
log.error("没有找到"+fileName+"文件!");
}
}catch(FileNotFoundException e) {
log.error("没有找到"+fileName+"文件!");
downloadResult=false;
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
log.error("文件读取错误!");
downloadResult=false;
e.printStackTrace();
}
return downloadResult;
}
/**
* 上传文件
*/
public static boolean uploadFile(String ftpHost, String ftpUserName, String ftpPassword, int ftpPort, String ftpPath, String fileName, String localPath) {
boolean uploadResult = false;
FTPClient ftpClient = null;
try {
ftpClient=getFtpClient(ftpHost, ftpUserName, ftpPassword, ftpPort);
int reply=ftpClient.getReplyCode();
if(!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
return uploadResult;
}
ftpClient.setControlEncoding(charSet);
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
ftpClient.changeWorkingDirectory(ftpPath);
FileInputStream in=new FileInputStream(new File(localPath));
ftpClient.storeFile(fileName, in);
in.close();
ftpClient.logout();
uploadResult=true;
}catch(IOException e) {
log.error("文件读取错误!");
e.printStackTrace();
}finally {
if(ftpClient!=null) {
try {
ftpClient.disconnect();
}catch(IOException e) {
e.printStackTrace();
}
}
}
return uploadResult;
}
}