1. 程式人生 > >java利用jcraft實現和遠端伺服器互動,實現上傳下載檔案

java利用jcraft實現和遠端伺服器互動,實現上傳下載檔案

git地址:https://github.com/fusugongzi/upLoadAndDownloadFile

第一步:引入maven支援,新增maven依賴

<!-- https://mvnrepository.com/artifact/com.jcraft/jsch -->
<dependency>
    <groupId>com.jcraft</groupId>
    <artifactId>jsch</artifactId>
    <version>0.1.54</version>
</dependency>

第二步:寫一個類sshconfiguration,作用是儲存要登入機器的host,port,username.pwd

public class SshConfiguration {
    private String host;
    private int    port;
    private String userName;
    private String password;
    public String getHost() {
        return host;
}

    public void setHost(String host) {
        this
.host = host; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword
() { return password; } public void setPassword(String password) { this.password = password; } public SshConfiguration(String host, int port, String userName, String password) { this.host = host; this.port = port; this.userName = userName; this.password = password; } public SshConfiguration(){} }

第三步:寫一個類sshutil,實現上傳下載

public class SshUtil {
    private ChannelSftp channelSftp;
    private ChannelExec channelExec;
    private Session session=null;
    private int timeout=60000;
    public SshUtil(SshConfiguration conf) throws JSchException {
        System.out.println("try connect to  "+conf.getHost()+",username: "+conf.getUserName()+",password: "+conf.getPassword()+",port: "+conf.getPort());
JSch jSch=new JSch(); //建立JSch物件
session=jSch.getSession(conf.getUserName(), conf.getHost(), conf.getPort());//根據使用者名稱,主機ip和埠獲取一個Session物件
session.setPassword(conf.getPassword()); //設定密碼
Properties config=new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);//Session物件設定properties
session.setTimeout(timeout);//設定超時
session.connect();//通過Session建立連線
}
    public void download(String src,String dst) throws JSchException, SftpException{
        //src linux伺服器檔案地址,dst 本地存放地址
channelSftp=(ChannelSftp) session.openChannel("sftp");
channelSftp.connect();
channelSftp.get(src, dst);
channelSftp.quit();
}
    public void upLoad(String src,String dst) throws JSchException,SftpException{
        //src 本機檔案地址。 dst 遠端檔案地址
channelSftp=(ChannelSftp) session.openChannel("sftp");
channelSftp.connect();
channelSftp.put(src, dst);
channelSftp.quit();
}
    public void close(){
        session.disconnect();
}
    public static void main(String[] args){
        SshConfiguration configuration=new SshConfiguration();
configuration.setHost("172.17.1.232");
configuration.setUserName("root");
configuration.setPassword("root275858");
configuration.setPort(22);
        try{
//            SshUtil sshUtil=new SshUtil(configuration);
//            sshUtil.download("/home/cafintech/Logs/metaData/meta.log","D://meta.log");
//            sshUtil.close();
//            System.out.println("檔案下載完成");
SshUtil sshUtil=new SshUtil(configuration);
sshUtil.upLoad("D://meta.log","/home/cafintech/");
sshUtil.close();
System.out.println("檔案上傳完成");
}catch(Exception e){
            e.printStackTrace();
}
    }
}

github地址:https://github.com/fusugongzi/upLoadAndDownloadFile