1.sftp登陆命令 sftp [email protected] ---账号@ip 默认端口22
sftp -oPort=8080 [email protected] ---指定端口,账号@ip
2.nginx 代理
nginx重启 sudo ../sbin/nginx -s reload
netstat -nao|grep 8080
nginx.conf添加
worker_processes 1;
error_log logs/error.log info;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
include healcheck.conf;
default_type application/octet-stream;
log_format main '$remote_addr|$remote_user|[$time_local]|"$request"'
'|$status|$request_time|$body_bytes_sent|"$http_referer"'
'|"$http_user_agent"|"$http_x_forwarded_for"|"$upstream_response_time"';
access_log logs/access.log main;
server_tokens off;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 60;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
gzip_proxied any;
upstream temple {
sticky;
server 10.202.36.59:22;
server 10.202.53.27:22;
check interval=3000 rise=2 fall=5 timeout=1000 type=http;
check_http_send "HEAD /mwmonitor/check.jsp HTTP/1.0\r\n\r\n";
check_http_expect_alive http_2xx;
}
server {
listen 8081;
server_name localhost;
access_log logs/access.log main;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://temple;
root html;
index index.html index.htm;
}
location /check {
root html;
index index.html;
}
location = /stats {
stub_status on;
access_log off;
allow 10.0.0.0/8;
deny all;
}
location = /nstatus {
stub_status on;
check_status;
access_log off;
allow 10.0.0.0/8;
deny all;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
stream {
upstream sftp {
hash $remote_addr consistent;
server 10.202.53.27:22 max_fails=3 fail_timeout=30s;
server 10.202.36.59:22 max_fails=3 fail_timeout=30s;
}
server {
listen 8080;
proxy_connect_timeout 100s;
proxy_timeout 300s;
proxy_pass sftp;
}
}
3.java代码访问ftp 及sftp
public static void main(String[] args) throws JSchException, SftpException {
Session session = getSftpSession(ftpHostProNginx1, ftpHostProNginxport, ftpUserName, password, privateKey);
ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
channelSftp.connect();
Vector vector = channelSftp.ls("/ebillapi");
channelSftp.cd("ebillapi");
for (Object obj : vector) {
if (obj instanceof com.jcraft.jsch.ChannelSftp.LsEntry) {
String fileName = ((com.jcraft.jsch.ChannelSftp.LsEntry) obj).getFilename();
System.out.println(fileName);
}
}
String remoteFilepath = "rule3.txt";
String localFilePath = "G:/nfsc/rule.txt";
try {
//channelSftp.mkdir("testDir");
//channelSftp.rm("rule3.txt");
//channelSftp.put(localFilePath, remoteFilepath);
} finally {
channelSftp.quit();
channelSftp.disconnect();
session.disconnect();
}
}
public static void uploadSftpFile(String ftpHost, int ftpPort, String ftpUserName, String remoteFilepath,
String localFilePath, String privateKey) throws JSchException, SftpException {
Session session = getSftpSession(ftpHost, ftpPort, ftpUserName, null, privateKey);
uploadSftpFile(remoteFilepath, localFilePath, session);
}
public static void uploadSftpFile(int ftpPort, String ftpHost, String ftpUserName, String remoteFilepath,
String localFilePath, String password) throws JSchException, SftpException {
Session session = getSftpSession(ftpHost, ftpPort, ftpUserName, password, null);
uploadSftpFile(remoteFilepath, localFilePath, session);
}
private static void uploadSftpFile(String remoteFilepath, String localFilePath, Session session)
throws JSchException, SftpException {
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp chSftp = (ChannelSftp) channel;
try {
chSftp.put(localFilePath, remoteFilepath);
} finally {
chSftp.quit();
channel.disconnect();
session.disconnect();
}
}
public static void uploadFtpFile(int ftpPort, String ftpHost, String ftpUserName, String remoteFilepath,
String localFilePath, String password) throws JSchException, SftpException, IOException {
FTPClient ftpClient = null;
InputStream in = null;
try {
ftpClient = getFtpClient(ftpHost, ftpPort, ftpUserName, password);
ftpClient.setControlEncoding("UTF-8"); // 中文支持
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
in = new FileInputStream(localFilePath);
boolean storeFile = ftpClient.storeFile(remoteFilepath, in);
if (!storeFile) {
in.close();
ftpClient.disconnect();
throw new RuntimeException("-999,upload failed!");
}
} finally {
if (in != null)
in.close();
if (ftpClient != null)
ftpClient.disconnect();
}
}
/**
* 通过sftp下载文件数据
*
* @param ftpHost
* @param ftpUserName
* @param ftpPort
* @param remoteFile
* @param localFilePath
* @throws JSchException
* @throws SftpException
*/
public static void downloadSftpFile(String ftpHost, int ftpPort, String ftpUserName, String remoteFilepath,
String localFilePath, String privateKey) throws JSchException, SftpException {
Session session = getSftpSession(ftpHost, ftpPort, ftpUserName, null, privateKey);
downloadSftpFile(remoteFilepath, localFilePath, session);
}
public static void downloadSftpFile(int ftpPort, String ftpHost, String ftpUserName, String remoteFilepath,
String localFilePath, String password) throws JSchException, SftpException {
Session session = getSftpSession(ftpHost, ftpPort, ftpUserName, password, null);
downloadSftpFile(remoteFilepath, localFilePath, session);
}
/**
* 通过ftp下载文件数据
*
* @param ftpPort
* @param ftpHost
* @param ftpUserName
* @param remoteFilepath
* @param localFilePath
* @param password
* @throws JSchException
* @throws SftpException
* @throws IOException
*/
public static void downloadFtpFile(int ftpPort, String ftpHost, String ftpUserName, String remoteFilepath,
String localFilePath, String password) throws JSchException, SftpException, IOException {
FTPClient ftpClient = null;
OutputStream out = null;
try {
ftpClient = getFtpClient(ftpHost, ftpPort, ftpUserName, password);
FTPFile[] listFiles = ftpClient.listFiles(remoteFilepath);
if (listFiles.length == 0) {
throw new RuntimeException(String.format("下载文件不存在,文件路径:%s", remoteFilepath));
}
out = new FileOutputStream(localFilePath);
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.retrieveFile(remoteFilepath, out);
} finally {
if (out != null)
out.close();
if (ftpClient != null)
ftpClient.disconnect();
}
}
/**
* 下载文件
*/
private static void downloadSftpFile(String remoteFilepath, String localFilePath, Session session)
throws JSchException, SftpException {
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp chSftp = (ChannelSftp) channel;
try {
if (remoteFilepath.startsWith("/")) {
chSftp.cd("/");
}
chSftp.get(remoteFilepath, localFilePath);
} finally {
chSftp.quit();
channel.disconnect();
session.disconnect();
}
}
/**
* 获取sftp连接session
*/
private static Session getSftpSession(String ftpHost, int ftpPort, String ftpUserName, String password,
String privateKey) throws JSchException {
JSch jsch = new JSch();
if (StringUtils.isNotBlank(privateKey)) {
jsch.addIdentity(privateKey);
}
Session session = jsch.getSession(ftpUserName, ftpHost, ftpPort);
session.setTimeout(100000);
if (StringUtils.isNotBlank(password)) {
session.setPassword(password);
}
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
return session;
}
/**
* 获取ftp客服端
*/
private static FTPClient getFtpClient(String ftpHost, int ftpPort, String ftpUserName, String password)
throws IOException {
FTPClient ftpClient = new FTPClient();
ftpClient.connect(ftpHost, ftpPort);
ftpClient.login(ftpUserName, password);
int reply = ftpClient.getReplyCode();
ftpClient.setDataTimeout(60000);
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
throw new RuntimeException("-999,FTP server refuse connect!");
}
return ftpClient;
}