SFTP & FTP Upload
简述
>> 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:
假设ftpserver没有开通20。可是FTPserver开通了高位随机port,则必须使用被动模块,同一时候也能够使用主动模式。
假设客服端的防火墙关闭了port的主动接收功能,则无法使用主动模式。但能够使用被动模块,这也是被动模块存在的原因。
一般公司内部为了server安全点。使用主动模式,但对client有些要求,能够接受port的请求数据。
PORT(主动)方式的连接过程是:
client向server的FTPport(默认是21)发送连接请求,server接受连接。建立一条命令链路。
当须要传送数据时,client在命令链路上用PORT命令告诉server:“我打开了****port,你过来连接我”。
于是server从20port向client的****port发送连接请求,建立一条数据链路来传送数据。
PASV(被动)方式的连接过程是:
client向server的FTPport(默认是21)发送连接请求,server接受连接。建立一条命令链路。
当须要传送数据时。server在命令链路上用PASV命令告诉client:“我打开了****port,你过来连接我”。
于是client向server的****port发送连接请求。建立一条数据链路来传送数据。
从上面能够看出。两种方式的命令链路连接方法是一样的,而数据链路的建立方法就全然不同。
【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
SFTP & FTP Upload的更多相关文章
- Java使用SFTP和FTP两种连接方式实现对服务器的上传下载 【我改】
[]如何区分是需要使用SFTP还是FTP? []我觉得: 1.看是否已知私钥. SFTP 和 FTP 最主要的区别就是 SFTP 有私钥,也就是在创建连接对象时,SFTP 除了用户名和密码外还需要知道 ...
- 小白的linux笔记4:几种共享文件方式的速度测试——SFTP(SSH)/FTP/SMB
测试一下各个协议的速度,用一个7205M的centos的ISO文件上传下载.5Gwifi连接时,本地SSD(Y7000)对服务器的HDD: smb download 23M/s(资源管理器) smb ...
- Sftp和ftp 差别、工作原理等(汇总ing)
Sftp和ftp over ssh2的差别 近期使用SecureFx,涉及了两个不同的安全文件传输协议: -sftp -ftp over SSH2 这两种协议是不同的.sftp是ssh内含的协议,仅仅 ...
- 浅谈SFTP和FTP的区别
一.适用场景 我们平时习惯了使用ftp来上传下载文件,尤其是很多Linux环境下,我们一般都会通过第三方的SSH工具连接到Linux,但是当我们需要传输文件到Linux服务器当中,很多人习惯用ftp来 ...
- Linux 下上传下载命令,SCP,SFTP,FTP
scp 帮助命令: man scp scp功能: 下载远程文件或者目录到本地, 如果想上传或者想下载目录,最好的办法是采用tar压缩一下,是最明智的选择. 从远程主机 下载东西到 本地电脑 拷贝文件命 ...
- sftp与ftp的区别
SFTP和FTP非常相似,都支持批量传输(一次传输多个文件),文件夹/目录导航,文件移动,文件夹/目录创建,文件删除等.但还是存在着差异,下面我们来看看SFTP和FTP之间的区别. 1. 安全通道FT ...
- sftp,ftp文件下载
一.sftp工具类 package com.ztesoft.iotcmp.util; import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsc ...
- linux命令-sftp(模拟ftp服务)和scp(文件异地直接复制)
1)sftp sftp是模拟ftp的服务,使用22端口 针对远方服务器主机 (Server) 之行为 变换目录到 /etc/test 或其他目录 cd /etc/testcd PATH 列出目前所在目 ...
- Linux 远程连接sftp与ftp
linux sftp远程连接命令 sftp -oPort=60001 root@192.168.0.254 使用-o选项来指定端口号. -oPort=远程端口号 sftp> get /var/w ...
随机推荐
- ogre3D学习基础3 -- 粒子与表层脚本
9.粒子脚本 粒子脚本允许你实例化地在你的脚本代码中定义粒子系统,而不必在源代码中进行设置,使得你做任何修改都能得到快速回应.脚本里定义的粒子系统被用作模板,并且多个实际的系统可以在运行时从这里被创建 ...
- lucene.NET详细使用与优化详解
lucene.NET详细使用与优化详解 http://www.cnblogs.com/qq4004229/archive/2010/05/21/1741025.html http://www.shan ...
- 并发编程——多进程——multiprocessing开启进程的方式及其属性(3)
开启进程的两种方式——Process 方式一:函数方法 from multiprocessing import Process import time def task(name): print('% ...
- PHP共享内存的应用shmop系列
简单的说明 可能很少情况会使用PHP来操控共享内存,一方面在内存的控制上,MC已经提供了一套很好的方式,另一方面,自己来操控内存的难度较大,内存的读写与转存,包括后面可能会用到的存储策略,要是没有一定 ...
- [转]jQuery中attr() 和 val() 的区别
[转](http://www.codeproject.com/Tips/780652/Difference-between-attr-and-val-in-jQuery)
- HDU 2896 病毒侵袭(AC自动机水)
病毒侵袭 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- leetcode 27 水
class Solution { public: int removeElement(vector<int>& nums, int val) { int length=nums.s ...
- JavaScript 笔记(7) -- 在HTML中嵌入 js (外部引用)
本节主要说明,在HTML中嵌入自定义 JavaScript.通过HTML的script标签加载JavaScript文件 为防止网页加载缓慢,也可以把非关键的JavaScript放到网页底部,例如下面的 ...
- Java中Collections的sort方法和Comparable与Comparator的比较
一.Comparable 新建Student1类,类实现Comparable接口,并重写compareTo方法 public class Student1 implements Comparable& ...
- Func<T1, T2, TResult> Delegate 系统Func委托类型
原文发布时间为:2011-03-25 -- 来源于本人的百度文章 [由搬家工具导入] http://msdn.microsoft.com/en-us/library/bb534647%28v=VS.1 ...