Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.OutputStream;
- import com.jcraft.jsch.Channel;
- import com.jcraft.jsch.ChannelSftp;
- import com.jcraft.jsch.JSch;
- import com.jcraft.jsch.Session;
- /**
- * @author https://kodehelp.com
- *
- */
- public class SFTPinJava {
- /**
- * @param args
- */
- public static void main(String[] args) {
- String SFTPHOST = "10.20.30.40";
- int SFTPPORT = 22;
- String SFTPUSER = "username";
- String SFTPPASS = "password";
- String SFTPWORKINGDIR = "/export/home/kodehelp/";
- Session session = null;
- Channel channel = null;
- ChannelSftp channelSftp = null;
- try {
- 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);
- File f = new File(FILETOTRANSFER);
- channelSftp.put(new FileInputStream(f), f.getName());
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement