Java远程连接Linux服务器并执行命令及上传文件功能
最近再开发中遇到需要将文件上传到Linux服务器上,至此整理代码笔记。
此种连接方法中有考虑到并发问题,在进行创建FTP连接的时候将每一个连接对象存放至
ThreadLocal
packagecom.test.utils; importjava.io.BufferedInputStream; importjava.io.File; importjava.io.FileFilter; importjava.io.FileInputStream; importjava.io.InputStream; importjava.util.ArrayList; importjava.util.Date; importjava.util.List; importorg.apache.commons.logging.Log; importorg.apache.commons.logging.LogFactory; importcom.jcraft.jsch.ChannelSftp; importcom.jcraft.jsch.JSch; importcom.jcraft.jsch.Session; publicclassFtp{ //打印log日志 privatestaticfinalLoglogger=LogFactory.getLog(Ftp.class); privatestaticDatelast_push_date=null; privateSessionsshSession; privateChannelSftpchannel; privatestaticThreadLocalsftpLocal=newThreadLocal (); privateFtp(Stringhost,intport,Stringusername,Stringpassword)throwsException{ JSchjsch=newJSch(); jsch.getSession(username,host,port); //根据用户名,密码,端口号获取session sshSession=jsch.getSession(username,host,port); sshSession.setPassword(password); //修改服务器/etc/ssh/sshd_config中GSSAPIAuthentication的值yes为no,解决用户不能远程登录 sshSession.setConfig("userauth.gssapi-with-mic","no"); //为session对象设置properties,第一次访问服务器时不用输入yes sshSession.setConfig("StrictHostKeyChecking","no"); sshSession.connect(); //获取sftp通道 channel=(ChannelSftp)sshSession.openChannel("sftp"); channel.connect(); logger.info("连接ftp成功!"+sshSession); } /** *是否已连接 * *@return */ privatebooleanisConnected(){ returnnull!=channel&&channel.isConnected(); } /** *获取本地线程存储的sftp客户端 * *@return *@throwsException */ publicstaticFtpgetSftpUtil(Stringhost,intport,Stringusername,Stringpassword)throwsException{ //获取本地线程 FtpsftpUtil=sftpLocal.get(); if(null==sftpUtil||!sftpUtil.isConnected()){ //将新连接防止本地线程,实现并发处理 sftpLocal.set(newFtp(host,port,username,password)); } returnsftpLocal.get(); } /** *释放本地线程存储的sftp客户端 */ publicstaticvoidrelease(){ if(null!=sftpLocal.get()){ sftpLocal.get().closeChannel(); logger.info("关闭连接"+sftpLocal.get().sshSession); sftpLocal.set(null); } } /** *关闭通道 * *@throwsException */ publicvoidcloseChannel(){ if(null!=channel){ try{ channel.disconnect(); }catch(Exceptione){ logger.error("关闭SFTP通道发生异常:",e); } } if(null!=sshSession){ try{ sshSession.disconnect(); }catch(Exceptione){ logger.error("SFTP关闭session异常:",e); } } } /** *@paramdirectory上传ftp的目录 *@paramuploadFile本地文件目录 * */ publicvoidupload(Stringdirectory,StringuploadFile)throwsException{ try{
//执行列表展示ls命令 channel.ls(directory);
//执行盘符切换cd命令 channel.cd(directory); Listfiles=getFiles(uploadFile,newArrayList ()); for(inti=0;i getFiles(Stringrealpath,List files){ FilerealFile=newFile(realpath); if(realFile.isDirectory()){ File[]subfiles=realFile.listFiles(newFileFilter(){ @Override publicbooleanaccept(Filefile){ if(null==last_push_date){ returntrue; }else{ longmodifyDate=file.lastModified(); returnmodifyDate>last_push_date.getTime(); } } }); for(Filefile:subfiles){ if(file.isDirectory()){ getFiles(file.getAbsolutePath(),files); }else{ files.add(file); } if(null==last_push_date){ last_push_date=newDate(file.lastModified()); }else{ longmodifyDate=file.lastModified(); if(modifyDate>last_push_date.getTime()){ last_push_date=newDate(modifyDate); } } } } returnfiles; } }
总结
以上所述是小编给大家介绍的Java远程连接Linux服务器并执行命令及上传文件,希望对大家有所帮助如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。