In my test case, SFTP is required as I need to verify the file is created. Anyway to perform SFTP download and upload?
Hi Jeff,
this is my code or downloading files from SFTP. I don’t use upload so I don’t have code for it.
I use 3rd party JSch library, you must add it into your project in Project - Settings - External libraries.
public class SFTPHandler { String SFTPHOST = "sftp.server.com"; int SFTPPORT = 22; String SFTPUSER = "admin"; String SFTPPASS = "password"; /** * @param ftpFolder - e.g. "/QA/test/" * @param ftpFileName - e.g. "myFile.txt" * @param absolutePathToLocalFile - a location for saving a file on local machine (including filename itself) e.g. "C:\\test\\myFile.txt" */ public void downloadRemoteFile(String ftpFolder, String ftpFileName, File localFile) { long ts = System.currentTimeMillis() / 1000L String SFTPWORKINGDIR = ftpFolder; Session session = null; Channel channel = null; ChannelSftp channelSftp = null; JSch jsch = new JSch(); session = jsch.getSession(SFTPUSER, SFTPHOST, SFTPPORT); session.setPassword(SFTPPASS); java.util.Properties config = new java.util.Properties(); config.put("StrictHostKeyChecking", "no"); session.setConfig(config); session.connect(); channel = session.openChannel("sftp"); channel.connect(); channelSftp = (ChannelSftp) channel; channelSftp.cd(SFTPWORKINGDIR); byte[] buffer = new byte[1024]; BufferedInputStream bis = new BufferedInputStream(channelSftp.get(ftpFileName)); OutputStream os = new FileOutputStream(localFile); BufferedOutputStream bos = new BufferedOutputStream(os); int readCount; while ((readCount = bis.read(buffer)) > 0) { System.out.println("Writing: "); bos.write(buffer, 0, readCount); } bis.close(); bos.close(); channel.disconnect() session.disconnect() }}
1 Like