(曾在天涯)的文章详细讲解了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. android中如何在系统启动的时候启动自己的service

    自定义一个broadcastreciver在去接受系统启动消息,然后在处理的时候启动自己的service即可

  2. Web项目的导出和部署

    -----------------siwuxie095                                 Web 项目的导出     工程结构目录如下:                 ...

  3. CodeForces 492A Vanya and Cubes

    A. Vanya and Cubes time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  4. Entity Framework Code-First(9.11):DataAnnotations - InverseProperty Attribute

    DataAnnotations - InverseProperty Attribute: We have seen in the Code-First Convention section that ...

  5. Reboot

    目标是将浏览器的预设样式设为一致 Native font stack  本机字体堆栈 由于padding 及 border 会改变元素在运算后的宽度 此时的实际宽度为: width+左右padding ...

  6. bzoj2502: 清理雪道(有源汇有上下界最小流)

    传送门 别说话,自己看,我不会->这里 我这里用的建图方法是先跑一次最大流,连上$(t,s,inf)$之后再跑一遍,然后答案就是之前连的那条边的反向边的流量 据说还有种方法是连上$(t,s,in ...

  7. Python Day24

    AJAX 对于WEB应用程序:用户浏览器发送请求,服务器接收并处理请求,然后返回结果,往往返回就是字符串(HTML),浏览器将字符串(HTML)渲染并显示浏览器上. 1.传统的Web应用 一个简单操作 ...

  8. 分层图最短路【bzoj2834】: 回家的路

    分层图最短路[bzoj2834]: 回家的路 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2834 这道题难在建边. 自己写的时候想到了 ...

  9. Exadata 12.2.1.1.0 Highlights

    突然发现,在中国农历新年的这几年里,Exadata 12.2版本已经发布了. 本起去docs.oracle.com上看看它的新特性,结果发现文档还没有更新: 下面是找到的一些资料,让我们来一睹为快吧: ...

  10. 虚拟机上安装Cell节点(12.1.2.3.3)

    安装介质下载 打开firefox,输入:https://edelivery.oracle.com 点击"Sign In",输入帐号.密码,登陆edelivery网站.       ...