浏览器下载SFTP
String directory = "/opt/docs/content/certificate/AICH/";
String fileName1 = "MASS_READ_TEMPLATE1.xlsx";
String userName = "root";
String password = "centos21";
String host = "172.24.8.103";
int port = 22;
/**
*从SFTP上下载文件的方法,请求浏览器
*
* @param context
* FacesContext
* @param filePath
* 文件路径
* @param fileName
* 文件名
* @param userName
* 用户名
* @param password
* 密码
* @param host
* ip地址
* @param port
* 端口号
*/
public static void downAndReadSftpFileToExplorer(FacesContext context, String filePath, String fileName,
String userName, String password, String host, int port, String privateKey) throws Exception {
ExternalContext externalContext = context.getExternalContext();
byte[] buffer = downloadFileSftpToByte(userName, password, host, port, privateKey, filePath, fileName);
externalContext.responseReset();
externalContext.setResponseContentType("application/octet-stream");
externalContext.setResponseContentLength(buffer.length);
externalContext.setResponseHeader("Content-Disposition", "attachment;filename=" + new String(fileName));
try (OutputStream toClient = new BufferedOutputStream(externalContext.getResponseOutputStream())) {
toClient.write(buffer, 0, buffer.length);
toClient.flush();
} catch (IOException e) {
LogLogger.error(e);
throw e;
}
}
/**
* @Description: SFTP文件转Byte
* @param userName
* 用户名
* @param password
* 密码
* @param host
* ip地址
* @param port
* 端口号
* @param privateKey
* 私钥
* @param directory
* 文件路径
* @param downloadFile
* 文件名
* @Return: byte[]
* @Throws: Exception
*/
public static byte[] downloadFileSftpToByte(String userName, String password, String host, int port,
String privateKey, String directory, String downloadFile) throws Exception {
Session session = null;
ChannelSftp sftp = null;
//网关服务器SFTP
try {
JSch jsch = new JSch();
if (privateKey != null) {
// 密钥设置
jsch.addIdentity(privateKey);
}
session = jsch.getSession(userName, host, port);
if (password != null) {
session.setPassword(password);
}
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
} catch (JSchException e) {
e.printStackTrace();
}
if (null!=directory&&!"".equals(directory)) {
sftp.cd(directory);
}
InputStream is = sftp.get(downloadFile);
byte[] fileData = IOUtils.toByteArray(is);
return fileData;
}
客户端下载SFTP
/**
* @Description: 客户端下载SFTP文件
* @param userName
* 用户名
* @param password
* 密码
* @param host
* ip地址
* @param port
* 端口号
* @param privateKey
* 私钥
* @param directory
* 文件路径
* @param downloadFile
* 文件名
* @param saveFile
* 保存文件地址
* @Return: void
* @Throws: Exception
*/
public static void downloadFileToSftp(String userName, String password, String host, int port, String privateKey,
String directory, String downloadFile, String saveFile) throws Exception {
Session session = null;
ChannelSftp sftp = null;
// SFTP服务器
try {
JSch jsch = new JSch();
if (null!=privateKey) {
// 私钥
jsch.addIdentity(privateKey);
}
session = jsch.getSession(userName, host, port);
if (password != null) {
session.setPassword(password);
}
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
} catch (JSchException e) {
e.printStackTrace();
}
if (null!=directory && !"".equals(directory)) {
sftp.cd(directory);
}
File file = new File(saveFile);
sftp.get(downloadFile, new FileOutputStream(file));
}