jar 包:
jsch-0.1.52.jar
http://central.maven.org/maven2/com/jcraft/jsch/0.1.52/jsch-0.1.52.jar
junit-4.10.jar
http://central.maven.org/maven2/junit/junit/4.10/junit-4.10.jar
java代码:
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; /** * @ClassName: Sftp * @deprecated: 使用sftp操作远程Linux文件 * @author * @company * @date 2015-6-4 * @version V1.0 */ public class Sftp { /** * 连接sftp服务器 * * @param host * 主机 * @param port * 端口 * @param username * 用户名 * @param password * 密码 * @return ChannelSftp */ public ChannelSftp connect(String host, int port, String username, String password) throws Exception { ChannelSftp sftp = 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(); 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 + " success."); return sftp; } /** * 上传文件 * * @param directory * 上传的目录 * @param uploadFile * 要上传的文件 * @param sftp */ public void upload(String directory, String uploadFile, ChannelSftp sftp) throws Exception { sftp.cd(directory); File file = new File(uploadFile); sftp.put(new FileInputStream(file), file.getName()); System.out.println("上传成功."); } /** * 下载文件 * * @param directory * 下载目录 * @param downloadFile * 下载的文件 * @param saveFile * 存在本地的路径 * @param sftp */ public void download(String directory, String downloadFile, String saveFile, ChannelSftp sftp) throws Exception { sftp.cd(directory); File file = new File(saveFile); sftp.get(downloadFile, new FileOutputStream(file)); } /** * 删除文件 * * @param directory * 要删除文件所在目录 * @param deleteFile * 要删除的文件 * @param sftp */ public void delete(String directory, String deleteFile, ChannelSftp sftp) throws Exception { sftp.cd(directory); sftp.rm(deleteFile); } /** * 列出目录下的文件 * * @param directory * 要列出的目录 * @param sftp * @return * @throws SftpException */ public Vector listFiles(String directory, ChannelSftp sftp) throws Exception { return sftp.ls(directory); } }
测试类(使用 junit 4)
import static org.junit.Assert.*; import java.util.Iterator; import java.util.Vector; import org.junit.Test; import com.jcraft.jsch.ChannelSftp; /** * @ClassName: SftpTest * @deprecated: TODO * @author * @company * @date 2015-6-4 * @version V1.0 */ public class SftpTest { private static final String host = "192.168.1.10"; private static final int port = 22; private static final String username = "xxx"; private static final String password = "xxx"; private static final String directory = "/usr/local/"; @Test public void connect() { /* 测试是否能远程连接linux服务器 */ Sftp sf = new Sftp(); ChannelSftp sftp = null; try { sftp = sf.connect(host, port, username, password); System.out.println(sftp.isConnected()); } catch (Exception e) { e.printStackTrace(); } finally { if (null != sftp) { sftp.disconnect(); } } } @Test public void upload() { /* 将本地文件上传到服务器指定的目录下 */ ChannelSftp sftp = null; try { sftp = this.connectToServer(host, port, username, password); if (null != sftp && sftp.isConnected()) { String uploadFile = "D:\\shsddata\\SH_20150519_143533_PAS.DT"; Sftp sf = new Sftp(); sf.upload(directory, uploadFile, sftp); System.out.println("Upload success!"); } } catch (Exception e) { e.printStackTrace(); } } @Test public void download() { ChannelSftp sftp = null; try { sftp = this.connectToServer(host, port, username, password); if (null != sftp && sftp.isConnected()) { String downloadFile = "SH_SCADA.DT"; Sftp sf = new Sftp(); String saveFile = "D:\\shsddata\\SH_SCADA.DT"; sf.download(directory, downloadFile, saveFile, sftp); System.out.println("download '" + downloadFile + "' success!"); } } catch (Exception e) { e.printStackTrace(); } finally { if (null != sftp) { sftp.disconnect(); } } } @Test public void delete() { fail("Not yet implemented"); } @Test public void listFiles() { /* 列出指定目录下的所有文件及文件夹 */ ChannelSftp sftp = null; try { sftp = this.connectToServer(host, port, username, password); if (null != sftp && sftp.isConnected()) { String directory = "/opt/tomcat"; Sftp sf = new Sftp(); Vector v = sf.listFiles(directory, sftp); if (null != v && !v.isEmpty()) { Iterator it = v.iterator(); while (it.hasNext()) { System.out.println(it.next().toString()); } } else { System.out.println(directory + " no file"); } } } catch (Exception e) { e.printStackTrace(); } finally { if (null != sftp) { sftp.disconnect(); } } } private ChannelSftp connectToServer(String host, int port, String username, String password) throws Exception { Sftp sf = new Sftp(); ChannelSftp sftp = null; try { sftp = sf.connect(host, port, username, password); } catch (Exception e) { throw e; } return sftp; } }