栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

解决上传SFTPorg.apache.commons.net.MalformedServerReplyException: Could not parse respon

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

解决上传SFTPorg.apache.commons.net.MalformedServerReplyException: Could not parse respon

解决上次SFTP报错:org.apache.commons.net.MalformedServerReplyException: Could not parse respon; Server Reply: SSH-2.0-OpenSSH_5.3

原因:当使用org.apache.commons.net.ftp.FTPClient通过协议SSH2进行SFTP连接时报如上错误,原因是它不支持这种方式的连接

解决办法

使用                com.jcraft


    com.jcraft
    jsch
    0.1.49

import com.jcraft.jsch.*;

import java.io.*;
import java.util.Properties;

public class FileUtil {
    //用户名
    private String username;
     //密码
    private String password;
     //ip
    private String host;
     //端口一般为22
    private int port;
    //私钥
    private String privateKey;
    
    ChannelSftp sftp = null;
   //通过构造方法传参
   public FileUtil(String username, String password, String host, int port){
       this.username = username;
       this.password = password;
       this.host = host;
       this.port = port;
   }
    public FileUtil(String username, String host, int port, String privateKey){
        this.username = username;
        this.host = host;
        this.port = port;
        this.privateKey = privateKey;
    }
   //登录,检查链接情况
    public void login(){
        try {
            JSch jSch = new JSch();
            if(privateKey != null){
                jSch.addIdentity(privateKey);
            }
            Session session = jSch.getSession(username,host,port);
            if(password != null){
                session.setPassword(password);
            }
            session.setTimeout(100000);

            Properties config = new Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
            session.connect();

            Channel channel = session.openChannel("sftp");
            channel.connect();
            sftp = (ChannelSftp) channel;
            System.out.println("登录成功");
        } catch (JSchException e) {
            e.printStackTrace();
        }

    }
    //上传文件
     
    public void  upload(String basePath, String direcotry, String sftpFileName, InputStream inputStream) throws SftpException{
        try {
            //进入到目标目录
            sftp.cd(basePath);
            sftp.cd(direcotry);
        } catch (SftpException e) {
            String[] dirs = direcotry.split("/");
            String temPath = basePath;
            for (String dir: dirs
                 ) {
                if( null == dir || "".equals(dir)) continue;
                temPath +="/" + dir;
                try {
                    sftp.cd(temPath);
                } catch (SftpException ex) {
                    sftp.mkdir(temPath);
                    sftp.cd(temPath);
                }
            }
        }
        sftp.put(inputStream,sftpFileName);
        System.out.println("上传成功");
    }
    //下载文件
    
    public void download(String directory, String downloadFile, String saveFileDirectory) throws SftpException, FileNotFoundException{
       if(directory != null && !"".equals(directory)){
           sftp.cd(directory);
       }
        String saveFile = saveFileDirectory + "//" + downloadFile;
       File file = new File(saveFile);
       sftp.get(downloadFile, new FileOutputStream(file));
        System.out.println("下载成功");
    }
    //登出




    public static void main(String[] args) throws FileNotFoundException,SftpException {
        FileUtil fiel = new FileUtil("用户名","密码","host",22);
        fiel.login();     
        //测试上传功能
        File file = new File(图片地址);
        InputStream is = new FileInputStream(file);
        fiel.upload("上传目标目录","","文件名.xlxs",is);
        //测试下载功能
        try {
            fiel.download("下载目标目录","下载文件名","下载到的路径(本地服务器路径)");
        } catch (SftpException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1023840.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号