summaryrefslogtreecommitdiffstats
path: root/Dozentenmodul/src/downloader/jschtest.java
blob: 83be7e036b7a9629b0ee840d9d79cdeaf1e0f4ba (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package downloader;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Properties;

import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
import com.jcraft.jsch.UserInfo;

public class jschtest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		File testfile=new File("C:\\Users\\tspitzer\\Documents\\201301_ws_win7_OPT_Apps_NC_Off2010.zip");
		try {
			sftpFile(testfile, "fr-bwlehrpool-rw-admin" , "fefobu36", "bwsonas.lsdf.kit.edu", "/.mounts/bwlehrpool");
		} catch (FileNotFoundException | JSchException | SftpException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("Fertig");

	}

	private static void sftpFile(File localFile, String username, String password, String hostname, String remoteDirectory)
            throws JSchException, SftpException, FileNotFoundException {
 
        JSch jsch = new JSch();
        String filename = localFile.getName();
        Session session = jsch.getSession(username, hostname, 22);
        session.setUserInfo(new HardcodedUserInfo(password));
        Properties config = new Properties();
        config.setProperty("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.connect();
        ChannelSftp channel = (ChannelSftp)session.openChannel("sftp");
        channel.connect();
        channel.cd(remoteDirectory);
        channel.put(new FileInputStream(localFile), filename);
        channel.disconnect();
        session.disconnect();
    }
	
    private static class HardcodedUserInfo implements UserInfo {
    	 
        private final String password;
 
        private HardcodedUserInfo(String password) {
            this.password = password;
        }
 
        public String getPassphrase() {
            return null;
        }
 
        public String getPassword() {
            return password;
        }
 
        public boolean promptPassword(String s) {
            return true;
        }
 
        public boolean promptPassphrase(String s) {
            return true;
        }
 
        public boolean promptYesNo(String s) {
            return true;
        }
 
        public void showMessage(String s) {
            System.out.println("message = " + s);
        }
    }
 
}