자바, windows 에서 Linux 로 로 컬 파일 전송

2227 단어 자바
package com.ddddd.test;  
  
import java.io.File;  
import java.io.IOException;  

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
  
import ch.ethz.ssh2.Connection;  
import ch.ethz.ssh2.SCPClient;  
  
/** 
 * 
 */  

@Controller
@RequestMapping("/testsss")
public class Testsss {  
    private static String IP = "10.126.252.68";  
    private static int PORT = 22;  
    private static String USER = "test";//    Linux        
    private static String PASSWORD = "teller";//   Linux        
    private static Connection connection = new Connection(IP, PORT);  
    private static boolean usePassword = true;//                
  
    /** 
     * ssh      ,            
     *  
     * @param user 
     * @param password 
     * @return 
     */  
    
    
    @RequestMapping("/app")
    public void app(String[] args) {  
    	try {   
    		putFile("e://Tomcat/Testsss.java", "/home/test/fanwb");  
    	} catch (Exception e) {  
    		e.printStackTrace();  
    	}  
    }  
    
    public static void putFile(String localFile, String remoteTargetDirectory) {  
    	try {  
    		connection.connect();  
    		boolean isAuthed = isAuth();  
    		if (isAuthed) {  
    			SCPClient scpClient = connection.createSCPClient();  
    			scpClient.put(localFile, remoteTargetDirectory);  
    			System.out.println("    !");
    		} else {  
    			System.out.println("    !");  
    		}  
    	} catch (Exception ex) {  
    		ex.printStackTrace();  
    	} finally {  
    		connection.close();  
    	}  
    }  
    
    public static boolean isAuth() {  
    	if (usePassword) {  
    		return isAuthedWithPassword(USER, PASSWORD);  
    	}
    	return false;
    }  
    
    
    public static boolean isAuthedWithPassword(String user, String password) {  
        try {  
            return connection.authenticateWithPassword(user, password);  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        return false;  
    }  
  

  
  
}  

의존 하 는 jar 패키지:https://download.csdn.net/download/qq_40375627/10429653

좋은 웹페이지 즐겨찾기