|
|
@@ -0,0 +1,103 @@
|
|
|
+import ch.ethz.ssh2.*;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.IOException;
|
|
|
+
|
|
|
+/**
|
|
|
+ * SCPClient
|
|
|
+ *
|
|
|
+ * @author lvzhiqiang
|
|
|
+ * 2022/4/16 16:10
|
|
|
+ */
|
|
|
+public class Test3 {
|
|
|
+ public static void main(String[] args) {
|
|
|
+ String ip = "172.16.0.101";
|
|
|
+ String username = "lvzhiqiang";
|
|
|
+ String passwd = "123456";
|
|
|
+ String srcfile = "/home/lvzhiqiang/QCCDel.log";
|
|
|
+ String despath = "/home/lvzhiqiang/abc/";
|
|
|
+
|
|
|
+ //连接服务器 服务器名称和端口号
|
|
|
+ Connection connection = new Connection(ip, 22);
|
|
|
+ //你要上传文件所在地址,linux和window路径不一样看你自己的系统
|
|
|
+ File f = new File(srcfile);
|
|
|
+ FileInputStream fis = null;
|
|
|
+ SCPOutputStream os = null;
|
|
|
+ try {
|
|
|
+ fis = new FileInputStream(f);
|
|
|
+ connection.connect();
|
|
|
+ //yuan服务器用户名和密码
|
|
|
+ boolean isAuthenticated = connection.authenticateWithPassword(username, passwd);
|
|
|
+ if (!isAuthenticated) {
|
|
|
+ throw new Exception("authcation fail");
|
|
|
+ }
|
|
|
+ SCPClient scpClient = new SCPClient(connection);
|
|
|
+
|
|
|
+ //这个是你要上传文件的目标服务器的文件路径
|
|
|
+ String remoteTargetDirectory = despath;
|
|
|
+ SFTPv3Client sftpv3Client = new SFTPv3Client(connection);
|
|
|
+ //判断是否有这个文件夹 如果没有就创建一个
|
|
|
+ Boolean isdir = sshDirectoryExists(sftpv3Client, remoteTargetDirectory);
|
|
|
+ if (!isdir) {
|
|
|
+ sftpv3Client.mkdir(remoteTargetDirectory, 0700);
|
|
|
+ }
|
|
|
+ os = scpClient.put(f.getName(), f.length(), remoteTargetDirectory, null);
|
|
|
+ byte[] b = new byte[4096];
|
|
|
+ int i;
|
|
|
+ while ((i = fis.read(b)) != -1) {
|
|
|
+ os.write(b, 0, i);
|
|
|
+ }
|
|
|
+ Session session = connection.openSession();//打开一个会话
|
|
|
+ //远程执行linux命令 因为上传的文件没有读的文件 需要加上才能下载 (如果你上传的文件有)
|
|
|
+ String cmd = "chmod +r " + remoteTargetDirectory + f.getName();
|
|
|
+ System.out.println("linux命令==" + cmd);
|
|
|
+ session.execCommand(cmd);//执行命令
|
|
|
+ os.flush();
|
|
|
+
|
|
|
+ System.out.println("upload ok");
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ if (null != fis) {
|
|
|
+ try {
|
|
|
+ fis.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (null != os) {
|
|
|
+ try {
|
|
|
+ os.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (null != connection) {
|
|
|
+ connection.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Checks if a directory exists
|
|
|
+ *
|
|
|
+ * @param sftpClient
|
|
|
+ * @param directory
|
|
|
+ * @return true, if directory exists
|
|
|
+ */
|
|
|
+ public static boolean sshDirectoryExists(SFTPv3Client sftpClient, String directory) {
|
|
|
+ try {
|
|
|
+ SFTPv3FileAttributes attributes = sftpClient.stat(directory);
|
|
|
+
|
|
|
+ if (attributes != null) {
|
|
|
+ return (attributes.isDirectory());
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|