Java Code Examples for com.jcraft.jsch.ChannelSftp#rename()

The following examples show how to use com.jcraft.jsch.ChannelSftp#rename() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SFTPTransfer.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Override
public void rename(final String source, final String target) throws IOException {
    final ChannelSftp sftp = getChannel(null);
    try {
        sftp.rename(source, target);
    } catch (final SftpException e) {
        switch (e.id) {
            case ChannelSftp.SSH_FX_NO_SUCH_FILE:
                throw new FileNotFoundException();
            case ChannelSftp.SSH_FX_PERMISSION_DENIED:
                throw new PermissionDeniedException("Could not rename remote file " + source + " to " + target + " due to insufficient permissions");
            default:
                throw new IOException(e);
        }
    }
}
 
Example 2
Source File: SftpLightWeightFileSystem.java    From incubator-gobblin with Apache License 2.0 6 votes vote down vote up
@Override
public boolean rename(Path oldPath, Path newPath) throws IOException {
  ChannelSftp channelSftp = null;
  try {
    channelSftp = this.fsHelper.getSftpChannel();
    channelSftp.rename(HadoopUtils.toUriPath(oldPath), HadoopUtils.toUriPath(newPath));

  } catch (SftpException e) {
    throw new IOException(e);
  } finally {
    safeDisconnect(channelSftp);
  }
  return true;
}
 
Example 3
Source File: SftpFileUtil.java    From Leo with Apache License 2.0 6 votes vote down vote up
/**
   * 文件重命名
   * @param oldpath
   * @param newpath
   * @return boolean
   */
  public  boolean rename(String oldpath,String newpath) {
	boolean flag=false;
	ChannelSftp channel=getChannel();
	try {
		channel.rename(oldpath, newpath);
		log.info("重命名文件"+oldpath+"成功");
		log.info("更新后的文件名为:"+newpath);
		flag=true;
	} catch (SftpException e) {
		log.error("重命名文件"+oldpath+"失败");
		log.error(e.getMessage());
	}finally {
		//channel.quit();
         
      }
	
	return flag;
}
 
Example 4
Source File: SftpSupport.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public StatInfo call() throws Exception {
    StatInfo result = null;
    SftpException exception = null;
    if (LOG.isLoggable(Level.FINE)) {
        LOG.log(Level.FINE, "{0} started", getTraceName());
    }
    String threadName = Thread.currentThread().getName();
    Thread.currentThread().setName(PREFIX + ": " + getTraceName()); // NOI18N
    RemoteStatistics.ActivityID activityID = RemoteStatistics.startChannelActivity("move", from); // NOI18N
    ChannelSftp cftp = null;
    try {
        cftp = getChannel();
        cftp.rename(from, to);
    } catch (SftpException e) {
        exception = e;
        if (e.id == SftpIOException.SSH_FX_FAILURE) {
            if (MiscUtils.mightBrokeSftpChannel(e)) {
                cftp.quit();
            }
        }
    } finally {
        RemoteStatistics.stopChannelActivity(activityID);
        releaseChannel(cftp);
        Thread.currentThread().setName(threadName);
    }
    if (exception != null) {
        if (LOG.isLoggable(Level.FINE)) {
            LOG.log(Level.FINE, "{0} failed", getTraceName());
        }
        throw decorateSftpException(exception, from + " to " + to); //NOI18N
    }

    if (LOG.isLoggable(Level.FINE)) {
        LOG.log(Level.FINE, "{0} finished", new Object[] {getTraceName()});
    }
    return result;
}
 
Example 5
Source File: SftpFileObject.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Renames the file.
 */
@Override
protected void doRename(final FileObject newFile) throws Exception {
    final ChannelSftp channel = getAbstractFileSystem().getChannel();
    try {
        final SftpFileObject newSftpFileObject = (SftpFileObject) FileObjectUtils.getAbstractFileObject(newFile);
        channel.rename(relPath, newSftpFileObject.relPath);
    } finally {
        getAbstractFileSystem().putChannel(channel);
    }
}
 
Example 6
Source File: SftpUtils.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
public static void landFile(File fileToLand, UploadProperties properties) throws Exception {
    JSch jsch = new JSch();
    
    String host = properties.getSftpServer();
    String user = properties.getUser();
    String password = properties.getPassword();
    int port = properties.getPort();
    
    Session session = jsch.getSession(user, host, port);
    session.setOutputStream(System.out);
    
    session.setPassword(password);
    
    session.setConfig("StrictHostKeyChecking", "no");
    session.connect(TIMEOUT);
    
    Channel channel = session.openChannel("sftp");
    channel.connect();
    ChannelSftp c = (ChannelSftp) channel;
    
    // delete any existing file with the target filename, so that rename will work
    @SuppressWarnings("unchecked")
    Vector<LsEntry> files = c.ls(".");
    for (LsEntry file : files) {
        if (FINAL_REMOTE_FILENAME.equals(file.getFilename())) {
            c.rm(FINAL_REMOTE_FILENAME);
        }
    }
    
    // transmit file, using temp remote name, so ingestion won't process file until complete
    c.put(new FileInputStream(fileToLand), TEMP_REMOTE_FILENAME, ChannelSftp.OVERWRITE);
    
    // rename remote file so ingestion can begin
    c.rename(TEMP_REMOTE_FILENAME, FINAL_REMOTE_FILENAME);
    
    c.disconnect();
    channel.disconnect();
    session.disconnect();
}
 
