java sftp文件上传核心代码
2015-11-19 15:20
465 查看
package sftp; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.util.Properties; import com.jcraft.jsch.Channel; import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.JSch; import com.jcraft.jsch.Session; import com.jcraft.jsch.SftpException; public class SFTPUtil { private String host = "172.30.104.40"; private String username="weblogic"; private String password="weblogic"; private int port = 22; private ChannelSftp sftp = null; private static final String linuxFileSeparator = "/"; public SFTPUtil(){} public SFTPUtil(String host, String username, String password, int port){ this.host = host; this.username = username; this.password = password; this.port = port; } /** * connect server via sftp */ public void connect() { try { if(sftp != null){ System.out.println("sftp is not null"); } JSch jsch = new JSch(); jsch.getSession(username, host, port); Session sshSession = jsch.getSession(username, host, port); System.out.println("Session created."); sshSession.setPassword(password); Properties sshConfig = new Properties(); sshConfig.put("StrictHostKeyChecking", "no"); sshSession.setConfig(sshConfig); sshSession.connect(); sshSession.setTimeout(1000*60*10); System.out.println("Session connected."); System.out.println("Opening Channel."); Channel channel = sshSession.openChannel("sftp"); channel.connect(); sftp = (ChannelSftp) channel; System.out.println("Connected to " + host + "."); } catch (Exception e) { e.printStackTrace(); } } /** * Disconnect with server */ public void disconnect() { if(this.sftp != null){ if(this.sftp.isConnected()){ this.sftp.disconnect(); }else if(this.sftp.isClosed()){ System.out.println("sftp is closed already"); } } } public void download(String remoteDirectory, String remoteDownloadFileName, String localSaveFilePath) { try { this.sftp.cd(remoteDirectory); File file = new File(localSaveFilePath); this.sftp.get(remoteDownloadFileName, new FileOutputStream(file)); System.out.println("download remote file[" + remoteDirectory + linuxFileSeparator + remoteDownloadFileName + "] to local " + localSaveFilePath); } catch (Exception e) { e.printStackTrace(); } } /** * upload one file to the server */ public void upload(String localFilePath, String remoteFileDirectory) { try { File file = new File(localFilePath); if(file.isFile()){ System.out.println("localFile : " + file.getAbsolutePath()); System.out.println("remotePath:" + remoteFileDirectory); this.sftp.cd(remoteFileDirectory); this.sftp.put(new FileInputStream(file), file.getName()); System.out.println("=========upload file for " + localFilePath); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (SftpException e) { e.printStackTrace(); } } /** * @return the host */ public String getHost() { return host; } /** * @param host the host to set */ public void setHost(String host) { this.host = host; } /** * @return the username */ public String getUsername() { return username; } /** * @param username the username to set */ public void setUsername(String username) { this.username = username; } /** * @return the password */ public String getPassword() { return password; } /** * @param password the password to set */ public void setPassword(String password) { this.password = password; } /** * @return the port */ public int getPort() { return port; } /** * @param port the port to set */ public void setPort(int port) { this.port = port; } /** * @return the sftp */ public ChannelSftp getSftp() { return sftp; } /** * @param sftp the sftp to set */ public void setSftp(ChannelSftp sftp) { this.sftp = sftp; } public static void main(String[] args) { SFTPUtil ftp= new SFTPUtil(); ftp.connect(); ftp.upload("c:\\RpStatisticsAmountVO.java", "/home/weblogic/job"); ftp.download("/home/weblogic/job", "RpStatisticsAmountVO.java", "d:\\RpStatisticsAmountVO.java"); ftp.disconnect(); } }
项目下载地址:http://download.csdn.net/download/qq_14946627/9282783
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树
- [原创]java局域网聊天系统