标题 sftp下载服务器文件
package com.sinosoft.ireport.jdbcMysql;
import com.jcraft.jsch.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Properties;
public class SftpUtil {
private static String host = "XXX";
private static int port = XX;
private static String userName = "XXX";
private static String password = "XXX";
private static Session sshSession;
public static ChannelSftp sftpConnection(String host, int port, String userName, String password) {
JSch jsch = new JSch();
ChannelSftp channelSftp = new ChannelSftp();
try {
jsch.getSession(userName, host, port);
sshSession = jsch.getSession(userName, host, port);
sshSession.setPassword(password);
Properties properties = new Properties();
properties.put("StrictHostKeyChecking", "no");
sshSession.setConfig(properties);
sshSession.connect();
Channel channel = sshSession.openChannel("sftp");
channel.connect();
channelSftp = (ChannelSftp) channel;
System.out.println("Sftp服务器登录成功!");
} catch (Exception e) {
e.printStackTrace();
System.out.println("Sftp服务器登录异常!");
}
return channelSftp;
}
public static void sftpClose(ChannelSftp channelSftp) {
if (channelSftp != null) {
if (channelSftp.isConnected()) {
channelSftp.disconnect();
}
}
}
public static void sessionClose() {
if (sshSession != null) {
if (sshSession.isConnected()) {
sshSession.disconnect();
sshSession = null;
}
}
}
public static void downSftpFile(String newFileName, String path, String fileName, String downUrl) {
ChannelSftp channelSftp = new ChannelSftp();
try {
channelSftp = sftpConnection(host, port, userName, password);
downSftpFile(channelSftp, newFileName, path, fileName, downUrl);
} catch (Exception e) {
e.printStackTrace();
} finally {
sftpClose(channelSftp);
sessionClose();
}
}
private static void downSftpFile(ChannelSftp sftp, String newFileName, String path, String fileName, String downUrl) throws Exception {
OutputStream os = null;
try {
File localFile = new File(downUrl + "/" + newFileName);
if (!localFile.getParentFile().exists()) {
localFile.getParentFile().mkdirs();
localFile.createNewFile();
}
if (path != null && !"".equals(path)) {
sftp.cd(path);
}
os = new FileOutputStream(localFile);
sftp.get(path + fileName, os);
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static boolean delete(String directory, String deleteFile) {
boolean result = false;
ChannelSftp sftp = sftpConnection(host, port, userName, password);
try {
sftp.cd(directory);
sftp.rm(deleteFile);
} catch (SftpException e) {
e.printStackTrace();
} finally {
sftpClose(sftp);
}
result = true;
return result;
}
public static void main(String[] args) {
host = "";
port = ;
userName = "";
password = ";
ChannelSftp channelSftp = sftpConnection(host, port, userName, password);
String newFileName = "hahah2021031815305001.xls";
String downUrl = "C:\\\\111";
String path = "/u01/report/result/";
String fileName = "2021031815305001.xls";
try {
downSftpFile(channelSftp, newFileName, path, fileName, downUrl);
} catch (Exception e) {
e.printStackTrace();
} finally {
sftpClose(channelSftp);
sessionClose();
}
}
}