Domanda

I am using this code to download file from server:

 import com.jcraft.jsch.Channel;
    import com.jcraft.jsch.ChannelSftp;
    import com.jcraft.jsch.JSch;
    import com.jcraft.jsch.Session;
    public class Main {
        public static void

 main(String[] args) {
        String host = "hostname";
        String login = "username";
        String password = "pass";
        int port = 22;
        String remoteDirectory = "directoryPath";
        String localDirectory = "directoryPath";


        Session session = null;
        Channel channel = null;
        ChannelSftp sftpChannel = null;

        try{
            JSch jsch = new JSch();
            session = jsch.getSession(login, host, port);
            session.setPassword(password);
            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
            session.connect();
            channel = session.openChannel("sftp");
            channel.connect();
            sftpChannel = (ChannelSftp)channel;
            sftpChannel.get(remoteDirectory, localDirectory);

        }
        catch (Exception ex) {
            ex.printStackTrace();
        }



    }

}

As you can see, username, password, host and port are hardcoded. I need to get these properties from settings.txt file which I have on my computer, but didn't succeed to make this possible.

È stato utile?

Soluzione

settings.txt to the root of your project and use this code to load it:

Properties properites = new Properties();
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();           
InputStream is = classLoader.getResourceAsStream("/settings.txt");
properties.load(is);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top