/** * 获取远程文件 * * @param src 远程文件路径 * @param dest 目标文件路径 * @return this */ public Sftp get(String src, String dest) { try { channel.get(src, dest); } catch (SftpException e) { throw new JschRuntimeException(e); } return this; }
/** * 获取远程文件 * * @param src 远程文件路径 * @param dest 目标文件路径 * @return this */ public Sftp get(String src, String dest) { try { channel.get(src, dest); } catch (SftpException e) { throw new JschRuntimeException(e); } return this; }
/** * @deprecated use {@link #get(FileObject, String)} * @param localFilePath * @param remoteFile * @throws KettleJobException */ @Deprecated public void get( String localFilePath, String remoteFile ) throws KettleJobException { int mode = ChannelSftp.OVERWRITE; try { c.get( remoteFile, localFilePath, null, mode ); } catch ( SftpException e ) { throw new KettleJobException( e ); } }
@Override public InputStream getInputStream(final String remoteFileName, final FlowFile flowFile) throws IOException { final ChannelSftp sftp = getChannel(flowFile); try { return sftp.get(remoteFileName); } catch (final SftpException e) { switch (e.id) { case ChannelSftp.SSH_FX_NO_SUCH_FILE: throw new FileNotFoundException("Could not find file " + remoteFileName + " on remote SFTP Server"); case ChannelSftp.SSH_FX_PERMISSION_DENIED: throw new PermissionDeniedException("Insufficient permissions to read file " + remoteFileName + " from remote SFTP Server", e); default: throw new IOException("Failed to obtain file content for " + remoteFileName, e); } } }
out= sftpChannel.get(remoteFile); BufferedReader br = new BufferedReader(new InputStreamReader(out)); String line;
public void get( FileObject localFile, String remoteFile ) throws KettleJobException { OutputStream localStream = null; try { localStream = KettleVFS.getOutputStream( localFile, false ); c.get( remoteFile, localStream ); } catch ( SftpException e ) { throw new KettleJobException( e ); } catch ( IOException e ) { throw new KettleJobException( e ); } finally { if ( localStream != null ) { try { localStream.close(); } catch ( IOException ignore ) { // Ignore any IOException, as we're trying to close the stream anyways } } } }
@Override public FSDataInputStream open(Path f, int bufferSize) throws IOException { ChannelSftp channel = connect(); Path workDir; try { workDir = new Path(channel.pwd()); } catch (SftpException e) { throw new IOException(e); } Path absolute = makeAbsolute(workDir, f); FileStatus fileStat = getFileStatus(channel, absolute); if (fileStat.isDirectory()) { disconnect(channel); throw new IOException(String.format(E_PATH_DIR, f)); } InputStream is; try { // the path could be a symbolic link, so get the real path absolute = new Path("/", channel.realpath(absolute.toUri().getPath())); is = channel.get(absolute.toUri().getPath()); } catch (SftpException e) { throw new IOException(e); } FSDataInputStream fis = new FSDataInputStream(new SFTPInputStream(is, channel, statistics)); return fis; }
import com.jcraft.jsch.*; public class TestJSch { public static void main(String args[]) { JSch jsch = new JSch(); Session session = null; try { session = jsch.getSession("username", "127.0.0.1", 22); session.setConfig("StrictHostKeyChecking", "no"); session.setPassword("password"); session.connect(); Channel channel = session.openChannel("sftp"); channel.connect(); ChannelSftp sftpChannel = (ChannelSftp) channel; sftpChannel.get("remotefile.txt", "localfile.txt"); sftpChannel.exit(); session.disconnect(); } catch (JSchException e) { e.printStackTrace(); } catch (SftpException e) { e.printStackTrace(); } } }
@Signature public InputStream get(String src, long skip, @Nullable Invoker progressMonitor) throws SftpException { try { return getWrappedObject().get(src, new SftpProgressMonitor() { private long max; private long offset;
/** * Executes a get SftpCommand and returns an input stream to the file * @param cmd is the command to execute * @param sftp is the channel to execute the command on * @throws SftpException */ @Override public InputStream getFileStream(String file) throws FileBasedHelperException { SftpGetMonitor monitor = new SftpGetMonitor(); try { ChannelSftp channel = getSftpChannel(); return new SftpFsFileInputStream(channel.get(file, monitor), channel); } catch (SftpException e) { throw new FileBasedHelperException("Cannot download file " + file + " due to " + e.getMessage(), e); } }
@Override public FSDataInputStream open(Path path, int bufferSize) throws IOException { SftpGetMonitor monitor = new SftpGetMonitor(); try { ChannelSftp channelSftp = this.fsHelper.getSftpChannel(); InputStream is = channelSftp.get(HadoopUtils.toUriPath(path), monitor); return new FSDataInputStream(new BufferedFSInputStream(new SftpFsHelper.SftpFsFileInputStream(is, channelSftp), bufferSize)); } catch (SftpException e) { throw new IOException(e); } }
@Override public InputStream readRaw(String source) throws IOException { try { return this.channel.get(source); } catch (SftpException e) { throw new NestedIOException("failed to read file " + source, e); } }
@Override public InputStream get(String path) throws IOException { return map(() -> ftp.get(path)); }
@Override public InputStream getInputStream(FileAbstractModel file) throws Exception { return sftp.get(file.getFullpath()); }
@Override public void read(String source, OutputStream os) throws IOException { Assert.state(this.channel != null, "session is not connected"); try { InputStream is = this.channel.get(source); FileCopyUtils.copy(is, os); } catch (SftpException e) { throw new NestedIOException("failed to read file " + source, e); } }
@Override public SftpSession getSession() { if (this.sftpEntries.size() == 0) { this.init(); } try { ChannelSftp channel = mock(ChannelSftp.class); String[] files = new File("remote-test-dir").list(); for (String fileName : files) { when(channel.get("remote-test-dir/" + fileName)) .thenReturn(new FileInputStream("remote-test-dir/" + fileName)); } when(channel.ls("remote-test-dir")).thenReturn(sftpEntries); when(jschSession.openChannel("sftp")).thenReturn(channel); return SftpTestSessionFactory.createSftpSession(jschSession); } catch (Exception e) { throw new RuntimeException("Failed to create mock sftp session", e); } }
/** * Gets the file (from the path defined on the URL). * * @param fileName the file name * @return the file * @throws SftpException the SFTP exception * @throws IOException Signals that an I/O exception has occurred. */ public InputStream getFile(String fileName) throws SftpException, IOException { return getChannel().get(url.getPath() + File.separatorChar + fileName); }
@Override public InputStream getInputStream() throws IOException { String filePath = getPath(); try { return getChannel().get(filePath); } catch (SftpException e) { throw new IOException("Can't retrieve " + filePath + " from " + url.getHost() + " because " + e.getMessage()); } }
@Override public Payload create() throws Exception { sftp = acquire(sftpConnection); return Payloads.newInputStreamPayload(new CloseFtpChannelOnCloseInputStream(sftp.get(path), sftp)); }