java从windows向linux传输文件

java从windows向linux传输文件

<dependency>
            <groupId>ch.ethz.ganymed</groupId>
            <artifactId>ganymed-ssh2</artifactId>
            <version>build210</version>
        </dependency>
package com.ruoyi.xatool;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.SCPClient;
import ch.ethz.ssh2.SFTPv3Client;
import ch.ethz.ssh2.Session;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;

public class SSH2App {
	private static final String host = "";
	private static final String username = "";
	private static final String password="";
	private static final Connection conn = new Connection(host,2222);
	
	//账密验证
	private static boolean isAuthed(){
		try {
			return conn.authenticateWithPassword(username, password);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	//上传文件
	public static void putFile(String localFile,String remoteTargetDirectory){
		try {
			conn.connect();
			if(isAuthed()){
				SCPClient scpClient = conn.createSCPClient();
				scpClient.put(localFile, remoteTargetDirectory);
				System.out.println("上传成功");
			}else{
				System.out.println("上传失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			conn.close();
		}
	}
	//判断是否有该文件夹
	public static boolean exec(String command,String url){
		boolean flag =false;
		try {
			conn.connect();
			if(isAuthed()){
				SFTPv3Client sftpv3Client = new SFTPv3Client(conn);
				Session session = conn.openSession();
				session.execCommand(command);
				InputStream input = session.getStdout();
				byte[] data = new byte[1024];
				int read = input.read(data);
				//String result = new String(data,0,data.length);
				if(read<1){
					sftpv3Client.mkdir(url,0777);
					System.out.println("创建文件夹:"+url);
				}else{
					flag = true;
				}
			}else{
				System.out.println("authentication fail.");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			conn.close();
			return flag;
		}
	}
	//拼路径
	public void isDir(String upUrl,String localUrl){
		String[] split = upUrl.split("/");
		String dirUrl ="/home/filebackup";
		//判断是否有该村文件夹,split[split.length-2]==村名
		String dir = "find "+dirUrl+" -name "+split[split.length-2]+" -type d";
		boolean exec = exec(dir, upUrl);
		if(!exec){
			for (int i = 4; i < split.length; i++) {
				dirUrl=dirUrl+"/"+split[i];
				dir="find "+dirUrl+" -name "+split[i]+" -type d";
				exec(dir,dirUrl);
			}
		}else{
		//split[split.length-1]==村民姓名
			dir = "find "+dirUrl+" -name "+split[split.length-1]+" -type d";
			exec(dir,upUrl);
		}
		putFile(localUrl, upUrl);
	}
	
	public static void main(String[] args) throws UnsupportedEncodingException {
		String dir = "";
		String url="";
		//isDir(url);
	}
}

创建文件夹那里感觉还能优化,如果有大佬会的可以留言哦

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值