JSch jSch = new JSch(); Session session = jSch.getSession(user, host, 22); session.setPassword(password); Properties config = new Properties(); session.setConfig(config); session.connect(); Channel channel = session.openChannel("sftp"); channel.connect(); sftp = (ChannelSftp) channel; try { sftp.cd("/your/tomcat/dir/wars"); File file = getWarFile(); sftp.put(new FileInputStream(file), file.getName()); } catch (SftpException e) { e.printStackTrace(); }
public static void main(String[] args) { SimpleLinuxGUI slg = new SimpleLinuxGUI(); JFrame jf = new JFrame(); DefaultMutableTreeNode nroot = new DefaultMutableTreeNode(slg.sshremotedir); try { cargarRTree(slg.sshremotedir, nroot); } catch (SftpException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (JSchException ex) { Logger.getLogger(SimpleLinuxGUI.class.getName()).log(Level.SEVERE, null, ex); } JTree yourJTree = new JTree(nroot); jf.add(yourJTree); jf.setSize(640, 480); jf.setVisible(true); }
journal.log(Level.SEVERE,"File access error "+e.getMessage()); } catch (SftpException e) { e.printStackTrace();
} catch (SftpException e) { _log.error("connection issue with sftp"); e.printStackTrace(); return getDefaultFeedMessage(); } finally {
return false; } catch (SftpException e) { e.printStackTrace(); FileTransferExecutor.logger.error("sftp文件传送异常!{}", file.getAbsolutePath(),e); return false;
@Override public InputStream getInputStream(long offset) throws IOException { // Retrieve a ConnectionHandler and lock it final SFTPConnectionHandler connHandler = (SFTPConnectionHandler)ConnectionPool.getConnectionHandler(connHandlerFactory, fileURL, true); try { // Makes sure the connection is started, if not starts it connHandler.checkConnection(); InputStream in = new SFTPInputStream(connHandler.channelSftp.get(absPath), connHandler); in.skip(offset); return in; } catch(IOException e) { // Release the lock on the ConnectionHandler if the InputStream could not be created connHandler.releaseLock(); // Re-throw IOException throw e; } catch (SftpException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } }
} catch (SftpException e) { e.printStackTrace();
} catch (SftpException e) { e.printStackTrace();
@Override public void mkdir() throws IOException { // Retrieve a ConnectionHandler and lock it SFTPConnectionHandler connHandler = (SFTPConnectionHandler)ConnectionPool.getConnectionHandler(connHandlerFactory, fileURL, true); try { // Makes sure the connection is started, if not starts it connHandler.checkConnection(); connHandler.channelSftp.mkdir(absPath); // Update local attributes fileAttributes.setExists(true); fileAttributes.setDirectory(true); fileAttributes.setDate(System.currentTimeMillis()); fileAttributes.setSize(0); } catch (SftpException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { // Release the lock on the ConnectionHandler connHandler.releaseLock(); } }
@Override public void changePermissions(int permissions) throws IOException { // Retrieve a ConnectionHandler and lock it SFTPConnectionHandler connHandler = null; try { connHandler = (SFTPConnectionHandler)ConnectionPool.getConnectionHandler(connHandlerFactory, fileURL, true); // Makes sure the connection is started, if not starts it connHandler.checkConnection(); connHandler.channelSftp.chmod(permissions, absPath); // Update local attribute copy fileAttributes.setPermissions(new SimpleFilePermissions(permissions)); } catch (SftpException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { // Release the lock on the ConnectionHandler if(connHandler!=null) connHandler.releaseLock(); } }
e.printStackTrace(); } catch (SftpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace();
@Override public void changeDate(long lastModified) throws IOException, UnsupportedFileOperationException { SFTPConnectionHandler connHandler = null; SftpATTRS attributes = null; try { // Retrieve a ConnectionHandler and lock it connHandler = (SFTPConnectionHandler)ConnectionPool.getConnectionHandler(connHandlerFactory, fileURL, true); // Makes sure the connection is started, if not starts it connHandler.checkConnection(); // Retrieve an SftpFile instance for write, will throw an IOException if the file does not exist or cannot // be written. // /!\ SftpFile instance must be closed afterwards to release its file handle attributes = connHandler.channelSftp.lstat(absPath); attributes.setACMODTIME(attributes.getATime(), (int)(lastModified/1000)); connHandler.channelSftp.setStat(absPath, attributes); // Update local attribute copy fileAttributes.setDate(lastModified); } catch (SftpException e) { e.printStackTrace(); } finally { // Release the lock on the ConnectionHandler if(connHandler!=null) connHandler.releaseLock(); } }
} catch (SftpException e) { e.printStackTrace();
@Override public void delete() throws IOException { // Retrieve a ConnectionHandler and lock it SFTPConnectionHandler connHandler = null; try { // Retrieve a ConnectionHandler and lock it connHandler = (SFTPConnectionHandler)ConnectionPool.getConnectionHandler(connHandlerFactory, fileURL, true); // Makes sure the connection is started, if not starts it connHandler.checkConnection(); if(isDirectory()) connHandler.channelSftp.rmdir(absPath); else connHandler.channelSftp.rm(absPath); // Update local attributes fileAttributes.setExists(false); fileAttributes.setDirectory(false); fileAttributes.setSymlink(false); fileAttributes.setSize(0); } catch (SftpException e) { e.printStackTrace(); } finally { // Release the lock on the ConnectionHandler if the OutputStream could not be created if(connHandler!=null) connHandler.releaseLock(); } }
} catch (SftpException e) { e.printStackTrace(); return null;
} catch (SftpException e) { e.printStackTrace();