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