Java Code Examples for org.apache.commons.net.ftp.FTPClient#rename()

The following examples show how to use org.apache.commons.net.ftp.FTPClient#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: FTPFileSystem.java    From RDFS with Apache License 2.0 6 votes vote down vote up
/**
 * Convenience method, so that we don't open a new connection when using this
 * method from within another method. Otherwise every API invocation incurs
 * the overhead of opening/closing a TCP connection.
 * 
 * @param client
 * @param src
 * @param dst
 * @return
 * @throws IOException
 */
private boolean rename(FTPClient client, Path src, Path dst)
    throws IOException {
  Path workDir = new Path(client.printWorkingDirectory());
  Path absoluteSrc = makeAbsolute(workDir, src);
  Path absoluteDst = makeAbsolute(workDir, dst);
  if (!exists(client, absoluteSrc)) {
    throw new IOException("Source path " + src + " does not exist");
  }
  if (exists(client, absoluteDst)) {
    throw new IOException("Destination path " + dst
        + " already exist, cannot rename!");
  }
  String parentSrc = absoluteSrc.getParent().toUri().toString();
  String parentDst = absoluteDst.getParent().toUri().toString();
  String from = src.getName();
  String to = dst.getName();
  if (!parentSrc.equals(parentDst)) {
    throw new IOException("Cannot rename parent(source): " + parentSrc
        + ", parent(destination):  " + parentDst);
  }
  client.changeWorkingDirectory(parentSrc);
  boolean renamed = client.rename(from, to);
  return renamed;
}
 
Example 2
Source File: FTPFileSystem.java    From hadoop-gpu with Apache License 2.0 6 votes vote down vote up
/**
 * Convenience method, so that we don't open a new connection when using this
 * method from within another method. Otherwise every API invocation incurs
 * the overhead of opening/closing a TCP connection.
 * 
 * @param client
 * @param src
 * @param dst
 * @return
 * @throws IOException
 */
private boolean rename(FTPClient client, Path src, Path dst)
    throws IOException {
  Path workDir = new Path(client.printWorkingDirectory());
  Path absoluteSrc = makeAbsolute(workDir, src);
  Path absoluteDst = makeAbsolute(workDir, dst);
  if (!exists(client, absoluteSrc)) {
    throw new IOException("Source path " + src + " does not exist");
  }
  if (exists(client, absoluteDst)) {
    throw new IOException("Destination path " + dst
        + " already exist, cannot rename!");
  }
  String parentSrc = absoluteSrc.getParent().toUri().toString();
  String parentDst = absoluteDst.getParent().toUri().toString();
  String from = src.getName();
  String to = dst.getName();
  if (!parentSrc.equals(parentDst)) {
    throw new IOException("Cannot rename parent(source): " + parentSrc
        + ", parent(destination):  " + parentDst);
  }
  client.changeWorkingDirectory(parentSrc);
  boolean renamed = client.rename(from, to);
  return renamed;
}
 
Example 3
Source File: FTPTransfer.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Override
public void rename(final String source, final String target) throws IOException {
    final FTPClient client = getClient(null);
    final boolean renameSuccessful = client.rename(source, target);
    if (!renameSuccessful) {
        throw new IOException("Failed to rename temporary file " + source + " to " + target + " due to: " + client.getReplyString());
    }
}
 
Example 4
Source File: FTPClientUtils.java    From springboot-ureport with Apache License 2.0 5 votes vote down vote up
/**
 * 修改 ftp 客户端文件名称
 * @param from 原文件名
 * @param to 新文件名
 * @return 是否成功
 */
public boolean rename(String from, String to)  {
	FTPClient ftpClient = borrowObject();
	try {
		boolean result = ftpClient.rename(from, to);
		return result;
	} catch (IOException e) {
		e.printStackTrace();
		throw new RuntimeException(e);
	}finally {
		returnObject(ftpClient);
	}
}
 
Example 5
Source File: FTPTransfer.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public void rename(final FlowFile flowFile, final String source, final String target) throws IOException {
    final FTPClient client = getClient(flowFile);
    final boolean renameSuccessful = client.rename(source, target);
    if (!renameSuccessful) {
        throw new IOException("Failed to rename temporary file " + source + " to " + target + " due to: " + client.getReplyString());
    }
}
 
Example 6
Source File: FTPOperationHandler.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 {
	FTPClient ftp = null;
	try {
		ftp = connect(source);
		Info sourceInfo = info(source, ftp);
		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, ftp);
		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, ftp);
				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, ftp);
				targetChanged = false;
			}
			if (targetInfo == null) {
				URI parentUri = URIUtils.getParentURI(target);
				create(ftp, parentUri, CREATE_PARENT_DIRS);
			}
		}
		if (source.normalize().equals(target.normalize())) {
			throw new SameFileException(source, target);
		}
		return ftp.rename(getPath(source), getPath(target)) ? target : null;
	} finally {
		disconnect(ftp);
	}
}
 
Example 7
Source File: PooledFTPOperationHandler.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 {
	PooledFTPConnection connection = null;
	try {
		connection = connect(source);
		FTPClient ftp = connection.getFtpClient();
		Info sourceInfo = info(source, ftp);
		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, ftp);
		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, ftp);
				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, ftp);
				targetChanged = false;
			}
			if (targetInfo == null) {
				URI parentUri = URIUtils.getParentURI(target);
				create(ftp, parentUri, CREATE_PARENT_DIRS);
			}
		}
		if (source.normalize().equals(target.normalize())) {
			throw new SameFileException(source, target);
		}
		return ftp.rename(getPath(source), getPath(target)) ? target : null;
	} finally {
		disconnect(connection);
	}
}