zoukankan      html  css  js  c++  java
  • sftp + java,获取ftp文件,用的freesshd

    package com.qing.qing.test;

    import com.jcraft.jsch.*;
    import com.sun.xml.internal.messaging.saaj.util.ByteInputStream;
    import org.apache.commons.io.FileUtils;
    import sun.awt.image.ByteInterleavedRaster;

    import javax.swing.*;
    import java.io.*;
    import java.nio.charset.StandardCharsets;
    import java.util.Iterator;
    import java.util.Properties;
    import java.util.Vector;

    public class Test2 {
    public static void main(String[] args) {
    Session session = null;
    Channel channel = null;
    try {
    JSch jSch = new JSch();
    session = jSch.getSession("admin", "192.168.0.180",22);
    session.setPassword("666666");
    Properties sshConfig = new Properties();
    // ssh登录时不需要点击确定等验证
    sshConfig.put("StrictHostKeyChecking", "no");
    session.setConfig(sshConfig);
    session.connect();
    setUser(session);

    channel = session.openChannel("sftp");
    channel.connect(3000);
    ChannelSftp sftp = (ChannelSftp) channel; //强制转换

    // channel = session.openChannel("shell");
    // channel.connect(3000);
    // ChannelShell sftp = (ChannelShell) channel; //强制转换

    //取得文件列表
    Vector vector = sftp.ls("*");
    Iterator iterator = vector.iterator();
    int index = 0;
    while (iterator.hasNext()) {
    index++;
    ChannelSftp.LsEntry obj = (ChannelSftp.LsEntry) iterator.next();
    //取得文件名
    String file = obj.getFilename();
    //根据文件名获取文件流
    InputStream inputStream = sftp.get(file);

    byte[] buffer = TestBM.getByteArray(inputStream);
    String charStr = TestBM.getFilecharset(new ByteArrayInputStream(buffer));

    System.out.println("编码格式="+charStr);

    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(buffer), charStr));
    String ss = null;

    while((ss = bufferedReader.readLine()) != null){
    System.out.println(" " + ss);
    }

    bufferedReader.close();

    }
    // channel = session.openChannel("exec");
    // ChannelExec sftp = (ChannelExec) channel; //强制转换
    // sftp.setCommand("ls");
    // channel.setInputStream(null);

    } catch (Exception e) {
    e.printStackTrace();
    }finally {
    if(channel != null) channel.disconnect();
    if(session != null) session.disconnect();
    }
    }

    public static void setUser(Session session){
    UserInfo ui = new MyUserInfo(){
    public void showMessage(String message){
    JOptionPane.showMessageDialog(null, message);
    }
    public boolean promptYesNo(String message){
    Object[] options={ "yes", "no" };
    int foo=JOptionPane.showOptionDialog(null,
    message,
    "Warning",
    JOptionPane.DEFAULT_OPTION,
    JOptionPane.WARNING_MESSAGE,
    null, options, options[0]);
    return foo==0;
    }

    // If password is not given before the invocation of Session#connect(),
    // implement also following methods,
    // * UserInfo#getPassword(),
    // * UserInfo#promptPassword(String message) and
    // * UIKeyboardInteractive#promptKeyboardInteractive()

    };

    session.setUserInfo(ui);
    }

    public static abstract class MyUserInfo
    implements UserInfo, UIKeyboardInteractive{
    public String getPassword(){ return null; }
    public boolean promptYesNo(String str){ return false; }
    public String getPassphrase(){ return null; }
    public boolean promptPassphrase(String message){ return false; }
    public boolean promptPassword(String message){ return false; }
    public void showMessage(String message){ }
    public String[] promptKeyboardInteractive(String destination,
    String name,
    String instruction,
    String[] prompt,
    boolean[] echo){
    return null;
    }
    }
    }
  • 相关阅读:
    bzoj 1176 cdq分治套树状数组
    Codeforces 669E cdq分治
    Codeforces 1101D 点分治
    Codeforces 1100E 拓扑排序
    Codeforces 1188D Make Equal DP
    Codeforces 1188A 构造
    Codeforces 1188B 式子转化
    Codeforces 1188C DP 鸽巢原理
    Codeforces 1179D 树形DP 斜率优化
    git commit -m "XX"报错 pre -commit hook failed (add --no-verify to bypass)问题
  • 原文地址:https://www.cnblogs.com/shihx/p/13500096.html
Copyright © 2011-2022 走看看