需要的jar包:ganymed-ssh2-build210.jar

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

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

public class Scpclient {

// public Scpclient(){}
    static private Scpclient instance;

static synchronized public Scpclient getInstance(String IP, int port,
            String username, String passward) {
        if (instance == null) {
            instance = new Scpclient(IP, port, username, passward);
        }
        return instance;
    }

public Scpclient(String IP, int port, String username, String passward) {
        this.ip = IP;
        this.port = port;
        this.username = username;
        this.password = passward;
    }

public void getFile(String remoteFile, String localTargetDirectory) {
        Connection conn = new Connection(ip,port);
        try {
            conn.connect();
            boolean isAuthenticated = conn.authenticateWithPassword(username,
                    password);
            if (isAuthenticated == false) {
                System.err.println("authentication failed");
            }
            SCPClient client = new SCPClient(conn);
            client.get(remoteFile, localTargetDirectory);
            conn.close();
        } catch (IOException ex) {
            Logger.getLogger(SCPClient.class.getName()).log(Level.SEVERE, null,ex);
        }
    }

public void putFile(String localFile, String remoteTargetDirectory) {
        Connection conn = new Connection(ip,port);
        try {
            conn.connect();
            boolean isAuthenticated = conn.authenticateWithPassword(username,
                    password);
            if (isAuthenticated == false) {
                System.err.println("authentication failed");
            }
            SCPClient client = new SCPClient(conn);
            client.put(localFile, remoteTargetDirectory);
            conn.close();
        } catch (IOException ex) {
            Logger.getLogger(SCPClient.class.getName()).log(Level.SEVERE, null,ex);
        }
    }
   
   
    public void putFile(String localFile, String remoteFileName,String remoteTargetDirectory,String mode) {
        Connection conn = new Connection(ip,port);
        try {
            conn.connect();
            boolean isAuthenticated = conn.authenticateWithPassword(username,
                    password);
            if (isAuthenticated == false) {
                System.err.println("authentication failed");
            }
            SCPClient client = new SCPClient(conn);
            if((mode == null) || (mode.length() == 0)){
                mode = "0600";
            }
            client.put(localFile, remoteFileName, remoteTargetDirectory, mode);
           
            //重命名
            ch.ethz.ssh2.Session sess = conn.openSession();
            String tmpPathName = remoteTargetDirectory +File.separator+ remoteFileName;
            String newPathName = tmpPathName.substring(0, tmpPathName.lastIndexOf("."));
            sess.execCommand("mv " + remoteFileName + " " + newPathName);//重命名回来
           
            conn.close();
        } catch (IOException ex) {
            Logger.getLogger(SCPClient.class.getName()).log(Level.SEVERE, null,ex);
        }
    }
   
//    public void putFile(String localFile, String remoteFileName,String remoteTargetDirectory) {
//        Connection conn = new Connection(ip,port);
//        try {
//            conn.connect();
//            boolean isAuthenticated = conn.authenticateWithPassword(username,
//                    password);
//            if (isAuthenticated == false) {
//                System.err.println("authentication failed");
//            }
//            SCPClient client = new SCPClient(conn);
//            client.put(getBytes(localFile), remoteFileName, remoteTargetDirectory);
//            conn.close();
//        } catch (IOException ex) {
//            Logger.getLogger(SCPClient.class.getName()).log(Level.SEVERE, null,ex);
//        }
//    }
   
    public static byte[] getBytes(String filePath) {
        byte[] buffer = null;
        try {
            File file = new File(filePath);
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream byteArray = new ByteArrayOutputStream(1024*1024);
            byte[] b = new byte[1024*1024];
            int i;
            while ((i = fis.read(b)) != -1) {
                byteArray.write(b, 0, i);
            }
            fis.close();
            byteArray.close();
            buffer = byteArray.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer;
    }

private String ip;
    private int port;
    private String username;
    private String password;

public String getUsername() {
        return username;
    }

public void setUsername(String username) {
        this.username = username;
    }

public String getPassword() {
        return password;
    }

public void setPassword(String password) {
        this.password = password;
    }

public int getPort() {
        return port;
    }

public void setPort(int port) {
        this.port = port;
    }

}

调用方法:
Scpclient scp = Scpclient.getInstance(ip, port,uname,pwd);
scp.putFile("本地文件路径", fileFeed.getName()+".tmp", "推送文件,到服务器的目录路径", null);
scp.getFile(remoteFile, localTargetDirectory);

Java 执行linux scp 远程获取文件和上传的更多相关文章

