SFTP Java Client -- jcraft.jsch

阅读更多

 http://www.jcraft.com/jsch

 

        JSch jsch = new JSch();
        ChannelSftp channelSftp = null;
        Session session = null;
        try{
            session = jsch.getSession(userName, host, port);
            session.setPassword(password);
            Properties config = new Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
            session.connect();

            channelSftp = (ChannelSftp)session.openChannel("sftp");
            channelSftp.connect();

            channelSftp.cd(dirTransactions);
            OutputStream outputStream = channelSftp.put(fileName);
            outputStream.write(content.getBytes());

            outputStream.flush();
            outputStream.close();
        } catch (JSchException e) {
            e.printStack();
        } catch (SftpException e) {
            e.printStack();
        } catch (IOException e) {
            e.printStack();
        } finally{
            if (channelSftp != null)
                channelSftp.quit();
            if (session != null)
                session.disconnect();
        }
    

 

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        channelSftp.get(dir + fileName, outputStream);
        content = new String(outputStream.toByteArray());

 

 

 

 

你可能感兴趣的:(java,sftp,jcraft,jsch)