import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.StringTokenizer;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import com.ea.rs4.Logger;
import com.ea.rs4.utils.StringUtils;
/**
* This class is used to upload files to ftp server or get files from ftp server
*
* @author Leo.Zhou
*/
public class FtpTool {
private static final Logger LOG = new Logger(FtpTool.class.getSimpleName());
private String ip;
private int port;
private String user;
private String pwd;
private String remotePath;
FTPClient ftpClient;
/**
* Connect to server
*
* @param ip
* @param port
* @param user
* @param pwd
* @return
* @throws FtpException
*/
public boolean connectServer(String ip, int port, String user, String pwd) throws FtpException {
boolean isSuccess = false;
try {
ftpClient = new FTPClient();
ftpClient.connect(ip, port);
ftpClient.login(user, pwd);
isSuccess = true;
} catch (Exception ex) {
LOG.error("Can not connect to remote ftp server");
throw new FtpException(ex);
}
return isSuccess;
}
/**
* Create directory
*
* @param dir
* @return
* @throws FtpException
*/
private void createDir(String dir) throws FtpException {
try {
ftpClient.setFileType(FTP.ASCII_FILE_TYPE);
StringTokenizer s = new StringTokenizer(dir, "/");
s.countTokens();
String pathName = "";
while (s.hasMoreElements()) {
String currentFolder = (String) s.nextElement();
if (currentFolder == null || currentFolder.length() == 0) {
continue;
}
pathName = pathName + (pathName.equals("")?"":"/") + currentFolder;
if(!isDirExist(pathName)) {
boolean result = ftpClient.makeDirectory(pathName);
if(!result) {
throw new FtpException(String.format("Permission denied, can not create folder : %s", pathName));
}
}
}
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
} catch (IOException e1) {
throw new FtpException(e1);
}
}
/**
* Check is directory exist
*
* @param dir
* @return
* @throws FtpException
*/
private boolean isDirExist(String dir) {
if(StringUtils.isEmpty(dir)) {
return true;
}
String preFolder = null;
String lastFolder = null;
if(dir.contains("/")) {
preFolder = dir.substring(0, dir.lastIndexOf("/"));
lastFolder = dir.substring(dir.lastIndexOf("/") + 1, dir.length());
} else {
preFolder = "";
lastFolder = dir;
}
boolean flag = false;
try {
FTPFile[] ftpFileArr = null;
if(preFolder.equals("")) {
ftpFileArr = ftpClient.listFiles();
} else {
ftpFileArr = ftpClient.listFiles(preFolder);
}
LOG.debug("Files in top folder : %s", Arrays.toString(ftpFileArr));
for (FTPFile ftpFile : ftpFileArr) {
if (ftpFile.isDirectory() && ftpFile.getName().equalsIgnoreCase(lastFolder)) {
flag = true;
break;
}
}
} catch (IOException e) {
flag = false;
}
return flag;
}
/**
* Upload file to ftp server
*
* @param remotePath
* @param localPath
* @param filename
* @throws FtpException
*/
public void uploadFile(String remotePath, File localFile) throws FtpException {
try {
if (connectServer(getIp(), getPort(), getUser(), getPwd())) {
if (!isDirExist(remotePath)) {
createDir(remotePath);
}
if (remotePath.length() != 0)
ftpClient.changeWorkingDirectory(remotePath);
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
InputStream iStream = null;
try {
iStream = new FileInputStream(localFile);
boolean result = ftpClient.storeFile(localFile.getName(), iStream);
if(!result) {
LOG.error("Upload file to FTP server failed.");
throw new FtpException("Upload file failed.");
}
} finally {
if (iStream != null) {
iStream.close();
}
}
closeServer();
}
} catch (IOException ex) {
throw new FtpException("Ftp upload file error:", ex);
}
}
/**
* Delete FTP file by link
*
* @param link
* @throws FtpException
*/
public void deleteFileByLink(String link) throws FtpException{
try {
if (connectServer(getIp(), getPort(), getUser(), getPwd())) {
ftpClient.deleteFile(getRemotePath() + "/" + link);
int status = ftpClient.getReplyCode();
LOG.debug("Ftp delete info:"+ftpClient.getReplyString());
if(status == 250) {
LOG.debug("Delete file from FTP server success.");
} else {
LOG.info("Delete file from FTP server failed.FTP status : %s, link : %s", status, link);
throw new FtpException(String.format("Delete file from FTP server failed, link : %s, FTP status : %s", link, status));
}
closeServer();
}
} catch (IOException ex) {
throw new FtpException(String.format("Delete file from FTP server failed, link : %s", link));
}
}
/**
* Close ftp server connection
*
* @throws FtpException
*/
private void closeServer() throws FtpException {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException e) {
throw new FtpException(e);
}
}
}
/**
* @return
*/
public String getIp() {
return ip;
}
/**
* @return
*/
public int getPort() {
return port;
}
/**
* @return
*/
public String getPwd() {
return pwd;
}
/**
* @return
*/
public String getUser() {
return user;
}
/**
* @param string
*/
public void setIp(String string) {
ip = string;
}
/**
* @param i
*/
public void setPort(int i) {
port = i;
}
/**
* @param string
*/
public void setPwd(String string) {
pwd = string;
}
/**
* @param string
*/
public void setUser(String string) {
user = string;
}
/**
* @return
*/
public String getRemotePath() {
return remotePath;
}
/**
* @param string
*/
public void setRemotePath(String remotePath) {
this.remotePath = remotePath;
this.remotePath = this.remotePath.replace("\\", "/");
LOG.debug("Saving path in current system: %s", this.remotePath);
}
}
分享到:
相关推荐
apache commons-net-3.5 实现 FTP 上传文件的神器,拿走不谢! 主要涉及上传文件的jar,希望能帮到你。
使用apache commons-net包实现文件ftp上传
Apache Commons是一个非常有用的工具包,解决各种实际的通用问题。(附件中提供了该工具包的jar包,及源文件以供研究) BeanUtils Commons-BeanUtils 提供对 Java 反射和自省API的包装 Betwixt Betwixt提供将 ...
NULL 博文链接:https://yangmeng.iteye.com/blog/2077964
包含commons-net-3.4.jar,commons-net-3.4-sources.jar,commons-net-examples-3.4.jar包,ftp上传主要依靠commons-net-3.4.jar
apache-jakarta旗下的所有开源...apache-jakarta-commons-net-ftp.jar apache-jakarta-oro.jar apache-jakarta-oro-2.0.8.jar commons-io-2.1-bin commons-logging-1.1.1-bin commons-modeler-2.0.1 commons-net-1.4.1
市面上的大部分jar包下载了都不可用,这个保证能用!包是二合一打包的! apache的FTP可以解决sun的ftp在操作过程中遇到中文会乱码的问题,用法大同小异,看API就会了!
使用JDK1.8、SpringBoot2.2.10.RELEASE、lombok1.18.8、guava23.0、hutool5.3.10、commons-pool2 2.7.0、tika1.22等实现多Ftp连接池实现,通过守护线程实现连接池内连接可用性校验,配置最大、最小连接个数防止Ftp...
jar包,亲测可用
Apache Commons Net库包含网络实用程序和协议实现的集合。 支持的协议包括:Echo,Finger,FTP,NNTP,NTP,POP3(S),SMTP(S),Telnet,Whois 文献资料 可以在上找到更多信息。 可以浏览 。 与Apache Commons ...
.net.ftp.FTPClient jar ,exaple, commons-net-2.0.jar
commons各种jar包下载 commons-beanutils-1.8.3.jar commons-codec-1.7.jar commons-collections-3.2.1.jar commons-dbcp-1.4.jar ...commons-net-ftp-2.0.jar commons-pool-1.3.jar common-utils.jar
Java apache-commons-net Ftp 进行文件、文件夹的上传下载及日志的输出(3.3版本),Android可用
Java apache-commons-net Ftp 进行文件、文件夹的上传下载及日志的输出(3.0.1版本),Android可用
Apache ftp上传所需jar包 commons-net-3.0.1.jar ftp4j-1.6.1.jar
上传了收集的最新的 commons-io-2.4.jar 包 和 commons-net-3.3.jar 以及 FTP java调用例子。net 包是一个用于操作Internet基础协议(Finger,Whois,TFTP,Telnet,POP3,FTP,NNTP,以及SMTP)的底层API。Net包...
NULL 博文链接:https://vakinge.iteye.com/blog/487486
org.apache.commons.net.ftp.FTPClient包,不错,在实际项目和产品中使用过。
org.apache.commons.net.ftp.FTPClient的最新jar报
org.apache.commons.net.ftp的官方完整jar包 放心使用