1、搭建本地sftp

  1.1、下载msftpsrvr.exe软件

      下载地址:http://www.download3k.com/Install-Core-FTP-Mini-SFTP-Server.html

      sftp上传 - 待完

 

  1.2、双击msftpsrvr.exe,录入sftp信息

    sftp上传 - 待完

 

2、工具类

SftpUploadUtil

package com.hundsun.channel.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;
import java.util.Vector;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;

public class SftpUploadUtil {
    
    public static final Log logger = LogFactory.getLog(SftpUploadUtil.class);
    
    /**
     * 连接sftp服务器
     * @param host 主机
     * @param port 端口
     * @param username 用户名
     * @param password 密码
     * @param privateKey 密钥文件路径
     * @param passphrase 密钥口令
     * @return
     */
    public ChannelSftp connect(String host, int port, String username,String password,String privateKey,String passphrase) {
     ChannelSftp sftp = null;
     try {
      JSch jsch = new JSch();
      jsch.getSession(username, host, port);
      if (privateKey != null && !"".equals(privateKey)) {
          //使用密钥验证方式,密钥可以使有口令的密钥,也可以是没有口令的密钥
          if (passphrase != null && !"".equals(passphrase)) {
              jsch.addIdentity(privateKey, passphrase);
          } else {
              jsch.addIdentity(privateKey);
          }
      }      
      
      Session sshSession = jsch.getSession(username, host, port);
      System.out.println("Session created.");
      if (password != null && !"".equals(password)) {
          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 + ".");
      //System.out.println("登录成功");
     } catch (Exception e) {
         System.out.println("------");
         e.printStackTrace();
     }
     return sftp;
    }

    /**
     * 关闭
     * @param sftp
     * @param sshSession
     * @throws JSchException 
     */
    public void close(ChannelSftp sftp) throws JSchException {
        if (null!=sftp) {
            if (null!=sftp.getSession()) {
                sftp.getSession().disconnect();
            }   
            sftp.disconnect();
        }

    }

    
    
    
    /**
     * 上传文件
     * @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());
      System.out.println("上传成功!");
     } 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 {
            logger.info("下载目录:" + directory + ",下载文件:" + downloadFile + ",存本地路径:" + saveFile);
            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);
    }
    
    /**
     * 创建文件夹
     * @param sftp
     * @param directory
     * @throws SftpException 
     */
    public void createFolder(ChannelSftp  sftp,String directory) throws SftpException {
              
        sftp.mkdir(directory);
    }   
    
     
    public static void main(String[] args) {
        
        /*
        Vector<LsEntry> vector = new Vector<LsEntry>();
        String host="192.168.54.112";
        int port=22;
        String username="wjs";
        String password="wjs@567";
        //上传的目录
        String directory="/home/wjs/upload/1001/"+DateUtil.getDateTime("yyyyMMdd", new Date());
        System.out.println("directory:--------------"+directory);
        
        //要上传的文件
        String uploadFile="F:\\upload\\1001\\20151102\\info_1001_9000_20151102.txt";

        SftpUploadUtil sf = new SftpUploadUtil(); 
        
        //获取连接   
        ChannelSftp  sftp = sf.connect(host, port, username, password); 
      
        //创建文件夹
        try {
            //home/wjs/upload/1001
            vector = sftp.ls(directory);
        } catch (SftpException e) {
            // TODO Auto-generated catch block
            if (e.id == 2 || "No such file".equals(e.getMessage())) {
                Iterator<LsEntry> it = vector.iterator();
                while (it.hasNext()) {
                    LsEntry lsEntry = it.next();
                    System.out.println(lsEntry.getLongname());
                    System.out.println(lsEntry.getLongname().startsWith("d"));
                }
                try {
                    sftp.mkdir(directory);
                    //上传文件   
                    sf.upload(directory, uploadFile, sftp);
                    File file = new File(uploadFile);
                    long fileSize = file.length();

                    sftp.put(directory + "/info_1001_9000_20151026.txt", new FileProgressMonitor(
                        fileSize), ChannelSftp.OVERWRITE);
                    sftp.quit();
                    sf.close(sftp);
                } catch (SftpException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (JSchException e2) {
                    // TODO Auto-generated catch block
                    e2.printStackTrace();
                }
            }
        } 
    */}
}
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-13
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-07-02
  • 2022-12-23
  • 2021-12-25
  • 2021-11-02
  • 2021-05-04
  • 2021-11-01
相关资源
相似解决方案