java linux 切换目录,java在远程linux服务器中切换目录和新建目录,把本地文件上传到新建的目录中...

//java在远程linux服务器中切换目录和新建目录,把本地文件上传到刚才新建的目录中

//ganymed-ssh2-build210.jar需要下载

package com.company;

import ch.ethz.ssh2.*;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

public class Main {

//#开发环境

//#数据服务器的ip地址

private static String dataServerIp = "192.168.100.100";

// #数据服务器的用户名

private static String dataServerUsername = "root";

//#数据服务器的密码

private static String dataServerPassword = "abc";

//#数据服务器的目的文件夹

private static String dataServerDestDir = "/export/uploadDir/";

private static String localDir = "D:\\上传文件的临时目录\\";

public static void main(String[] args) {

//文件scp到数据服务器

Connection conn = new Connection(dataServerIp);

SFTPv3Client sftpClient = null;

Session session = null;

System.out.println("开始scp文件");

try {

conn.connect();

boolean isAuthenticated = conn.authenticateWithPassword(dataServerUsername, dataServerPassword);

if (isAuthenticated == false)

throw new IOException("Authentication failed.文件scp到数据服务器时发生异常");

// sftpClient = new SFTPv3Client(conn);

// 在root的目录~下创建文件夹,无法切换目录

// sftpClient.mkdir("10092", 0755); //成功,远程新建目录 ,第二个参数是创建的文件夹的读写权限,0755->即用户具有读/写/执行权限,组用户和其它用户具有读写权限;

// sftpClient.mkdir("/10092/input",0755);//失败

// sftpClient.mkdir(dataServerDestDir+"10092/input",0755);//失败

// sftpClient.rmdir("oldRemoteDir"); //远程删除目录

// sftpClient.rm("file");//远程删除文件

//执行远程命令,成功

session = conn.openSession();

session.execCommand("cd "+dataServerDestDir+" ; mkdir 10092 ; cd 10092 ; mkdir input ;mkdir output ");//分号隔开执行多条指令

//显示执行命令后的信息

InputStream stdout = new StreamGobbler(session.getStdout());

BufferedReader br = new BufferedReader(new InputStreamReader(stdout));

while (true) {

String line = br.readLine();

if (line == null) {

System.out.println("session,远程服务器返回信息:空");

break;

}

System.out.println("session,远程服务器返回信息:" + line);

}

//获得退出状态

System.out.println("session,ExitCode: " + session.getExitStatus());

session.close();

//远程文件复制到本地成功

SCPClient client = new SCPClient(conn);

client.put(localDir+"10092.zip", dataServerDestDir+"10092/input");//成功,把本地的10092.zip上传到/10092/input/目录中

// client.get(dataServerDestDir + "10092/input/10092.zip", localDir); //成功,远程scp到本地

} catch (IOException e) {

e.printStackTrace();

System.out.println("文件scp到数据服务器时发生异常");

if (conn != null) {

conn.close();

}

if (sftpClient != null) {

sftpClient.close();

}

if (session != null) {

session.close();

}

} finally {

if (conn != null) {

conn.close();

}

if (sftpClient != null) {

sftpClient.close();

}

if (session != null) {

session.close();

}

}

System.out.println("scp文件结束");

}//main

}

以上,亲测可用

参考: http://blog.sina.com.cn/s/blog_68fec59801015f5n.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值