SFTP & FTP Upload

简介: <p><span style="font-family:KaiTi_GB2312; font-size:18px">简述</span></p> <p><span style="font-family:'Courier New'"><span style="font-size:14px">>> FTP:</span></span></p> <span style="font-

简述

>> 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)) {
					// Add localFile into localFileSet after upload successfully, and will delete these local files
					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> localFileCollection = FileUtils.listFiles(localDirectory, new String[] { "gz", "GZ", "xml", "XML", "zip", "ZIP" }, true);

		Set<File> localFileSet = FtpUpload.upload(localFileCollection);

		delete(localFileSet);
	}
}

【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();
	}
}

【Shell FTP Upload】

#!/bin/bash

HOST="192.168.0.1"
USER="ftpuser"
PASS="ftpPassword"

LOCAL_DIR="/home/tmp/local"
REMOTE_DIR="/home/<ftp-user>/remote"

ftp -v -n << EOF
open ${HOST}
user ${USER} ${PASS}
prompt off
passive off
binary
lcd ${LOCAL_DIR}
cd ${REMOTE_DIR}
mput *
close
bye
EOF


相关文章
|
安全 Unix Linux
Xftp 7(FTP/SFTP客户端) V7.0.0107 官方中文免费正式版(附文件+安装教程)
Xftp 7(FTP/SFTP客户端) V7.0.0107 官方中文免费正式版(附文件+安装教程)
9045 0
Xftp 7(FTP/SFTP客户端) V7.0.0107 官方中文免费正式版(附文件+安装教程)
|
大数据 开发工具 数据安全/隐私保护
大数据基本开发工具的FTP/SFTP工具的Transmit
在大数据开发中,FTP和SFTP工具是必不可少的。其中Transmit是一款功能强大的FTP/SFTP客户端,可以帮助开发者高效管理传输文件。本文将介绍Transmit的特点,安装和使用方法。
388 0
|
算法 大数据 Linux
大数据基本开发工具的FTP/SFTP工具的FileZilla
大数据是当今的热门话题,而在大数据的开发过程中,FTP/SFTP工具是非常重要的一个环节。本文将介绍一款优秀的FTP/SFTP工具——FileZilla。
237 0
|
存储 大数据 网络安全
大数据基本开发工具的FTP/SFTP工具的WinSCP
在大数据开发中,FTP/SFTP工具是必不可少的工具之一。WinSCP是一个功能丰富的FTP/SFTP客户端,它可以让您方便地上传和下载大量数据,支持多种协议和加密算法。本文将会介绍如何在WinSCP中使用FTP/SFTP工具进行大数据开发。
339 0
WGCLOUD v3.4.3从入门到精通(zabbix使用详解) 监测FTP和SFTP教程
我们在左侧菜单点击【FTP/SFTP监测】,可以看到如下列表
WGCLOUD v3.4.3从入门到精通(zabbix使用详解) 监测FTP和SFTP教程
|
搜索推荐 网络安全 C#
一个Windows远程工具,小巧但实用,支持RDP、SSH、SFTP、FTP等多种协议
这是一个C#开发的Windows远程桌面开源项目,它支持RDP、SSH、VNC、Telnet、(S)FTP、RemoteApp、NoMachine和其他应用。
879 0
一个Windows远程工具,小巧但实用,支持RDP、SSH、SFTP、FTP等多种协议
|
Shell 数据安全/隐私保护 网络安全
shell使用lftp连接ftp和sftp,并可以指定私钥
lftp连接ftp在脚本中可以 lftp -c "open username:password@host:port; ls /Friso/20180822/click/mobile/SUCCESS | wc -l" lftp usename:password@host:port -e "ls /Fr...
4026 0
|
数据安全/隐私保护 Python