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";
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>
|