`
Jameslyy
  • 浏览: 385600 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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());

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics