本文整理匯總了Java中com.jcraft.jsch.ChannelSftp.get方法的典型用法代碼示例。如果您正苦於以下問題:Java ChannelSftp.get方法的具體用法?Java ChannelSftp.get怎麽用?Java ChannelSftp.get使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.jcraft.jsch.ChannelSftp
的用法示例。
在下文中一共展示了ChannelSftp.get方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: fileFetch
import com.jcraft.jsch.ChannelSftp; //導入方法依賴的package包/類
public static void fileFetch(String host, String user, String keyLocation, String sourceDir, String destDir) {
JSch jsch = new JSch();
Session session = null;
try {
// set up session
session = jsch.getSession(user,host);
// use private key instead of username/password
session.setConfig(
"PreferredAuthentications",
"publickey,gssapi-with-mic,keyboard-interactive,password");
jsch.addIdentity(keyLocation);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
// copy remote log file to localhost.
ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
channelSftp.connect();
channelSftp.get(sourceDir, destDir);
channelSftp.exit();
} catch (Exception e) {
e.printStackTrace();
} finally {
session.disconnect();
}
}
示例2: download
import com.jcraft.jsch.ChannelSftp; //導入方法依賴的package包/類
/**
* Perform the specified SSH file download.
*
* @param from source remote file URI ({@code ssh} protocol)
* @param to target local folder URI ({@code file} protocol)
*/
private static void download(URI from, URI to) {
File out = new File(new File(to), getName(from.getPath()));
try (SessionHolder<ChannelSftp> session = new SessionHolder<>(ChannelType.SFTP, from);
OutputStream os = new FileOutputStream(out);
BufferedOutputStream bos = new BufferedOutputStream(os)) {
LOG.info("Downloading {} --> {}", session.getMaskedUri(), to);
ChannelSftp channel = session.getChannel();
channel.connect();
channel.cd(getFullPath(from.getPath()));
channel.get(getName(from.getPath()), bos);
} catch (Exception e) {
throw new RemoteFileDownloadFailedException("Cannot download file", e);
}
}
示例3: getInputStream
import com.jcraft.jsch.ChannelSftp; //導入方法依賴的package包/類
@Override
public InputStream getInputStream(String relativePath, boolean mustExist, boolean closeSession) {
Session session = null;
ChannelSftp sftp = null;
try {
session = openSession();
// Get a reusable channel if the session is not auto closed.
sftp = (closeSession) ? openConnectedChannel() : openConnectedChannel(CHANNEL_IN);
sftp.cd(basePath);
if (mustExist && !fileExists(sftp, relativePath)) {
throw new IoException("Could not find endpoint '%s' that was configured as MUST EXIST",relativePath);
}
return new CloseableInputStream(sftp.get(relativePath), session, sftp, closeSession);
} catch (Exception e) {
if (e instanceof IoException ||
(e instanceof SftpException && ((SftpException) e).id != 2)) {
throw new IoException("Error getting the input stream for sftp endpoint. Error %s", e.getMessage());
} else {
return null;
}
}
}
示例4: startSession
import com.jcraft.jsch.ChannelSftp; //導入方法依賴的package包/類
public boolean startSession(String file, String host) throws JSchException, SftpException {
String path = BioNimbusConfig.get().getDataFolder();
try {
session = jsch.getSession(USER, host, PORT);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(PASSW);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
LOGGER.info("Downloading file");
sftpChannel.get(path + file, path);
sftpChannel.exit();
session.disconnect();
} catch (JSchException | SftpException e) {
return false;
}
return true;
}
示例5: download
import com.jcraft.jsch.ChannelSftp; //導入方法依賴的package包/類
/**
* Downloads the content of a file from the remote host as a String.
*
* @param fileName the name of the file for which the content will be downloaded
* @param fromPath the path of the file for which the content will be downloaded
* @param isUserHomeBased true if the path of the file is relative to the user's home directory
* @return the content of the file
* @throws Exception exception thrown
*/
public String download(String fileName, String fromPath, boolean isUserHomeBased) throws Exception {
ChannelSftp channel = (ChannelSftp) this.session.openChannel("sftp");
channel.connect();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
BufferedOutputStream buff = new BufferedOutputStream(outputStream);
String absolutePath = isUserHomeBased ? channel.getHome() + "/" + fromPath : fromPath;
channel.cd(absolutePath);
channel.get(fileName, buff);
channel.disconnect();
return outputStream.toString();
}
示例6: download
import com.jcraft.jsch.ChannelSftp; //導入方法依賴的package包/類
public void download(String srcFile, File destFile, long timeout) throws JSchException, SftpException, IOException {
Channel channel = this.session.openChannel("sftp");
channel.setInputStream(System.in);
channel.setOutputStream(System.out);
channel.connect();
channels.add(channel);
new ChanneOperationTimer(channel, timeout).start();
ChannelSftp sftp = (ChannelSftp) channel;
try (FileOutputStream out = FileUtils.openOutputStream(destFile)) {
sftp.get(srcFile, out);
} finally {
sftp.exit();
}
}
示例7: work
import com.jcraft.jsch.ChannelSftp; //導入方法依賴的package包/類
protected void work() throws IOException, CancellationException, JSchException, SftpException, ExecutionException, InterruptedException {
if (LOG.isLoggable(Level.FINE)) {
LOG.log(Level.FINE, "{0} started", getTraceName());
}
ChannelSftp cftp = getChannel();
RemoteStatistics.ActivityID activityID = RemoteStatistics.startChannelActivity("download", srcFileName); // NOI18N
try {
cftp.get(srcFileName, dstFileName);
} catch (SftpException e) {
if (MiscUtils.mightBrokeSftpChannel(e)) {
cftp.quit();
}
throw decorateSftpException(e, srcFileName);
} finally {
releaseChannel(cftp);
RemoteStatistics.stopChannelActivity(activityID, new File(dstFileName).length());
}
}
示例8: openFileForRead
import com.jcraft.jsch.ChannelSftp; //導入方法依賴的package包/類
@Override
public InputStream openFileForRead(String path) throws Exception {
ChannelSftp c = init(path);
try {
byte[] buff = new byte[8000];
int bytesRead = 0;
InputStream in = c.get(extractSessionPath(path));
ByteArrayOutputStream bao = new ByteArrayOutputStream();
while ((bytesRead = in.read(buff)) != -1) {
bao.write(buff, 0, bytesRead);
}
byte[] data = bao.toByteArray();
ByteArrayInputStream bin = new ByteArrayInputStream(data);
c.getSession().disconnect();
return bin;
} catch (Exception e) {
tryDisconnect(c);
throw convertException(e);
}
}
示例9: get
import com.jcraft.jsch.ChannelSftp; //導入方法依賴的package包/類
/**
* 抓遠端檔案然後存到本機 , 單筆
*
* @param user
* @param password
* @param addr
* @param port
* @param remoteFile
* @param localFile
* @throws JSchException
* @throws SftpException
* @throws Exception
*/
public static void get(String user, String password, String addr, int port,
String remoteFile, String localFile) throws JSchException, SftpException, Exception {
Session session = getSession(user, password, addr, port);
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
logger.info("get remote file: " + remoteFile + " write to:" + localFile );
try {
sftpChannel.get(remoteFile, localFile);
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
sftpChannel.exit();
channel.disconnect();
session.disconnect();
}
File f=new File(localFile);
if (!f.exists()) {
f=null;
logger.error("get remote file:"+remoteFile + " fail!");
throw new Exception("get remote file:"+remoteFile + " fail!");
}
f=null;
logger.info("success write:" + localFile);
}
示例10: getFile
import com.jcraft.jsch.ChannelSftp; //導入方法依賴的package包/類
/**
* 下載指定的文檔內容,保存到指定位置,返回文件內容
* @param filepath
* @param savepath
* @return String
*/
public String getFile(String filepath,String savepath) {
String strtmp=null;
InputStream input;
ChannelSftp channel=getChannel();
try {
input=channel.get(filepath);
strtmp=FileUtil.readInputStreamToString(input, "UTF-8");
if (null!=savepath && savepath.length()>0) {
FileUtil.writeString(strtmp, savepath, "UTF-8");
}
// log.info("從文件"+filepath+"獲取信息成功");
} catch (SftpException e) {
log.error("從文件"+filepath+"獲取信息失敗");
log.error(e.getMessage());
}finally {
// channel.quit();
}
return strtmp;
}
示例11: getFile
import com.jcraft.jsch.ChannelSftp; //導入方法依賴的package包/類
private void getFile(final ChannelSftp channel,
final ChannelSftp.LsEntry le,
File localFile) throws IOException, SftpException {
final String remoteFile = le.getFilename();
if (!localFile.exists()) {
final String path = localFile.getAbsolutePath();
final int i = path.lastIndexOf(File.pathSeparator);
if (i != -1) {
if (path.length() > File.pathSeparator.length()) {
new File(path.substring(0, i)).mkdirs();
}
}
}
if (localFile.isDirectory()) {
localFile = new File(localFile, remoteFile);
}
final long startTime = System.currentTimeMillis();
final long totalLength = le.getAttrs().getSize();
SftpProgressMonitor monitor = null;
final boolean trackProgress = getVerbose() && totalLength > HUNDRED_KILOBYTES;
if (trackProgress) {
monitor = getProgressMonitor();
}
try {
log("Receiving: " + remoteFile + " : " + le.getAttrs().getSize());
channel.get(remoteFile, localFile.getAbsolutePath(), monitor);
} finally {
final long endTime = System.currentTimeMillis();
logStats(startTime, endTime, (int) totalLength);
}
if (getPreserveLastModified()) {
FileUtils.getFileUtils().setFileLastModified(localFile,
((long) le.getAttrs()
.getMTime())
* 1000);
}
}
示例12: getFileStream
import com.jcraft.jsch.ChannelSftp; //導入方法依賴的package包/類
/**
* 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);
}
}
示例13: processCommands
import com.jcraft.jsch.ChannelSftp; //導入方法依賴的package包/類
private Object processCommands(ChannelSftp channel) throws Exception {
Object result = null;
if ("get".equals(action)) {
if (log.isDebugEnabled()) {
log.debug("Sftp get from '" + from + "' to '" + to + "'");
}
ensureFrom();
if (StringUtils.isBlank(to)) {
// return content as result
ByteArrayOutputStream out = new ByteArrayOutputStream();
channel.get(from, out);
result = out.toString("UTF-8");
} else {
channel.get(from, to);
}
doPostOperations(channel, from);
} else if ("put".equals(action)) {
if (log.isDebugEnabled()) {
log.debug("Sftp put from '" + from + "' to '" + to + "'");
}
ensureTo();
if (StringUtils.isBlank(from)) {
// put value as content
Object val = getValue();
if (val == null) {
throw new PaxmlRuntimeException("Sftp command wrong: no value to put on remote server");
}
InputStream in = new ByteArrayInputStream(String.valueOf(val).getBytes("UTF-8"));
channel.put(in, to);
} else {
channel.put(from, to);
}
doPostOperations(channel, to);
} else if ("move".equals(action)) {
if (log.isDebugEnabled()) {
log.debug("Sftp move from '" + from + "' to '" + to + "'");
}
ensureFrom();
ensureTo();
channel.rename(from, to);
} else if ("delete".equals(action)) {
if (log.isDebugEnabled()) {
log.debug("Sftp delete from: " + from);
}
ensureFrom();
channel.rm(from);
} else if ("mkdir".equals(action)) {
if (log.isDebugEnabled()) {
log.debug("Sftp mkdir to: " + to);
}
ensureTo();
channel.mkdir(to);
} else if ("list".equals(action)) {
if (log.isDebugEnabled()) {
log.debug("Sftp list from: " + from);
}
ensureFrom();
result = channel.ls(from);
} else {
throw new PaxmlRuntimeException("Unknown sftp action: " + action);
}
return result;
}
示例14: open
import com.jcraft.jsch.ChannelSftp; //導入方法依賴的package包/類
@Override
public FSDataInputStream open(Path path, int bufferSize) throws IOException {
SftpGetMonitor monitor = new SftpGetMonitor();
try {
ChannelSftp channelSftp = fsHelper.getSftpChannel();
InputStream is = channelSftp.get(path.toString(), monitor);
return new FSDataInputStream(new BufferedFSInputStream(new SftpFsFileInputStream(is, channelSftp), bufferSize));
} catch (SftpException e) {
throw new IOException(e);
}
}
示例15: openNextExportFile
import com.jcraft.jsch.ChannelSftp; //導入方法依賴的package包/類
Pair<BufferedReader,Runnable> openNextExportFile() throws Exception
{
if (m_exportFiles.isEmpty())
{
checkForMoreExportFiles();
}
Pair<ChannelSftp, String> remotePair = m_exportFiles.poll();
if (remotePair == null) return null;
final ChannelSftp channel = remotePair.getFirst();
final String path = remotePair.getSecond();
System.out.println(
"INFO export: Opening export file: " + channel.getSession().getHost() + "@" + path);
final BufferedReader reader = new BufferedReader(
new InputStreamReader(channel.get(path)), 4096 * 32
);
Runnable r = new Runnable()
{
@Override
public void run()
{
try
{
reader.close();
channel.rm(path);
} catch (Exception e)
{
Throwables.propagate(e);
}
}
};
return Pair.of(reader,r);
}