📄 sftpclient.java
字号:
/******************************************************************************* * Copyright (c) 2000, 2006 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Corporation - initial API and implementation * Atsuhiko Yamanaka, JCraft, Inc. - adding sftp support. *******************************************************************************/package com.jcraft.eclipse.sftp.internal;import java.io.*;import java.net.*;import java.util.Date;import java.util.HashMap;import java.util.Map;import org.eclipse.core.runtime.IProgressMonitor;import com.jcraft.eclipse.sftp.*;import com.jcraft.eclipse.sftp.internal.streams.*;import com.jcraft.jsch.*;/** * Simple SFTP client class. * Based on ChannelSftp of JSch. * * The public methods of this class all throw SFTPExceptions to indicate errors. * Recoverable errors: (subsequent operations will be processed normally) * SFTPServerException and subclasses * SFTPAuthenticationException * SFTPFileNotAvailableException * SFTPServiceNotAvailableException * * Non-recoverable errors: (subsequent operations will probably fail) * SFTPCommunicationException and subclasses * ... anything else ... * */public class SFTPClient implements IClient{ public static final String PARENT_DIRECTORY_NAME="."; //$NON-NLS-1$ private static java.util.Hashtable pool=new java.util.Hashtable(); private SFTPServerLocation location; private ISFTPClientListener listener; private int timeout; boolean expectDataTransferCompletion; public static final int USE_DEFAULT_TIMEOUT=-1; private Session session; private ChannelSftp channel; /** * Creates an SFTPClient for the specified location. * @param location the SFTP server location * @param proxy the SFTP proxy location, or null * @param listener the SFTP listener * @param timeout the communications timeout in milliseconds */ public SFTPClient(SFTPServerLocation location, ISFTPClientListener listener){ this.location=location; this.listener=listener; this.timeout=SFtpPlugin.getDefault().getTimeout(); } /* (non-Javadoc) * @see com.jcraft.eclipse.sftp.IClient#getUrl() */ public URL getUrl(){ return location.getUrl(); } /* (non-Javadoc) * @see com.jcraft.eclipse.sftp.IClient#setAuthentication(java.lang.String, java.lang.String) */ public void setAuthentication(String username, String password){ location.setAuthentication(username, password); } /* (non-Javadoc) * @see com.jcraft.eclipse.sftp.IClient#run(com.jcraft.eclipse.sftp.internal.ISFtpRunnable, org.eclipse.core.runtime.IProgressMonitor) */ public void run(ISFtpRunnable runnable, IProgressMonitor monitor) throws SFtpException{ boolean isOuterRun=false; try{ monitor=SFtpPlugin.monitorFor(monitor); monitor.beginTask(null, 100+(channel==null ? 20 : 0)); if(channel==null){ open(SFtpPlugin.subMonitorFor(monitor, 10)); isOuterRun=true; } runnable.run(SFtpPlugin.subMonitorFor(monitor, 100)); } finally{ if(isOuterRun){ close(SFtpPlugin.subMonitorFor(monitor, 10)); } monitor.done(); } } /** * Opens and authenticates a connection to the server, if not already open. * @param monitor the progress monitor, or null */ public void open(IProgressMonitor monitor) throws SFtpException{ if(channel!=null) return; monitor=SFtpPlugin.monitorFor(monitor); monitor.beginTask(null, 100); monitor.subTask(SFtpPlugin.getResourceString( "SFTPClient.openConnection", location.getHostname())); //$NON-NLS-1$ try{ String hostname; int port; hostname=location.getHostname(); port=location.getPort(); monitor.worked(10); try{ session=JSchSession.getSession(null, location.getUsername(), location.getPassword(), hostname, port, monitor).getSession(); if(channel==null){ synchronized(pool){ channel=(ChannelSftp)pool.get(session); } } if(channel==null){ channel=(ChannelSftp)session.openChannel("sftp"); //$NON-NLS-1$ channel.connect(); pool.put(session, channel); } } catch(JSchException e){ throw new SFtpException(e.toString(), 0); } } catch(SFtpException e){ throw e; } finally{ monitor.done(); } } /** * Closes a connection to the server, if not already closed. * @param monitor the progress monitor, or null */ public void close(IProgressMonitor monitor) throws SFtpException{ if(channel==null) return; monitor=SFtpPlugin.monitorFor(monitor); monitor.beginTask(null, 100); monitor.subTask(SFtpPlugin.getResourceString("SFTPClient.closeConnection")); //$NON-NLS-1$ try{ channel.quit(); channel.disconnect(); } finally{ channel=null; synchronized(pool){ pool.remove(session); } } } public boolean isOpen(){ return channel!=null; } /** * Changes the current remote working directory. * @param directoryPath the absolute or relative path of the directory * @param monitor the progress monitor, or null */ public void changeDirectory(String directoryPath, IProgressMonitor monitor) throws SFtpException{ //Assert.isNotNull(directoryPath); monitor=SFtpPlugin.monitorFor(monitor); monitor.beginTask(null, 100); monitor.subTask(SFtpPlugin.getResourceString( "SFTPClient.changeDirectory", directoryPath)); //$NON-NLS-1$ try{ monitor.worked(50); try{ channel.cd(directoryPath); } catch(SftpException e){ throw new SFtpException(e.toString()+": changeDirectory "+directoryPath, 0); } monitor.worked(50); } finally{ monitor.done(); } } /** * Deletes a remote directory. * @param directoryPath the absolute or relative path of the directory * @param monitor the progress monitor, or null */ public void deleteDirectory(String directoryPath, IProgressMonitor monitor) throws SFtpException{ //Assert.isNotNull(directoryPath); monitor=SFtpPlugin.monitorFor(monitor); monitor.beginTask(null, 100); monitor.subTask(SFtpPlugin.getResourceString( "SFTPClient.deleteDirectory", directoryPath)); //$NON-NLS-1$ try{ monitor.worked(50); try{ channel.rmdir(directoryPath); } catch(SftpException e){ throw new SFtpException(e.toString()+": deleteDirectory "+directoryPath, 0); } monitor.worked(50); } finally{ monitor.done(); } } /** * Creates a remote directory. * @param directoryPath the absolute or relative path of the directory * @param monitor the progress monitor, or null */ public void createDirectory(String directoryPath, IProgressMonitor monitor) throws SFtpException{ //Assert.isNotNull(directoryPath); monitor=SFtpPlugin.monitorFor(monitor); monitor.beginTask(null, 100); monitor.subTask(SFtpPlugin.getResourceString( "SFTPClient.createDirectory", directoryPath)); //$NON-NLS-1$ try{ monitor.worked(50); try{ channel.mkdir(directoryPath); } catch(SftpException e){ throw new SFtpException(e.toString()+": createDirectory "+directoryPath, 0); } monitor.worked(50); } finally{ monitor.done(); } } /** * Deletes a remote file. * @param filePath the absolute or relative path of the file * @param monitor the progress monitor, or null */ public void deleteFile(String filePath, IProgressMonitor monitor) throws SFtpException{ //Assert.isNotNull(filePath); monitor=SFtpPlugin.monitorFor(monitor); monitor.beginTask(null, 100); monitor.subTask(SFtpPlugin.getResourceString( "SFTPClient.deleteFile", filePath)); //$NON-NLS-1$ try{ monitor.worked(50); try{ channel.rm(filePath); } catch(SftpException e){ throw new SFtpException(e.toString()+": deleteFile ", 0); } monitor.worked(50); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -