java 利用 jsch 上传文件到 linux服务器

package cn.net.haotuo.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;


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

public class Test {



    public static void main(String[] args) {


        String host = "127.0.0.1";//windos到linux用外网IP就可以,但linux上传到linux要涉及网段、防火墙等,所以这里用的是内网IP
        int port = 22;
        String username = "root";
        String password = "root";
        try {
            JSch jsch = new JSch();
            jsch.getSession(username, host, port);
            Session sshSession = jsch.getSession(username, host, port);
            sshSession.setPassword(password);
            Properties sshConfig = new Properties();
            sshConfig.put("StrictHostKeyChecking", "no");
            sshSession.setConfig(sshConfig);
            sshSession.connect();
            Channel channel = sshSession.openChannel("sftp");
            channel.connect();
            ChannelSftp sftp = (ChannelSftp) channel;
            sftp.cd("/usr/local/tomcat/apache-tomcat-8.5.50/webapps/font/fonts/");//上传时接文件的服务器的存放目录


            InputStream is = null;
            int k=0;
            for(File file: new File("D:\\BaiduNetdiskDownload\\松邦字体\\font").listFiles()){


                String fileName =file.getName();
                System.out.println(k+++"---"+fileName);
                is = new FileInputStream(file);

                sftp.put(is, fileName, ChannelSftp.OVERWRITE);//有重名文件覆盖

            }
            sshSession.disconnect();
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
      <dependency>
            <groupId>com.jcraft</groupId>
            <artifactId>jsch</artifactId>
            <version>0.1.50</version>
        </dependency>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值