zoukankan      html  css  js  c++  java
  • JSCH实现文件上传的代码实例

    package com.vcredit.ddcash.monitor.sendmail;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Properties;
    import java.util.Vector;

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;

    import com.jcraft.jsch.Channel;
    import com.jcraft.jsch.ChannelSftp;
    import com.jcraft.jsch.ChannelSftp.LsEntry;
    import com.jcraft.jsch.JSch;
    import com.jcraft.jsch.Session;

    /**
    * created by ding 2016-12-02 17:28 Friday
    * */
    public class FtpsFileList {
    private static final Logger LOG = LoggerFactory.getLogger(FtpsFileList.class);

    public static void main(String[] args) {
    listFileNames("****","**", "root", "}KCxBZC6IO]hWC>CfDkgD<41WhTP(", "/usr");

    }

    private static List<String> listFileNames(String host, int port, String username, final String password,
    String dir) {
    List<String> list = new ArrayList<String>();
    ChannelSftp sftp = null;
    Channel channel = null;
    Session sshSession = null;
    try {
    JSch jsch = new JSch();
    jsch.getSession(username, host, port);
    sshSession = jsch.getSession(username, host, port);
    sshSession.setPassword(password);
    Properties sshConfig = new Properties();
    sshConfig.put("StrictHostKeyChecking", "no");
    sshSession.setConfig(sshConfig);
    sshSession.connect();
    LOG.debug("Session connected!");
    channel = sshSession.openChannel("sftp");
    channel.connect();
    LOG.debug("Channel connected!");
    sftp = (ChannelSftp) channel;
    // 文件上传 下面这种方式也可以
    // File file = new File("15888888_back_cut.jpg");
    // file.getAbsolutePath();
    // if (!file.exists()){
    // file.createNewFile();
    // }
    // FileInputStream in = new FileInputStream(file);
    // sftp.put(in, "/usr/local/ding/15888888_back_cut.jpg1");

    // sftp.put(src, dst);//src本地文件,dst 服务器上的文件
    sftp.put("15888888_back_cut.jpg", "/usr/local/ding/");
    // 文件下载
    sftp.cd("/usr/local/ding/");
    // sftp.get(src, dst); src为服务器上的文件,dst问本地的目录,如果只是路径 ,默认文件名一致
    sftp.get("/usr/local/ding/15888888_back_cut.jpg", "D:/");

    Vector<?> vector = sftp.ls(dir);
    for (Object item : vector) {
    LsEntry entry = (LsEntry) item;
    System.out.println(entry.getFilename());
    }
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    closeChannel(sftp);
    closeChannel(channel);
    closeSession(sshSession);
    }
    return list;
    }

    private static void closeChannel(Channel channel) {
    if (channel != null) {
    if (channel.isConnected()) {
    channel.disconnect();
    }
    }
    }

    private static void closeSession(Session session) {
    if (session != null) {
    if (session.isConnected()) {
    session.disconnect();
    }
    }
    }
    }

  • 相关阅读:
    在安装ODAC后再安装.netframework导致应用程序无法找到.netframework data provider的解决方案(3种)
    浅谈Scrum敏捷开发:4个输入/输出、3个关键物、3个会议
    Payoneer个人账户注册申请教程
    巧用netsh命令实现端口转发(端口映射)不求人
    京东也开始欺骗消费者了
    powershell解决win10开始菜单和通知中心无法打开
    有些其他程序设置为从 Outlook 下载并删除邮件。为防止发生此意外情况,我们将这些邮件放入一个特殊的 POP 文件夹中
    maven搭建
    java面试第四弹(算法和编程)思路
    每秒处理10万高并发订单的乐视集团支付系统架构分享
  • 原文地址:https://www.cnblogs.com/muliu/p/6126597.html
Copyright © 2011-2022 走看看