I am trying to use localhost as SFTP server. I'm using Jsch library of Java to implement SFTP with SSH2. The following code upload a text file to a directory on local machine with sftp. But I cannot connect to localhost.
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
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;
public class Server{
public Server() {
}
public static void main(String[] args) {
String SFTPHOST = "localhost";
int SFTPPORT = 22;
String SFTPUSER = "root";
String SFTPPASS ="";
String SFTPWORKINGDIR = "D:\\Upload";
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("trial.txt");
channelSftp.put(new FileInputStream(f), f.getName());
}catch(Exception ex){
ex.printStackTrace();
}
}
}