(曾在天涯)的文章详细讲解了jsch中的函数以及用法

http://www.cnblogs.com/longyg/archive/2012/06/25/2556576.html

http://www.cnblogs.com/longyg/archive/2012/06/25/2561332.html

下面是一个例子:

package com.util;

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;
import com.jcraft.jsch.ChannelSftp.LsEntry;

public class Jsch4Sftp {
 /**
  * 连接sftp服务器
  * 
  * @param host
  *            主机
  * @param port
  *            端口
  * @param username
  *            用户名
  * @param password
  *            密码
  * @return
  */
 public ChannelSftp connect(String host, int port, String username,
   String password) {
  ChannelSftp sftp = null;
  try {
   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;
   // sshSession.disconnect();
   System.out.println("Connected to " + host + ".");
  } catch (Exception e) {

}
  return sftp;
 }

/**
  * 上传文件
  * 
  * @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());
  } 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<LsEntry> listFiles(String directory, ChannelSftp sftp)
   throws SftpException {
  return sftp.ls(directory);
 }

public static void main(String[] args) {    
     Jsch4Sftp sf = new Jsch4Sftp();    
        String host = "***";    
        int port = 22; 
        String username = "***";    
        String password = "***";    
        String directory = "/home";    
        String saveFile = "1.zip";    
        String downLoadDirectory = "D:\\";    
        ChannelSftp sftp = null;    
        Session sshSession = null;
        try {    
            JSch jsch = new JSch();    
            jsch.getSession(username, host, port);   
            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 + ".");    
               
               
               
//          sf.upload(directory, uploadFile, sftp);    
//          sf.download(directory, downloadFile, saveFile, sftp);    
    //      sf.delete(directory, deleteFile, sftp);    
           
//          sftp.cd(directory);    
               
               
            Vector<LsEntry> v = sf.listFiles(directory, sftp);   
            for (LsEntry e : v) {    
                if(!e.getFilename().startsWith(".")) {    
                    saveFile = downLoadDirectory + e.getFilename();   
                    sf.download(directory, e.getFilename(), saveFile, sftp);   
                }   
            }   
            System.out.println("finished");    
        } catch (Exception e) {    
            e.printStackTrace();   
        } finally {    
            sftp.exit();   
            sshSession.disconnect();   
        }   
    }}

java-jsch实现sftp文件操作的更多相关文章

  1. JAVA API 实现hdfs文件操作

    java api 实现hdfs 文件操作会出现错误提示: Permission denied: user=hp, access=WRITE, inode="/":hdfs:supe ...

  2. 【SFTP】使用Jsch实现Sftp文件上传-支持断点续传和进程监控

    JSch是Java Secure Channel的缩写.JSch是一个SSH2的纯Java实现.它允许你连接到一个SSH服务器,并且可以使用端口转发,X11转发,文件传输等,当然你也可以集成它的功能到 ...

  3. 使用JSch实现SFTP文件传输

    1.JSch开发包下载 http://www.jcraft.com/jsch/ 目前最新版本为: jsch - 0.1.51 2.简单例子,列出指定目录下的文件列表 import  java.util ...

  4. JAVA代码时间SFTP文件的下载

    参考文章:http://blog.csdn.net/smallerpig/article/details/50976191 SFTP文件的下载与FTP文件的下载差别较大,需要下载jsch-0.1.54 ...

  5. java的基础知识文件操作和标识符

    1.文件夹的操作 dir :显示当前文件夹中的所有文件和文件夹. cd 路径:  进入到指定的路径. cd ..  : 回到上一级目录 cd  \ : 回到当前目录的跟目录 md 文件夹名  创建一个 ...

  6. Java基础知识系列——文件操作

    对文件进行操作在编程中比较少用,但是我最近有一个任务需要用到对文件操作. 对文件有如下操作形式: 1.创建新的文件(夹) File fileName = new File("C:/myfil ...

  7. java中io对文件操作的简单介绍

    11.3 I/O类使用 由于在IO操作中,需要使用的数据源有很多,作为一个IO技术的初学者,从读写文件开始学习IO技术是一个比较好的选择.因为文件是一种常见的数据源,而且读写文件也是程序员进行IO编程 ...

  8. 【java学习笔记】文件操作

    文件操作 java.io.File ①创建删除文件及目录 ②查看文件及目录属性 ③文件过滤器 (PS:不包括文件读写数据) 1.单个文件 创建单个文件,查看属性,删除单个文件. package tmp ...

  9. java IO流 对文件操作的代码集合

    Io流 按照分类 有两种分类 流向方向: 有输入流和输出流 按照操作类型有:字节流和字符流 按照流向方向 字节流的一些操作 //读文件 FileInputStream fis = new FileIn ...

  10. Java学习之==>IO文件操作体系

    一.概述 在整个 Java.io 中最重要的就是5个类和一个接口.5个类指的是 File.InputStream.OutputStream.Reader.Writer,一个接口指的是Serializa ...

随机推荐

  1. java高并发前言

  2. Umbraco -- 在Visual Studio中新建一个View 如何在Umbraco back office 中显示出来

    在使用Umbraco中的过程中,遇到一个问题. 我在项目中(Visual Studio),添加了一个View---Test.cshtml. 然后进入到该Umbraco项目的back office, 在 ...

  3. Java之Spring Boot详解(非原创)

    文章大纲 一.Spring Boot 概述二.Spring Boot 入门案例三.Spring Boot核心功能代码实战四.项目源码与资料下载五.参考文章   一.Spring Boot 概述 1. ...

  4. OTRS 二次开发笔记

    公司使用otrs系统处理业务工单,各种事件流.因为是开源免费系统,因此需要在上面做一些功能补充或定制的二次开发. otrs是什么? OTRS 是一个功能强大的工单系统.完美适用于服务台(Help De ...

  5. uva12545 比特变换器(贪心)

    uva12545 比特变换器(贪心) 输入两个等长的串S,T(长度小于100),其中S包含字符0,1,?,T中包含0和1.有三种操作:将S中的0变为1,?变为0或1,交换S中的任意两个字符.求将S变成 ...

  6. InfoQ —— 百度搜索速度优化

    本篇源自InfoQ —— <百度搜索速度优化> 好多名词不了解... 相关文档下载链接 背景 许霞,毕业于浙江大学.08年加入百度运维部. 本篇讲座主要讲述百度关于速度的优化,包括PC端和 ...

  7. Windows日志为什么要把它转成Syslog呢?

    有的朋友会问,好好的Windows日志为什么要把它转成Syslog呢?呵呵,当Windows服务器比较少的时候,我们是不需要这样做的.但试想如果你管理着成千上百台的Windows机器,你会一台一台的登 ...

  8. P2742 【模板】二维凸包 / [USACO5.1]圈奶牛Fencing the Cows

    题意:n个点,求凸包周长.(纯板子QAQ) 定义 凸包:用最小的凸多边形将n个点围在里面的图形为凸包 前置 向量:点积:(a,b) (c,d)=(a*c,b*d) =|(a,b)|*|(c,d)|*c ...

  9. Git练习3 远程库分支 idea中状态条显示当前分支

  10. QT 简单的计算器例子

    开发工具:vs2010.qt5.1 1使用vs新建工程,Base Class 选择QDialog