JSch jsch = new JSch();
ChannelSftp channelSftp = null;
Session session = null;
try{
session = jsch.getSession(userName, host, port);
session.setPassword(password);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
channelSftp = (ChannelSftp)session.openChannel("sftp");
channelSftp.connect();
channelSftp.cd(dirTransactions);
OutputStream outputStream = channelSftp.put(fileName);
outputStream.write(content.getBytes());
outputStream.flush();
outputStream.close();
} catch (JSchException e) {
e.printStack();
} catch (SftpException e) {
e.printStack();
} catch (IOException e) {
e.printStack();
} finally{
if (channelSftp != null)
channelSftp.quit();
if (session != null)
session.disconnect();
}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
channelSftp.get(dir + fileName, outputStream);
content = new String(outputStream.toByteArray());
本文提供了一个使用JSch库通过SFTP进行文件上传和下载的Java示例代码。示例展示了如何建立SFTP连接、上传文件到远程服务器及从服务器下载文件。
6200

被折叠的 条评论
为什么被折叠?



