Package com.jcraft.jsch

Examples of com.jcraft.jsch.ChannelSftp


        assertTrue(file.exists());
        assertEquals(length, file.length());
    }

    protected String readFile(String path) throws Exception {
        ChannelSftp c = (ChannelSftp) session.openChannel("sftp");
        c.connect();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        InputStream is = c.get(path);
        try {
            byte[] buffer = new byte[256];
            int count;
            while (-1 != (count = is.read(buffer))) {
                bos.write(buffer, 0, count);
            }
        } finally {
            is.close();
        }

        c.disconnect();
        return new String(bos.toByteArray());
    }
View Full Code Here


        c.disconnect();
        return new String(bos.toByteArray());
    }

    protected void sendFile(String path, String data) throws Exception {
        ChannelSftp c = (ChannelSftp) session.openChannel("sftp");
        c.connect();
        c.put(new ByteArrayInputStream(data.getBytes()), path);
        c.disconnect();
    }
View Full Code Here

        @Override
        public synchronized SftpChannel getSftpChannel() throws IOException {
            Session session = getSession();

            ChannelSftp sftpChannel;
            try {
                sftpChannel = (ChannelSftp) session.openChannel("sftp");

                channelCount++;
                sftpChannel.connect();
            } catch (JSchException e) {
                // TODO: Close session if it's failed??
                channelCount--;
                throw new IOException("Error opening sftp channel (" + getInfo() + ")", e);
            }
View Full Code Here

    final Session session = getSession();

    session.connect();

    final ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");

    channel.connect();

    //

    final SftpProgressMonitor monitor = new SftpProgressMonitor() {

      @Override
      public void init(final int op, final String source,
          final String target, final long max) {
        logger.info("sftp upload: " + target);
      }

      @Override
      public boolean count(final long count) {
        logger.debug("sftp bytes: " + count);
        return true;
      }

      @Override
      public void end() {
        logger.debug("sftp done");
      }

    };

    final PathMaker maker = new PathMaker(logger, source, target);

    final List<Entry> entryList = maker.getEntryList();

    ensureTargetFolder(channel, target);

    for (final Entry entry : entryList) {

      final String file = entry.target;
      final int index = file.lastIndexOf("/");
      final String folder = file.substring(0, index);

      ensureTargetFolder(channel, folder);

      channel.put(entry.source, entry.target, monitor,
          ChannelSftp.OVERWRITE);

    }

    //

    channel.disconnect();

    final int status = channel.getExitStatus();

    session.disconnect();

    logger.info("sftp exit status: " + status);
View Full Code Here

        return false;
    }

    public Resource getResource(ResourceURI uri) {
        try {
            ChannelSftp channel = getOrCreateChannel(uri);
            SftpATTRS stat;

            try {
                stat = channel.stat(uri.getPath());
            } catch (SftpException e) {
                throw new ResourceNotFoundException(e);
            }

            return new SshResource(this, channel, uri, stat);
View Full Code Here

        }
    }

    protected ChannelSftp getOrCreateChannel(ResourceURI uri) {
        final ResourceKey key = new ResourceKey(uri);
        ChannelSftp channel;

        synchronized (channels) {
            channel = (ChannelSftp) channels.get(key);

            if (channel == null) {
                try {
                    ResourceContext.get().setCurrentURI(uri.getURI());

                    com.jcraft.jsch.Session session = jsch.getSession(key.getUser(), key.getHost(), key.getPort());

                    session.setUserInfo(new UserInfo() {
                        private UsernamePassword up;

                        public String getPassphrase() {
                            return null;
                        }

                        public boolean promptPassphrase(String message) {
                            return true;
                        }

                        public String getPassword() {
                            return up == null ? null : up.getPassword();
                        }

                        public boolean promptPassword(String message) {
                            URI uri = ResourceContext.get().getCurrentURI();
                            String username = ResourceContext.get().getCurrentUsername();
                            Set visitedURIs = ResourceContext.get().getVisitedURIs();

                            message = "\nAuthentication required: \n" + uri + "\n";

                            up = getResourceManager().getAuthenticationHandler().authenticate(message, uri, username,
                                    visitedURIs.contains(key));

                            visitedURIs.add(key);

                            return up != null;
                        }

                        public boolean promptYesNo(String str) {
                            return true;
                        }

                        public void showMessage(String message) {
                        }
                    });

                    session.connect();

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

                    String charset = uri.getOption("charset");

                    if (charset != null) {
                        channel.setFilenameEncoding(charset);
                    }

                    channels.put(key, channel);

                    // �ɹ���������Ա����ظ���ʾ��������
View Full Code Here

    }

    public void close() {
        synchronized (channels) {
            for (Iterator i = channels.values().iterator(); i.hasNext();) {
                ChannelSftp channel = (ChannelSftp) i.next();

                i.remove();

                if (channel != null) {
                    channel.quit();

                    try {
                        channel.getSession().disconnect();
                    } catch (JSchException e) {
                    }
                }
            }
        }
View Full Code Here

    }

    private void setLastModified(File localFile) throws JSchException {
        SftpATTRS fileAttributes = null;
        String remotePath = null;
        ChannelSftp channel = openSftpChannel();
        channel.connect();
        try {
            fileAttributes = channel.lstat(remoteDir(remoteFile)
                                           + localFile.getName());
        } catch (SftpException e) {
            throw new JSchException("failed to stat remote file", e);
        }
        FileUtils.getFileUtils().setFileLastModified(localFile,
View Full Code Here

  public ProtocolOutput getProtocolOutput(String url, WebPage page) {
    URL sUrl = null;
    String urlStr = url.toString().trim();

    ChannelSftp channelSftp = null;
    try {
      sUrl = new URL(urlStr);
      channelSftp = getChannelSftp(sUrl);

      String urlFile = sUrl.getFile();
View Full Code Here

    if (queue == null) {
      return null;
    }

    try {
      ChannelSftp cSftp = queue.take();
      return cSftp;
    } catch (InterruptedException e) {
      logger
          .error("Wait for getChannelSftp() interrupted for host: " + host, e);
      throw e;
View Full Code Here

TOP

Related Classes of com.jcraft.jsch.ChannelSftp

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.