java 代码链接SFTP,上传下载
2016-07-21 16:41
429 查看
依赖jar包:
<dependency> <groupId>com.jcraft</groupId> <artifactId>jsch</artifactId> <version>0.1.53</version> </dependency>
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.Properties; import com.haokuaisheng.log.LogManager; import com.haokuaisheng.sys.SystemUtils; import com.haokuaisheng.sys.SystemUtils.SystemType; import com.jcraft.jsch.Channel; import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.JSch; import com.jcraft.jsch.Session; public class SftpUtils { protected String host; protected int port; protected String username; protected String password; /** * @param host ip * @param port 端口 * @param username 账号 * @param password 密码 * */ public SftpUtils(String host, int port, String username, String password) { set(host, port, username, password); } public void set(String host, int port, String username, String password) { this.host = host; this.port = port; this.username = username; this.password = password; } Session sshSession = null ; /** * 链接linux * */ public ChannelSftp connect() { ChannelSftp sftp = null ; try { JSch jsch = new JSch(); jsch.getSession(username, host, port); sshSession = jsch.getSession(username, host, port); sshSession.setPassword(password); Properties sshConfig = new Properties(); sshConfig.put("StrictHostKeyChecking", "no"); sshSession.setConfig(sshConfig); sshSession.connect(); LogManager.info(String.format("%s connect success" , host)); Channel channel = sshSession.openChannel("sftp"); channel.connect() ; sftp = (ChannelSftp) channel; } catch (Exception e) { LogManager.err("connect:" + host , e ); close( null ); return null ; } return sftp; } /** * linux上传文件 * */ public void upload(String directory,File file){ ChannelSftp sftp = connect() ; try { if(null != sftp){ sftp.cd(directory); LogManager.info(String.format("cd %s" , directory)); sftp.put(new FileInputStream(file), file.getName()); } } catch (Exception e) { LogManager.err("upload:" + host , e ); }finally{ sftp.disconnect() ; sftp.exit(); sshSession.disconnect(); } } /** * linux下载文件 * */ public void get(String directory, String downloadFile) { ChannelSftp sftp = connect() ; try { if(null != sftp){ File file = new File( directory ) ; String parent = getParent( file ); sftp.cd( parent ); File desc = new File(downloadFile); FileOutputStream outputStream = new FileOutputStream(desc); sftp.get(file.getName() , outputStream); LogManager.info(String.format("down %s suc" , directory)); outputStream.close() ; } } catch (Exception e) { LogManager.err("down error :" + directory , e ); }finally{ close(sftp); } } protected void close(ChannelSftp sftp){ if(sftp!=null){ sftp.disconnect() ; sftp.exit(); } if(sshSession!=null){ sshSession.disconnect(); } } protected String getParent(File desc){ if(SystemUtils.getSystemType() == SystemType.WINDOWS){ String parent = desc.getParent(); return parent.replace("\\", "/"); } return desc.getParent() ; } public static void main(String[] args) { /** * @param host ip * @param port 端口 * @param username 账号 * @param password 密码 * */ SftpUtils sftpUtils = new SftpUtils("192.168.1.21" , 22 , "root" , "123456") ; sftpUtils.upload("/opt/datas/uploads", new File("c://config.ini")); sftpUtils.get("/opt/datas/uploads/config.ini" , "c://1.ini"); } }
相关文章推荐
- JAVA用画板JFrame画布Panel画笔Graphics画一个星星,可拖动-3
- 今天看看Java反射
- 装饰设计模式的演示
- JAVA IO包的整理---------Exception
- 谈谈对三大框架的理解
- Java-HashMap详解
- JAVA用画板JFrame画布Panel画笔Graphics画100个星星可以任意拖拽-2
- Spring定时任务之Quartz
- 纯java JJIL脸部识别人像识别
- 能够提高开发效率的Eclipse实用操作
- Spring和MyBatis环境整合
- Spring JMS ActiveMQ整合(转)
- JAVA设计模式---命令模式
- java和json互转
- Spring MVC之@RequestBody@ResponseBody详解
- java中如何停止线程
- java POI 实现合并单元格
- java语言基础入门——继承及多态
- Java Cookie工具类代码
- java directmemory监控