当前位置:   article > 正文

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

java jsch inputstream上传文件
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();
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
      <dependency>
            <groupId>com.jcraft</groupId>
            <artifactId>jsch</artifactId>
            <version>0.1.50</version>
        </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/715678
推荐阅读
相关标签
  

闽ICP备14008679号