SFTP上传下载

Posted 一棵小草77

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SFTP上传下载相关的知识,希望对你有一定的参考价值。

代码:

package com.line.intf.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
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.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;

public class SFTP {

    /**
     * @param args
     */
    public static void main(String[] args) {
        
        SFTP ftp = new SFTP();
        String host = "192.168.0.211";
        int port = 22;
        String username = "root";
        String password = "root";
        String directory = "/opt/tomcat/";
        String uploadFile = "D:/test.txt";
//        String downloadFile = "ipconfig.txt";
//        String saveFile = "D:/ipconfig.txt";
//        String deleteFile = "ipconfig.txt";
        
        ChannelSftp sftp = ftp.connect(host, port, username, password);
        try {
            ftp.upload(directory, uploadFile, sftp);
//            ftp.download(directory, downloadFile, saveFile, sftp);
//            ftp.delete(directory, deleteFile, sftp);
//            sftp.cd(directory);
//            sftp.mkdir("createFolder");
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            try {
            //如果没有sesstion的disconnect,程序不会退出
                sftp.getSession().disconnect();
            } catch (JSchException e) {
                e.printStackTrace();
            }
            sftp.disconnect();
            sftp.exit();
        }
    }

    /**
     * @AddBy: Ares
     * @Description: TODO(connect the host)
     * @param host
     * @param port
     * @param username
     * @param password
     * @return
     */
    public ChannelSftp connect(String host, int port, String username, String password) {
        ChannelSftp csftp = null;
        JSch jsch = new JSch();
        try {
            Session sshSession = jsch.getSession(username, host, port);
            System.out.println("jsch session created, user="+username);
            
            sshSession.setPassword(password);
            Properties sshConfig = new Properties();
            sshConfig.put("StrictHostKeyChecking", "no");
            sshSession.setConfig(sshConfig);
            sshSession.connect();
            System.out.println("session is connected.");
            
            Channel channel = sshSession.openChannel("sftp");
            channel.connect();
            
            csftp = (ChannelSftp)channel;
            System.out.println("connected to host:"+ host);
            
        } catch (JSchException e) {
            e.printStackTrace();
        }
        return csftp;
    }
    
    /**
     * @AddBy: Ares
     * @Description: TODO(upload file to host)
     * @param directory
     * @param uploadFile
     * @param sftp
     * @return
     */
    public boolean upload(String directory, String uploadFile, ChannelSftp sftp){
        File file = new File(uploadFile);
        try {
            sftp.cd(directory);
            sftp.put(new FileInputStream(file), file.getName());
            System.out.println("upload file success, file:"+uploadFile);
        } catch (Exception e) {
            System.err.println("upload file failed, file:"+uploadFile);
            e.printStackTrace();
            return false;
        }
        return true;
    }
    /**
     * @AddBy: Ares
     * @Description: TODO(download file from host)
     * @param directory
     * @param downloadFile
     * @param saveFile
     * @param sftp
     * @return
     */
    public boolean download(String directory, String downloadFile, String saveFile, ChannelSftp sftp){
        File file = new File(saveFile);
        try {
            sftp.cd(directory);
            sftp.get(downloadFile, new FileOutputStream(file));
            System.out.println("download file success, file:"+downloadFile);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (SftpException e) {
            System.err.println("download file failed, file:"+downloadFile);
            e.printStackTrace();
            return false;
        }
        return true;
    }
    
    /**
     * @AddBy: Ares
     * @Description: TODO(delete file of host)
     * @param directory
     * @param deleteFile
     * @param sftp
     * @return
     */
    public boolean delete(String directory, String deleteFile, ChannelSftp sftp){
        try {
            sftp.cd(directory);
            sftp.rm(deleteFile);
            System.out.println("delete file success, file:"+deleteFile);
        } catch (SftpException e) {
            System.err.println("delete file failed, file:"+deleteFile);
            e.printStackTrace();
            return false;
        }
        return true;
    }
    
    /**
     * @AddBy: Ares
     * @Description: TODO(get file list from directory of host)
     * @param directory
     * @param sftp
     * @return
     */
    public Vector<?> listFiles(String directory, ChannelSftp sftp){
        try {
            return sftp.ls(directory);
        } catch (SftpException e) {
            System.err.println("list directory failed, directory:"+directory);
            e.printStackTrace();
        }
        return null;
    }

}
View Code

 

jar包maven下载地址:

<!-- https://mvnrepository.com/artifact/com.jcraft/jsch -->
        <dependency>
            <groupId>com.jcraft</groupId>
            <artifactId>jsch</artifactId>
            <version>0.1.53</version>
        </dependency>
View Code

 

以上是关于SFTP上传下载的主要内容,如果未能解决你的问题,请参考以下文章

仅20行代码,实现文件自动化上传至sftp

Java利用JSch实现sftp通过ssh上传下载删除文件及配置代码

我的代码库-Java8实现FTP与SFTP文件上传下载

SFTP服务配置以及命令/代码操作

使用 SFTP 上传文件

paramiko批量上传下载sftp,解决访问windows系列sftp乱码问题