📄 sftpclient.java
字号:
finally{ monitor.done(); } } /** * Retrieves the contents of a remote file. * * The return input stream has a direct connection to the server. Therefore, * the contents should be read before the sftp connection is closed. * * @param filePath the absolute or relative path of the file * @param binary if true, uses binary transfer type * @param resumeAt the position in the remote file to start at (or 0 for the whole file) * @param monitor the progress monitor, or null */ private InputStream internalGetContents(String filePath, boolean binary, long resumeAt, IProgressMonitor monitor) throws SFtpException{ InputStream in=null; try{ if(resumeAt!=0){ throw new SFtpException("get: resume is not supported.", 0); } SftpProgressMonitor smonitor=null; if(monitor!=null){ final IProgressMonitor _monitor=monitor; smonitor=new SftpProgressMonitor(){ public void init(int op, String src, String dest, long max){ } public boolean count(long count){ return !_monitor.isCanceled(); } public void end(){ } }; } in=channel.get(filePath, smonitor); } catch(SftpException e){ throw new SFtpException(e.toString(), 0); } // setup line terminator conversion if(!binary&&!Constants.IS_CRLF_PLATFORM) in=new CRLFtoLFInputStream(in); return in; } public InputStream getFile(String filePath, boolean binary, long resumeAt, IProgressMonitor monitor) throws SFtpException{ monitor=SFtpPlugin.monitorFor(monitor); monitor.beginTask(null, 100); final String title=SFtpPlugin.getResourceString( "SFTPClient.getFile", filePath); //$NON-NLS-1$ monitor.subTask(title); try{ InputStream in=internalGetContents(filePath, binary, resumeAt, SFtpPlugin .subMonitorFor(monitor, 10)); // setup progress monitoring monitor.subTask(SFtpPlugin.getResourceString( "SFTPClient.transferNoSize", title)); //$NON-NLS-1$ in=new ProgressMonitorInputStream(in, 0, Constants.TRANSFER_PROGRESS_INCREMENT, monitor){ protected void updateMonitor(long bytesRead, long bytesTotal, IProgressMonitor monitor){ if(bytesRead==0) return; monitor.subTask(SFtpPlugin.getResourceString( "SFTPClient.transferUnknownSize", //$NON-NLS-1$ new Object[] {title, Long.toString(bytesRead>>10)})); } }; return in; } finally{ monitor.done(); } } public void putFile(String filePath, InputStream in, long fileSize, boolean binary, IProgressMonitor monitor) throws SFtpException{ //Assert.isNotNull(filePath); monitor=SFtpPlugin.monitorFor(monitor); monitor.beginTask(null, 100); final String title=SFtpPlugin.getResourceString( "SFTPClient.putFile", filePath); //$NON-NLS-1$ monitor.subTask(title); try{ try{ OutputStream out=null; try{ SftpProgressMonitor smonitor=null; if(monitor!=null){ final IProgressMonitor _monitor=monitor; smonitor=new SftpProgressMonitor(){ public void init(int op, String src, String dest, long max){ } public boolean count(long count){ return !_monitor.isCanceled(); } public void end(){ } }; } out=channel.put(filePath, smonitor, ChannelSftp.OVERWRITE); } catch(SftpException e){ throw new SFtpException(e.toString(), 0); } if(fileSize==0){ out.close(); return; } if(!binary&&!Constants.IS_CRLF_PLATFORM) in=new LFtoCRLFInputStream(in); monitor.subTask(SFtpPlugin.getResourceString( "SFTPClient.transferNoSize", title)); //$NON-NLS-1$ in=new ProgressMonitorInputStream(in, fileSize, Constants.TRANSFER_PROGRESS_INCREMENT, monitor){ protected void updateMonitor(long bytesRead, long bytesTotal, IProgressMonitor monitor){ if(bytesRead==0) return; monitor.subTask(SFtpPlugin.getResourceString( "SFTPClient.transferSize", //$NON-NLS-1$ new Object[] {title, Long.toString(bytesRead>>10), Long.toString(bytesTotal>>10)})); } }; // copy file to output stream byte[] buffer=new byte[SFtpPlugin.getDefault().getSendBufferSize()]; for(int count; (count=in.read(buffer))!=-1;){ out.write(buffer, 0, count); } out.close(); } finally{ try{ if(in!=null) in.close(); } finally{ } monitor.done(); } } catch(IOException e){ throw new SFtpCommunicationException(SFtpPlugin .getResourceString("SFTPClient.ErrorSendingFile"), e); //$NON-NLS-1$ } } /** * Lists the files stored in a remote directory. * @param directoryPath the absolute or relative path of the directory, or null (or empty string) for current * @param includeParents indicates whether the parent directory entries should be included. Parent directories * will have names with a single or double dot. * @param monitor the progress monitor, or null * @return an array of directory entries, or an empty array if none */ public IDirectoryEntry[] listFiles(String directoryPath, boolean includeParents, IProgressMonitor monitor) throws SFtpException{ monitor=SFtpPlugin.monitorFor(monitor); monitor.beginTask(null, 100); final String title=SFtpPlugin.getResourceString( "SFTPClient.listFiles", directoryPath==null ? "." : directoryPath); //$NON-NLS-1$ //$NON-NLS-2$ monitor.subTask(title); String pwd=null; try{ // Read the list entries from the data connection Map dirlist=new HashMap(); if(directoryPath.length()==0) directoryPath="."; try{ java.util.Vector files=channel.ls(directoryPath); for(int i=0; i<files.size(); i++){ ChannelSftp.LsEntry le=(ChannelSftp.LsEntry)files.elementAt(i); String name=le.getFilename(); boolean hasDirectorySemantics=true; boolean hasFileSemantics=true; IDirectoryEntry entry=null; SftpATTRS attrs=le.getAttrs(); Date mtime=new Date(attrs.getMTime()*(long)1000); if(attrs.isDir()){ if(!includeParents&&(name.equals(".")||name.equals(".."))){ continue; } hasFileSemantics=false; entry=new SFTPDirectoryEntry(name, hasDirectorySemantics, hasFileSemantics, attrs.getSize(), mtime); } else{ if(attrs.isLink()){ try{ if(pwd==null&&!directoryPath.equals(".")){ SftpATTRS _attrs=channel.stat(directoryPath); if(_attrs.isDir()){ pwd=channel.pwd(); try{ channel.cd(directoryPath); } catch(SftpException e){ pwd=null; } } } SftpATTRS _attrs=channel.stat(name); if(!_attrs.isDir()){ hasDirectorySemantics=false; } attrs=_attrs; mtime=new Date(attrs.getMTime()*(long)1000); } catch(SftpException ee){ continue; } } else{ hasDirectorySemantics=false; } entry=new SFTPDirectoryEntry(name, hasDirectorySemantics, hasFileSemantics, attrs.getSize(), mtime); } if(entry!=null){ if(dirlist.containsKey(name)) continue; // ignore Unix . and .. that are sometimes included in listing if(!includeParents&&(".".equals(name)||"..".equals(name)))continue; //$NON-NLS-1$ //$NON-NLS-2$ // A colon in a file or folder name is not supported in Eclipse dirlist.put(name, entry); } } } catch(SftpException e){ throw new SFtpException(e.toString()+": "+directoryPath, 0); //$NON-NLS-1$ } return (IDirectoryEntry[])dirlist.values().toArray( new SFTPDirectoryEntry[dirlist.size()]); } finally{ try{ if(pwd!=null) channel.cd(pwd); } catch(SftpException e){ } monitor.done(); } } /* (non-Javadoc) * @see com.jcraft.eclipse.sftp.IClient#getTimeout() */ public int getTimeout(){ return timeout; } /* (non-Javadoc) * @see com.jcraft.eclipse.sftp.IClient#setTimeout(int) */ public void setTimeout(int timeout){ this.timeout=timeout; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -