下载linux指定目录下的文件

需要用到的JAR包

<dependency>
<groupId>ch.ethz.ganymed</groupId>
<artifactId>ganymed-ssh2</artifactId>
<version>262</version>
</dependency> 

工具类

public class ScpClient {
    private static ScpClient instance;

    private String ip="114.116.23.456";

    private int port=22;

    private String name="root";

    private String password="123456";

    /**
     * 私有化默认构造函数 实例化对象只能通过getInstance
     */
    private ScpClient() {

    }

    /**
     * 私有化有参构造函数
     * 
     * @param ip       服务器ip
     * @param port     服务器端口 22
     * @param name     登录名
     * @param password 登录密码
     */
    private ScpClient(String ip, int port, String name, String password) {
        this.ip = ip;
        this.port = port;
        this.name = name;
        this.password = password;
    }

    /**
     * download
     * 
     * @param remoteFile            服务器上的文件名
     * @param remoteTargetDirectory 服务器上文件的所在路径
     * @param newPath               下载文件的路径
     */
    public void downloadFile(String remoteFile, String remoteTargetDirectory, String newPath) {
        Connection connection = new Connection(ip, port);

        try {
            connection.connect();
            boolean isAuthenticated = connection.authenticateWithPassword(name, password);
            if (isAuthenticated) {
                SCPClient scpClient = connection.createSCPClient();
                SCPInputStream sis = scpClient.get(remoteTargetDirectory + "/" + remoteFile);
                File f = new File(newPath);
                if (!f.exists()) {
                    f.mkdirs();
                }
                File newFile = new File(newPath + remoteFile);
                FileOutputStream fos = new FileOutputStream(newFile);
                byte[] b = new byte[4096];
                int i;
                while ((i = sis.read(b)) != -1) {
                    fos.write(b, 0, i);
                }
                fos.flush();
                fos.close();
                sis.close();
                connection.close();
                System.out.println("download ok");
            } else {
                System.out.println("连接建立失败");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * download
     * 
     * @param remoteFile            服务器上的文件名
     * @param remoteTargetDirectory 服务器上文件的所在路径*/
    public void download(String remoteFile, String remoteTargetDirectory,HttpServletResponse response) {
        Connection connection = new Connection(ip, port);
        try {
             //1.设置文件ContentType类型,这样设置,会自动判断下载文件类型   
            response.setContentType("multipart/form-data");   
            response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(remoteFile, "UTF-8"));//
            connection.connect();
            boolean isAuthenticated = connection.authenticateWithPassword(name, password);
            if (isAuthenticated) {
                SCPClient scpClient = connection.createSCPClient();
                SCPInputStream sis = scpClient.get(remoteTargetDirectory + "/" + remoteFile);
                OutputStream outs=response.getOutputStream();
                byte[] b = new byte[4096];
                int i;
                while ((i = sis.read(b)) != -1) {
                    outs.write(b, 0, i);
                }
                outs.flush();
                outs.close();
                sis.close();
                connection.close();
                System.out.println("download ok");
            } else {
                System.out.println("连接建立失败");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(instance!=null) {
                instance=null;
            }
        }
    }

    /**
     * 获取服务器上相应文件的流
     * 
     * @param remoteFile            文件名
     * @param remoteTargetDirectory 文件路径
     * @return
     * @throws IOException
     */
    public SCPInputStream getStream(String remoteFile, String remoteTargetDirectory) throws IOException {
        Connection connection = new Connection(ip, port);
        connection.connect();
        boolean isAuthenticated = connection.authenticateWithPassword(name, password);
        if (!isAuthenticated) {
            System.out.println("连接建立失败");
            return null;
        }
        SCPClient scpClient = connection.createSCPClient();
        return scpClient.get(remoteTargetDirectory + "/" + remoteFile);
    }

    /**
     * 上传文件到服务器
     * 
     * @param f                     文件对象
     * @param length                文件大小
     * @param remoteTargetDirectory 上传路径
     * @param mode                  默认为null
     */
    public void uploadFile(File f, String remoteTargetDirectory, String mode) {
        Connection connection = new Connection(ip, port);

        try {
            connection.connect();
            boolean isAuthenticated = connection.authenticateWithPassword(name, password);
            if (!isAuthenticated) {
                System.out.println("连接建立失败");
                return;
            }
            SCPClient scpClient = new SCPClient(connection);
            SCPOutputStream os = scpClient.put(f.getName(), f.length(), remoteTargetDirectory, mode);
            byte[] b = new byte[4096];
            FileInputStream fis = new FileInputStream(f);
            int i;
            while ((i = fis.read(b)) != -1) {
                os.write(b, 0, i);
            }
            os.flush();
            fis.close();
            os.close();
            connection.close();
            System.out.println("upload ok");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 单例模式 懒汉式 线程安全
     * 
     * @return
     */
    public static ScpClient getInstance() {
        if (null == instance) {
            synchronized (ScpClient.class) {
                if (null == instance) {
                    instance = new ScpClient();
                }
            }
        }
        return instance;
    }

    public static ScpClient getInstance(String ip, int port, String name, String password) {
        if (null == instance) {
            synchronized (ScpClient.class) {
                if (null == instance) {
                    instance = new ScpClient(ip, port, name, password);
                }
            }
        }
        return instance;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public int getPort() {
        return port;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

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

    public static void main(String[] args) {

        File f1 = new File("C:/Users/Administrator/Desktop/1577926953aaaa.png");
        System.out.println(IPUtil.getServerIpAndPort());
        ScpClient.getInstance("114.116.124.124", 22, "root", "123456").uploadFile(f1, "/usr/local", null);      ScpClient.getInstance().downloadFile("aaa.pang","/usr/local","E:/");
    }
}
posted @ 2020-01-13 17:17  累人猿  阅读(1357)  评论(0编辑  收藏  举报