解决上传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

<dependency>
    <groupId>com.jcraft</groupId>
    <artifactId>jsch</artifactId>
    <version>0.1.49</version>
</dependency>

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();
        }

    }
    //上传文件
    /**
     * @param basePath 目标路径
     * @param direcotry 目标子路径
     * @param sftpFileName 文件名称
     */ 
    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("上传成功");
    }
    //下载文件
    /**
     * @param directory 下载的文件路径
     * @param downloadFile 下载的文件名
     * @param saveFileDirectory 保存的文件路径
     */
    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();
        }
    }
}

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值