简述
>> FTP:
1. Install FTP service on Linux(Red Hat) as root user
[root]# yum install ftp
2. Configure FTP as root user
a) Be clear with below properties, and configure them in the file /etc/vsftpd/vsftpd.conf as root user
# Uncomment this to allow local users to log in.
local_enable=YES
# Uncomment this to enable any form of FTP write command.
write_enable=YES
# Default umask for local users is 077. You may wish to change this to 022,
# if your users expect that (022 is used by most other ftpd‘s)
local_umask=022
# When "listen" directive is enabled, vsftpd runs in standalone mode and
# listens on IPv4 sockets. This directive cannot be used in conjunction
# with the listen_ipv6 directive.
listen=YES
userlist_enable=YES
userlist_deny=NO
#Passive mode not allowed
pasv_enable=NO
# Make sure PORT transfer connections originate from port 20 (ftp-data).
connect_from_port_20=YES
#Specify listen port
listen_port=21
#Allow active mode
port_enable=YES
b) Make sure ftp user exists in the file /etc/vsftpd/user_list as root user
3. Restart FTP service as root user
[root]# service vsftpd restart
>> SFTP:
Generally SFTP service is installed/configured on Linux OS
>> Please refer to below info:
如果ftp服务器没有开通20,但是FTP服务器开通了高位随机端口,则必须使用被动模块,同时也可以使用主动模式。
如果客服端的防火墙关闭了端口的主动接收功能,则无法使用主动模式,但可以使用被动模块,这也是被动模块存在的原因。
一般公司内部为了服务器安全点,使用主动模式,但对客户端有些要求,可以接受端口的请求数据。
PORT(主动)方式的连接过程是:
客户端向服务器的FTP端口(默认是21)发送连接请求,服务器接受连接,建立一条命令链路。
当需要传送数据时,客户端在命令链路上用PORT命令告诉服务器:“我打开了****端口,你过来连接我”。
于是服务器从20端口向客户端的****端口发送连接请求,建立一条数据链路来传送数据。
PASV(被动)方式的连接过程是:
客户端向服务器的FTP端口(默认是21)发送连接请求,服务器接受连接,建立一条命令链路。
当需要传送数据时,服务器在命令链路上用PASV命令告诉客户端:“我打开了****端口,你过来连接我”。
于是客户端向服务器的****端口发送连接请求,建立一条数据链路来传送数据。
从上面可以看出,两种方式的命令链路连接方法是一样的,而数据链路的建立方法就完全不同。
【FTP Upload】
package shuai.study.ftp; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Collection; import java.util.HashSet; import java.util.Iterator; import java.util.Set; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.net.ftp.FTPClient; import org.apache.log4j.LogManager; import org.apache.log4j.Logger; import org.apache.log4j.xml.DOMConfigurator; /** * @Description: FTP Upload * @author Zhou Shengshuai * @date 2014年8月5日 下午2:37:57 * */ public class FtpUpload { private static final Logger logger = LogManager.getLogger(FtpUpload.class); static { System.setProperty("FTP_LOG", "/home/tmp/log"); DOMConfigurator.configure("/home/tmp/configuration/log4j.xml"); } private static String hostname = "192.168.0.1"; private static int ftpPort = 21; private static String ftpUsername = "ftpuser"; private static String ftpPassword = "ftpPassword"; private static String remoteDirectoryString = "/upload"; private static File localDirectory = new File("/home/tmp/local"); public static Set<File> upload(Collection<File> localFileCollection) { Set<File> localFileSet = new HashSet<File>(); FTPClient ftpClient = connect(hostname, ftpPort, ftpUsername, ftpPassword, remoteDirectoryString); if (ftpClient != null && ftpClient.isConnected()) { Iterator<File> localFileIterator = localFileCollection.iterator(); while (localFileIterator.hasNext()) { File localFile = localFileIterator.next(); if (upload(ftpClient, localFile)) { localFileSet.add(localFile); } } } disconnect(ftpClient); return localFileSet; } private static boolean upload(FTPClient ftpClient, File localFile) { boolean storeFileFlag = false; if (localFile != null && localFile.isFile()) { InputStream fileInputStream = null; try { fileInputStream = new FileInputStream(localFile); storeFileFlag = ftpClient.storeFile(localFile.getName(), fileInputStream); if (storeFileFlag) { logger.info("Upload local file " + localFile + " to remote directory " + ftpUsername + "@" + hostname + ":" + remoteDirectoryString + " via FTP"); } else { logger.error("Fail to upload local file " + localFile + " to remote directory " + ftpUsername + "@" + hostname + ":" + remoteDirectoryString + " via FTP"); } } catch (IOException ioe) { logger.error("FTP Upload Exception", ioe); } finally { IOUtils.closeQuietly(fileInputStream); } } return storeFileFlag; } private static FTPClient connect(String hostname, int ftpPort, String ftpUsername, String ftpPassword, String remoteDirectoryString) { FTPClient ftpClient = new FTPClient(); try { ftpClient.connect(hostname, ftpPort); logger.info("FTP Connected " + hostname + ":" + ftpPort); ftpClient.login(ftpUsername, ftpPassword); logger.info("FTP Logined as " + ftpUsername); // ftpClient.enterLocalPassiveMode(); // if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) { // logger.error("FTP Response Unsuccessfully"); // disconnect(ftpClient); // } if (ftpClient.makeDirectory(remoteDirectoryString)) { logger.info("FTP Directory Made " + remoteDirectoryString); } ftpClient.changeWorkingDirectory(remoteDirectoryString); logger.info("FTP Working Directory Changed as " + remoteDirectoryString); // ftpClient.setBufferSize(1024); // ftpClient.setControlEncoding("UTF-8"); ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); ftpClient.setFileTransferMode(FTPClient.BINARY_FILE_TYPE); } catch (IOException ioe) { logger.error("FTP Connection Exception", ioe); } return ftpClient; } private static void disconnect(FTPClient ftpClient) { if (ftpClient != null) { try { ftpClient.logout(); ftpClient.disconnect(); } catch (IOException ioe) { logger.error("FTP Disconnection Exception", ioe); } } logger.info("FTP Disconnected"); } private static void delete(Set<File> exportCucFileSet) { Iterator<File> exportCucFileIterator = exportCucFileSet.iterator(); while (exportCucFileIterator.hasNext()) { File exportCucFile = exportCucFileIterator.next(); if (FileUtils.deleteQuietly(exportCucFile)) { logger.info("Delete local file " + exportCucFile + " after FTP upload"); } else { logger.error("Fail to delete local file " + exportCucFile + " after FTP upload"); } } } public static void main(String[] args) { Collection<File> exportCucFileCollection = FileUtils.listFiles(localDirectory, new String[] { "gz", "GZ", "xml", "XML", "zip", "ZIP" }, true); Set<File> exportCucFileSet = FtpUpload.upload(exportCucFileCollection); delete(exportCucFileSet); } }
【SFTP Upload】
package shuai.study.sftp; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.Collection; import java.util.Iterator; import java.util.Properties; import org.apache.commons.io.FileUtils; import org.apache.log4j.LogManager; import org.apache.log4j.Logger; import org.apache.log4j.xml.DOMConfigurator; import com.jcraft.jsch.Channel; import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.JSch; import com.jcraft.jsch.JSchException; import com.jcraft.jsch.Session; import com.jcraft.jsch.SftpException; /** * @Description: SFTP Upload * @author Zhou Shengshuai * @date 2014年8月1日 下午2:37:57 * */ public class SftpUpload { private static final Logger logger = LogManager.getLogger(SftpUpload.class); static { System.setProperty("FTP_LOG", "/home/tmp/log"); DOMConfigurator.configure("/home/tmp/configuration/log4j.xml"); } private static String hostname = "192.168.0.1"; private static int sftpPort = 22; private static String sftpUsername = "ftpuser"; private static String sftpPassword = "ftpPassword"; private static String remoteDirectoryString = "/upload"; private static String localDirectoryString = "/home/tmp/local"; /** * @Description: Upload */ public static void upload() { File localDirectory = new File(localDirectoryString); if (localDirectory.exists()) { ChannelSftp sftp = connect(hostname, sftpPort, sftpUsername, sftpPassword); Collection<File> localFileCollection = FileUtils.listFiles(localDirectory, new String[] { "gz", "GZ", "xml", "XML", "zip", "ZIP" }, true); Iterator<File> localFileIterator = localFileCollection.iterator(); while (localFileIterator.hasNext()) { File localFile = localFileIterator.next(); logger.info("Local File: " + localFile); if (sftp != null && sftp.isConnected()) { upload(remoteDirectoryString, localFile, sftp); } } disconnect(sftp); } else { logger.warn("The Local Directory " + localDirectoryString + " doesn't exist"); } } /** * @Description: SFTP Upload * * @param remoteDirectoryString * Remote CM Directory * @param localFile * Local CM File * @param sftp * ChannelSftp */ public static void upload(String remoteDirectoryString, File localFile, ChannelSftp sftp) { logger.info("Remote Directory: " + remoteDirectoryString); try { sftp.cd(remoteDirectoryString); sftp.put(new FileInputStream(localFile), localFile.getName()); FileUtils.deleteQuietly(localFile); logger.info("Upload Local File " + localFile + " via SFTP"); } catch (FileNotFoundException fnfe) { logger.error("File Not Found Exception", fnfe); } catch (SftpException se) { logger.error("SFTP Upload Exception", se); } } /** * @Description: SFTP Connection * * @param hostname * SFTP HOST * @param sftpPort * SFTP PORT * @param sftpUsername * SFTP USERNAME * @param sftpPassword * SFTP PASSWORD * * @return ChannelSftp */ public static ChannelSftp connect(String hostname, int sftpPort, String sftpUsername, String sftpPassword) { JSch jsch = new JSch(); Properties sshConfig = new Properties(); Channel channel = null; try { Session session = jsch.getSession(sftpUsername, hostname, sftpPort); logger.info("Session Created"); session.setPassword(sftpPassword); sshConfig.put("StrictHostKeyChecking", "no"); session.setConfig(sshConfig); // session.setTimeout(60000); // session.setServerAliveInterval(90000); session.connect(); logger.info("Session Connected"); channel = session.openChannel("sftp"); logger.info("Channel Opened"); channel.connect(); logger.info("Channel Connected"); } catch (JSchException je) { logger.error("SFTP Exception", je); } return (ChannelSftp) channel; } /** * @Description: Disconnect SFTP * * @param sftp * ChannelSftp */ public static void disconnect(ChannelSftp sftp) { if (sftp != null) { try { sftp.getSession().disconnect(); } catch (JSchException je) { logger.error("SFTP Disconnect Exception", je); } sftp.disconnect(); } } /** * @Description: Main Thread * @param args */ public static void main(String[] args) { SftpUpload.upload(); } }
SFTP & FTP Upload