  1. linux scp 远程复制文件

    1.从本机复制文件到远程scp 文件名 远程计算机用户名@远程计算机的ip:远程计算机存放该文件的路径2.从远程复制文件到本机:scp 远程计算机用户名@远程计算机ip:文件名 存放该文件的本机路径3 ...

  2. linux scp远程拷贝文件及文件夹

    [http://www.jb51.net/LINUXjishu/73131.html] 1.拷贝本机/home/administrator/test整个目录至远程主机192.168.1.100的/ro ...

  3. 【转】linux scp远程拷贝文件及文件夹

    转自:http://www.jb51.net/LINUXjishu/73131.html 1.拷贝本机/home/administrator/test整个目录至远程主机192.168.1.100的/r ...

  4. Linux scp 远程文件/目录传输

    Linux scp远程文件/目录传输 1.获取远程服务器上的文件 scp -P 22 root@www.test.com:/root/test.tar.gz /home/test.tar.gz 上端口 ...

  5. java执行cmd命令并获取输出结果

    1.java执行cmd命令并获取输出结果 import java.io.BufferedReader; import java.io.InputStreamReader; import org.apa ...

  6. Linux下远程备份、上传工程,重启服务器

    Linux下远程备份.上传工程,重启服务器 Linux服务器实现远程,原项目的备份.删除,新项目上传,以及远程重启服务器!分成一个主shell调用三个shell文件步骤完成.mainsh.sh一次按顺 ...

  7. Node.js:上传文件,服务端如何获取文件上传进度

    内容概述 multer是常用的Express文件上传中间件.服务端如何获取文件上传的进度,是使用的过程中,很常见的一个问题.在SF上也有同学问了类似问题<nodejs multer有没有查看文件 ...

  8. Django积木块三——静态文件和上传文件

    静态文件和上传的文件 # 静态文件 STATIC_URL = '/static/' STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'static'), ) # ...

  9. js获取文件上传进度

    js获取文件上传进度: <input name="file" id="FileUpload" type="file" /> &l ...

随机推荐

  1. iOS主题/皮肤之SakuraKit

    概述 目前市场上很多 App 都有主题变更.皮肤切换的功能.随着项目代码量的不断增长,业务不断完善,功能性代码逐渐趋于模块化,尤其是在多人协作开发同一个项目时,模块解耦尤为重要,同时,公共基础库的功能 ...

  2. Cookie的介绍

    Cookie是某些站点为了辨别用户身份而存在用户本地终端上的数据.Cookie总是保存在client中,可分为内存 Cookie和硬盘Cookie,而依照时间划分.又能够分为非持久Cookie和持久C ...

  3. 优化技术之Android UI优化

    2013-06-30 UI 优化 在Android中,最常用LinearLayout表示UI的布局.比起LinearLayout,在资源利用上,RelativeLayout会占用更少的资源而达到相同的 ...

  4. ado.net 调用带参数的存储过程

    String connString = "Data Source = localhost; Initial Catalog = hkjc;User ID = sa;Pwd = 123&quo ...

  5. JS应用(资料很全)

    http://www.cnblogs.com/meil/archive/2007/02/06/642559.html 如果你找的javascript的东西的话,建议你 ctrl+F  直接在这个页上找 ...

  6. 工作总结 表单提交中 Input 设置 disabled readonly

    input框里面添加disabled属性之后,该内容就无法向上提交  需要的时候也可以再移除disabled readonly这个属性来禁止用户修改, 可以正常提交. Hiddent 隐藏  也可以正 ...

  7. xaf 修改主页logo

    https://documentation.devexpress.com/#Xaf/CustomDocument3156

  8. ItelliJ项目打jar包

    不是Eclipse里方便的export...了. 一.配置 . 点击View->Open Module Settings(快捷键是F4) . 在弹出的对话框中,点击最左侧树的Artifacts ...

  9. jqury插件编写

    sae中短信验证码: ; (function($) { $(document).ready(function() { if (parent && parent.location.hre ...

  10. shell脚本之函数的使用

    把代码封装成函数,相当于造了一个“轮子”,之后就直接重复使用即可. 函数的创建 shell中函数的创建有2种方式 1.使用function关键字 语法 function test { ... } 2. ...