赞
踩
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>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。