sftp下载图片java代码片段

1.准备jar包:jsch-0.1.46.jar

2.SFTPConstants.java

public class SFTPConstants {
    public static final String SFTP_REQ_HOST = "host";
    public static final String SFTP_REQ_PORT = "port";
    public static final String SFTP_REQ_USERNAME = "username";
    public static final String SFTP_REQ_PASSWORD = "password";
    public static final int SFTP_DEFAULT_PORT = 22;
    public static final String SFTP_REQ_LOC = "location";
}

3.SFTPChannel.java

import java.util.Map;
import java.util.Properties;

import org.apache.log4j.Logger;

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;

public class SFTPChannel {
    Session session = null;
    Channel channel = null;

    private static final Logger LOG = Logger.getLogger(SFTPChannel.class.getName());

    public ChannelSftp getChannel(Map<String, String> sftpDetails, int timeout) throws JSchException {

        String ftpHost = sftpDetails.get(SFTPConstants.SFTP_REQ_HOST);
        String port = sftpDetails.get(SFTPConstants.SFTP_REQ_PORT);
        String ftpUserName = sftpDetails.get(SFTPConstants.SFTP_REQ_USERNAME);
        String ftpPassword = sftpDetails.get(SFTPConstants.SFTP_REQ_PASSWORD);

        int ftpPort = SFTPConstants.SFTP_DEFAULT_PORT;
        if (port != null && !port.equals("")) {
            ftpPort = Integer.valueOf(port);
        }

        JSch jsch = new JSch(); // 创建JSch对象
        session = jsch.getSession(ftpUserName, ftpHost, ftpPort); // 根据用户名,主机ip,端口获取一个Session对象
        LOG.debug("Session created.");
        if (ftpPassword != null) {
            session.setPassword(ftpPassword); // 设置密码
        }
        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config); // 为Session对象设置properties
        session.setTimeout(timeout); // 设置timeout时间
        session.connect(); // 通过Session建立链接
        LOG.debug("Session connected.");

        LOG.debug("Opening Channel.");
        channel = session.openChannel("sftp"); // 打开SFTP通道
        channel.connect(); // 建立SFTP通道的连接
        LOG.debug("Connected successfully to ftpHost = " + ftpHost + ",as ftpUserName = " + ftpUserName
                + ", returning: " + channel);
        return (ChannelSftp) channel;
    }

    public void closeChannel() throws Exception {
        if (channel != null) {
            channel.disconnect();
        }
        if (session != null) {
            session.disconnect();
        }
    }
}          

4.SFTPGetTest.java

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import com.alibaba.fastjson.JSON;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.SftpATTRS;
import com.jcraft.jsch.ChannelSftp.LsEntry;
import com.jcraft.jsch.SftpProgressMonitor;

public class SFTPGetTest {
     public SFTPChannel getSFTPChannel() {
            return new SFTPChannel();
        }

        public static void main(String[] args) throws Exception {
            SFTPGetTest test = new SFTPGetTest();
            Map<String, String> sftpDetails = new HashMap<String, String>();
            // 设置主机ip,端口,用户名,密码
            sftpDetails.put(SFTPConstants.SFTP_REQ_HOST, "134.64.XX.XX"); // ip
            sftpDetails.put(SFTPConstants.SFTP_REQ_USERNAME, "XXXX"); // 用户名
            sftpDetails.put(SFTPConstants.SFTP_REQ_PASSWORD, "XXXX"); // 密码
            sftpDetails.put(SFTPConstants.SFTP_REQ_PORT, "22"); // 端口:22
            SFTPChannel channel = test.getSFTPChannel();
            ChannelSftp chSftp = channel.getChannel(sftpDetails, 60000);
            String filename = "/opt/crm21/clerkSale/20161115/201611151651020897178_A.jpeg"; // ftp服务器上的路径+名称
            String dst = "D:\\test123.jpeg"; // 要保存的地址
            try {
                chSftp.get(filename, dst); // 代码段1
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                chSftp.quit();
                channel.closeChannel();
            }
        }
}