标签:
java类:
需要引用的jar:
package com.isoftstone.www.ftp; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.Properties; import java.util.Vector; 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 SFTPTooL { /** * 1. 第一步:连接sftp服务器,先获取Session * * @param host * 主机 * @param port * 端口 * @param username * 用户名 * @param password * 密码 * @return */ public Session getSession(String host, int port, String username,String password) { Session session = null; try { JSch jsch = new JSch(); session = jsch.getSession(username, host, port); System.out.println("Session created."); session.setPassword(password); Properties sshConfig = new Properties(); sshConfig.put("StrictHostKeyChecking", "no"); session.setConfig(sshConfig); session.connect(); System.out.println("Session connected."); } catch (Exception e) { e.printStackTrace(); if (session!= null && session.isConnected()){ session.disconnect(); } } return session; } /** * 2.第二步: 连接sftp服务器,再获取链接 * @return */ public ChannelSftp getConnect(Session session) { ChannelSftp sftp = null; try { if(session == null){ System.out.println("Can‘t Create Connect,Because session is null"); return sftp; } Channel channel = session.openChannel("sftp"); System.out.println("Opening Channel."); channel.connect(); sftp = (ChannelSftp) channel; System.out.println("Connected to " + session.getHost()+":"+session.getPort()); } catch (Exception e) { e.printStackTrace(); if (sftp!= null && sftp.isConnected()){ sftp.disconnect(); } } return sftp; } /** * 3.第3步:先关闭 channel关链接 * @param channel */ public void disconnect(Channel channel) { try { if (channel!= null && channel.isConnected()){ channel.disconnect(); System.out.println("Disconnected channel"); } } catch (Exception e) { e.printStackTrace(); } } /** * 4.第四步:最后关闭Session * @param session */ public void disconnect(Session session) { try { if (session!= null && session.isConnected()){ session.disconnect(); System.out.println("Disconnected session"); } } catch (Exception e) { e.printStackTrace(); } } /** * 上传文件 * * @param directory * 上传的目录 * @param uploadFile * 要上传的文件 * @param sftp */ public void upload(String directory, String uploadFile, ChannelSftp sftp) { try { sftp.cd(directory); File file = new File(uploadFile); sftp.put(new FileInputStream(file), file.getName()); // sftp.put("D:/application/eclipse64ee/workspace/SFTP/src/com/isoftstone/www/ftp/SFTPTooL.java","/data/temp/test");//将本地目录的文件直接上传到服务器上 } catch (Exception e) { e.printStackTrace(); } } /** * 下载文件 * * @param directory * 下载目录 * @param downloadFile * 下载的文件 * @param saveFile * 存在本地的路径 * @param sftp */ public void download(String directory, String downloadFile, String saveFile, ChannelSftp sftp) { try { sftp.cd(directory); File file = new File(saveFile); sftp.get(downloadFile, new FileOutputStream(file)); } catch (Exception e) { e.printStackTrace(); } } /** * 删除文件 * * @param directory * 要删除文件所在目录 * @param deleteFile * 要删除的文件 * @param sftp */ public void delete(String directory, String deleteFile, ChannelSftp sftp) { try { sftp.cd(directory); sftp.rm(deleteFile); } catch (Exception e) { e.printStackTrace(); } } /** * 列出目录下的文件 * * @param directory * 要列出的目录 * @param sftp * @return * @throws SftpException */ public Vector listFiles(String directory, ChannelSftp sftp) throws SftpException { return sftp.ls(directory); } public static void main(String[] args) { SFTPTooL sf = new SFTPTooL(); String directory = "/data/temp/test/";//linux目标目录 String uploadFile = "D:/temp/upload.txt";//本地要上传的文件 String downloadFile = "download.txt";//linux将要下载的文件 String saveFile = "D:/temp/download.txt";//将要保存的文件 String deleteFile = "upload.txt";//将要删除的文件 String host = "10.15.22.120";//ip int port = 22; //port String username = "weblogic";//user String password = "weblogic";//password Session session = sf.getSession(host, port, username, password); ChannelSftp channel = sf.getConnect(session); sf.upload(directory, uploadFile, channel);//上传 sf.download(directory, downloadFile, saveFile, channel);//下载 sf.delete(directory, deleteFile, channel);//删除 sf.disconnect(channel); sf.disconnect(session); try { // sftp.cd(directory); // sftp.mkdir("ss"); System.out.println("finished"); } catch (Exception e) { e.printStackTrace(); } } }
标签:
原文地址:http://www.cnblogs.com/whatlonelytear/p/4992822.html