Example 7
Source File: SFTPUtils.java    From axelor-open-suite with GNU Affero General Public License v3.0 4 votes vote down vote up
/** Moves given {@code src} file to given {@code dst} file, in remote server. */
public static void move(ChannelSftp channel, String src, String dst) throws SftpException {
  channel.rename(src, dst);
}
 
Example 8
Source File: SFTPOperationHandler.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 4 votes vote down vote up
private URI rename(URI source, URI target, MoveParameters params) throws IOException, SftpException {
	SFTPSession session = null;
	try {
		session = connect(source);
		ChannelSftp channel = session.channel;
		Info sourceInfo = info(source, channel);
		if (sourceInfo == null) {
			throw new FileNotFoundException(MessageFormat.format(FileOperationMessages.getString("IOperationHandler.file_not_found"), source.toString())); //$NON-NLS-1$
		}
		Info targetInfo = info(target, channel);
		boolean targetChanged = false;
		if (((targetInfo != null) && targetInfo.isDirectory()) ||
				(Boolean.TRUE.equals(params.isMakeParents()) && target.toString().endsWith(URIUtils.PATH_SEPARATOR))) {
			target = URIUtils.getChildURI(target, sourceInfo.getName());
			targetChanged = true; // maybe new targetInfo will not be needed 
		}
		if (params.isUpdate() || params.isNoOverwrite()) {
			if (targetChanged) { // obtain new targetInfo if the target has changed
				targetInfo = info(target, channel);
			}
			if (targetInfo != null) {
				if (params.isNoOverwrite()) {
					return target;
				}
				if (params.isUpdate() && (sourceInfo.getLastModified().compareTo(targetInfo.getLastModified()) <= 0)) {
					return target;
				}
			}
		}

		if (Boolean.TRUE.equals(params.isMakeParents())) {
			if (targetChanged) { // obtain new targetInfo if the target has changed
				targetInfo = info(target, channel);
				targetChanged = false;
			}
			if (targetInfo == null) {
				URI parentUri = URIUtils.getParentURI(target);
				create(channel, parentUri, CREATE_PARENT_DIRS);
			}
		}
		if (source.normalize().equals(target.normalize())) {
			throw new SameFileException(source, target);
		}
		channel.rename(getPath(source), getPath(target));
		return target;
	} finally {
		disconnectQuietly(session);
	}
}
 
Example 9
Source File: PooledSFTPOperationHandler.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 4 votes vote down vote up
private URI rename(URI source, URI target, MoveParameters params) throws IOException, SftpException {
	PooledSFTPConnection connection = null;
	try {
		connection = connect(source);
		ChannelSftp channel = connection.getChannelSftp();
		Info sourceInfo = info(source, channel);
		if (sourceInfo == null) {
			throw new FileNotFoundException(MessageFormat.format(FileOperationMessages.getString("IOperationHandler.file_not_found"), source.toString())); //$NON-NLS-1$
		} else if (!sourceInfo.isDirectory() && target.getPath().endsWith(URIUtils.PATH_SEPARATOR)) {
			throw new IOException(MessageFormat.format(FileOperationMessages.getString("IOperationHandler.not_a_directory"), source)); //$NON-NLS-1$
		}
		Info targetInfo = info(target, channel);
		boolean targetChanged = false;
		if (((targetInfo != null) && targetInfo.isDirectory()) ||
				(Boolean.TRUE.equals(params.isMakeParents()) && target.toString().endsWith(URIUtils.PATH_SEPARATOR))) {
			target = URIUtils.getChildURI(target, sourceInfo.getName());
			targetChanged = true; // maybe new targetInfo will not be needed 
		}
		if (params.isUpdate() || params.isNoOverwrite()) {
			if (targetChanged) { // obtain new targetInfo if the target has changed
				targetInfo = info(target, channel);
				targetChanged = false;
			}
			if (targetInfo != null) {
				if (params.isNoOverwrite()) {
					return target;
				}
				if (params.isUpdate() && (sourceInfo.getLastModified().compareTo(targetInfo.getLastModified()) <= 0)) {
					return target;
				}
			}
		}

		if (Boolean.TRUE.equals(params.isMakeParents())) {
			if (targetChanged) { // obtain new targetInfo if the target has changed
				targetInfo = info(target, channel);
				targetChanged = false;
			}
			if (targetInfo == null) {
				URI parentUri = URIUtils.getParentURI(target);
				create(channel, parentUri, CREATE_PARENT_DIRS);
			}
		}
		if (source.normalize().equals(target.normalize())) {
			throw new SameFileException(source, target);
		}
		channel.rename(getPath(source), getPath(target));
		return target;
	} catch (Exception e) {
		throw new IOException(e);
	} finally {
		disconnectQuietly(connection);
	}
}