java.nio.file.CopyOption#com.jcraft.jsch.SftpException源码实例Demo

下面列出了java.nio.file.CopyOption#com.jcraft.jsch.SftpException 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: vividus   文件: SftpCommandTests.java
@Test
@SuppressWarnings({ "PMD.ReplaceVectorWithList", "PMD.UseArrayListInsteadOfVector" })
void shouldExecuteLsCommand() throws IOException, SftpException
{
    ChannelSftp channel = mock(ChannelSftp.class);
    String path = "/dir-to-list";
    LsEntry lsEntry1 = mock(LsEntry.class);
    when(lsEntry1.getFilename()).thenReturn("file1.txt");
    LsEntry lsEntry2 = mock(LsEntry.class);
    when(lsEntry2.getFilename()).thenReturn("file2.story");
    Vector<LsEntry> ls = new Vector<>();
    ls.add(lsEntry1);
    ls.add(lsEntry2);
    when(channel.ls(path)).thenReturn(ls);
    String result = SftpCommand.LS.execute(channel, path);
    assertEquals("file1.txt,file2.story", result);
}
 
源代码2 项目: Leo   文件: SftpFileUtil.java
/**
    * 删除服务器上的文件
    * @param filepath
    * @return boolean
    */
   public  boolean delFile(String filepath) {
	boolean flag=false;
	ChannelSftp channel=getChannel();
	try {
		channel.rm(filepath);
		log.info("删除文件"+filepath+"成功");
		flag=true;
	} catch (SftpException e) {
		log.error("删除文件"+filepath+"失败");
		log.error(e.getMessage());
	}finally {
			//channel.quit();
          
       }
	
	return flag;
}
 
源代码3 项目: vividus   文件: SftpExecutorTests.java
@Test
void testPopulateErrorStreamOnSftpError() throws Exception
{
    ChannelSftp channel = mock(ChannelSftp.class);
    SftpException sftpException = new SftpException(0, "error");
    doThrow(sftpException).when(channel).pwd();
    ServerConfiguration serverConfiguration = new ServerConfiguration();
    serverConfiguration.setAgentForwarding(true);
    SftpOutput sftpOutput = sftpExecutor.executeCommand(serverConfiguration, new Commands("pwd"), channel);
    assertEquals("", sftpOutput.getResult());
    InOrder ordered = inOrder(channel, softAssert);
    ordered.verify(channel).setAgentForwarding(serverConfiguration.isAgentForwarding());
    ordered.verify(channel).connect();
    ordered.verify(channel).pwd();
    ordered.verify(softAssert).recordFailedAssertion("SFTP command error", sftpException);
    ordered.verifyNoMoreInteractions();
}
 
源代码4 项目: davos   文件: SFTPConnection.java
@Override
public void download(FTPFile file, String localFilePath) {

    String path = FileUtils.ensureTrailingSlash(file.getPath()) + file.getName();
    String cleanLocalPath = FileUtils.ensureTrailingSlash(localFilePath);
    
    LOGGER.debug("Download. Remote path: {}", path);
    LOGGER.debug("Download. Local path: {}", cleanLocalPath);

    try {

        if (file.isDirectory())
            downloadDirectoryAndContents(file, cleanLocalPath, path);
        else
            doGet(path, cleanLocalPath);

    } catch (SftpException e) {
        throw new DownloadFailedException("Unable to download file " + path, e);
    }
}
 
源代码5 项目: anyline   文件: SFTPUtil.java
/**  
 * 删除文件夹-sftp协议.如果文件夹有内容,则会抛出异常.  
 * @param path 文件夹路径  
 * @throws SftpException   SftpException
 */   
public void deleteDir(String path) throws SftpException {   
    @SuppressWarnings("unchecked")   
    Vector<LsEntry> vector = client.ls(path);   
    if (vector.size() == 1) { // 文件,直接删除   
        client.rm(path);   
    } else if (vector.size() == 2) { // 空文件夹,直接删除   
        client.rmdir(path);   
    } else {   
        String fileName = "";   
        // 删除文件夹下所有文件   
        for (LsEntry en : vector) {   
            fileName = en.getFilename();   
            if (".".equals(fileName) || "..".equals(fileName)) {   
                continue;   
            } else {   
            	deleteDir(path + "/" + fileName);   
            }   
        }   
        // 删除文件夹   
        client.rmdir(path);   
    }   
}
 
public boolean exists(String filename) throws FileSystemException {
    // we have to be connected
    if (channel == null) {
        throw new FileSystemException("Not yet connected to SFTP server");
    }

    // easiest way to check if a file already exists is to do a file stat
    // this method will error out if the remote file does not exist!
    try {
        SftpATTRS attrs = channel.stat(filename);
        // if we get here, then file exists
        return true;
    } catch (SftpException e) {
        // map "no file" message to return correct result
        if (e.getMessage().toLowerCase().indexOf("no such file") >= 0) {
            return false;
        }
        // otherwise, this means an underlying error occurred
        throw new FileSystemException("Underlying error with SFTP session while checking if file exists", e);
    }
}
 
源代码7 项目: localization_nifi   文件: SFTPTransfer.java
@Override
public void deleteFile(final String path, final String remoteFileName) throws IOException {
    final String fullPath = (path == null) ? remoteFileName : (path.endsWith("/")) ? path + remoteFileName : path + "/" + remoteFileName;
    try {
        sftp.rm(fullPath);
    } catch (final SftpException e) {
        switch (e.id) {
            case ChannelSftp.SSH_FX_NO_SUCH_FILE:
                throw new FileNotFoundException("Could not find file " + remoteFileName + " to remove from remote SFTP Server");
            case ChannelSftp.SSH_FX_PERMISSION_DENIED:
                throw new PermissionDeniedException("Insufficient permissions to delete file " + remoteFileName + " from remote SFTP Server", e);
            default:
                throw new IOException("Failed to delete remote file " + fullPath, e);
        }
    }
}
 
@Override
public boolean delete(Path path) throws IOException {
  ChannelSftp channel = null;
  try {
    channel = this.fsHelper.getSftpChannel();
    if (getFileStatus(path).isDirectory()) {
      channel.rmdir(HadoopUtils.toUriPath(path));
    } else {
      channel.rm(HadoopUtils.toUriPath(path));
    }
  } catch (SftpException e) {
    throw new IOException(e);
  } finally {
    safeDisconnect(channel);
  }
  return true;
}
 
源代码9 项目: sftp-fs   文件: SFTPFileSystemTest.java
@Test
public void testCopySFTPFailure() throws IOException {
    addDirectory("/foo");
    addFile("/foo/bar");

    // failure: target parent does not exist

    CopyOption[] options = {};
    NoSuchFileException exception = assertThrows(NoSuchFileException.class,
            () -> fileSystem.copy(createPath("/foo/bar"), createPath("/baz/bar"), options));
    assertEquals("/baz/bar", exception.getFile());

    verify(getExceptionFactory()).createNewOutputStreamException(eq("/baz/bar"), any(SftpException.class), anyCollection());
    assertTrue(Files.isDirectory(getPath("/foo")));
    assertTrue(Files.isRegularFile(getPath("/foo/bar")));
    assertFalse(Files.exists(getPath("/baz")));
    assertFalse(Files.exists(getPath("/baz/bar")));
}
 
源代码10 项目: netbeans   文件: SftpClient.java
@Override
public synchronized int getPermissions(String path) throws RemoteException {
    int permissions = -1;
    try {
        sftpLogger.info("LIST " + path); // NOI18N
        sftpLogger.info(NbBundle.getMessage(SftpClient.class, "LOG_DirListing"));

        ChannelSftp.LsEntry file = getFile(path);
        if (file != null) {
            permissions = file.getAttrs().getPermissions();
        }

        sftpLogger.info(NbBundle.getMessage(SftpClient.class, "LOG_DirectorySendOk"));
    } catch (SftpException ex) {
        LOGGER.log(Level.FINE, "Error while getting permissions for " + path, ex);
    }
    return permissions;
}
 
源代码11 项目: sftp-fs   文件: SFTPFileSystemTest.java
@Test
public void testMoveReplaceFile() throws IOException {
    addDirectory("/foo");
    addDirectory("/foo/bar");
    addFile("/baz");

    CopyOption[] options = {};
    FileSystemException exception = assertThrows(FileSystemException.class,
            () -> fileSystem.move(createPath("/baz"), createPath("/foo/bar"), options));
    assertEquals("/baz", exception.getFile());
    assertEquals("/foo/bar", exception.getOtherFile());

    verify(getExceptionFactory()).createMoveException(eq("/baz"), eq("/foo/bar"), any(SftpException.class));
    assertTrue(Files.isDirectory(getPath("/foo")));
    assertTrue(Files.isDirectory(getPath("/foo/bar")));
    assertTrue(Files.isRegularFile(getPath("/baz")));
}
 
源代码12 项目: netbeans   文件: SftpSupport.java
private StatInfo createStatInfo(String dirName, String baseName, SftpATTRS attrs, ChannelSftp cftp) throws SftpException {
    String linkTarget = null;
    if (attrs.isLink()) {
        String path = dirName + '/' + baseName;
        RemoteStatistics.ActivityID activityID = RemoteStatistics.startChannelActivity("readlink", path); // NOI18N
        try {
            if (LOG.isLoggable(Level.FINE)) {
                LOG.log(Level.FINE, "performing readlink {0}", path);
            }
            linkTarget = cftp.readlink(path);
        } finally {
            RemoteStatistics.stopChannelActivity(activityID);
        }
    }
    Date lastModified = new Date(attrs.getMTime() * 1000L);
    StatInfo result = new FileInfoProvider.StatInfo(baseName, attrs.getUId(), attrs.getGId(), attrs.getSize(),
            attrs.isDir(), attrs.isLink(), linkTarget, attrs.getPermissions(), lastModified);
    return result;
}
 
源代码13 项目: bestconf   文件: SFTPUtil.java
public static void upload(String directory, String uploadFile, ChannelSftp sftp) throws Exception{
	
	File file = new File(uploadFile);
	if(file.exists()){
		
		try {
			Vector content = sftp.ls(directory);
			if(content == null){
				sftp.mkdir(directory);
				System.out.println("mkdir:" + directory);
			}
		} catch (SftpException e) {
			sftp.mkdir(directory);
		}
		sftp.cd(directory);
		System.out.println("directory: " + directory);
		if(file.isFile()){
			InputStream ins = new FileInputStream(file);
			
			sftp.put(ins, new String(file.getName().getBytes(),"UTF-8"));
			
		}else{
			File[] files = file.listFiles();
			for (File file2 : files) {
				String dir = file2.getAbsolutePath();
				if(file2.isDirectory()){
					String str = dir.substring(dir.lastIndexOf(file2.separator));
					directory = directory + str;
				}
				System.out.println("directory is :" + directory);
				upload(directory,dir,sftp);
			}
		}
	}
}
 
源代码14 项目: spring-boot   文件: SftpConnection.java
/**
 * Upload a single file to the sFTP server.未支持断点续传
 *
 * @param localFilePath       Path of the file on local computer
 * @param remoteDirectoryPath path of directory where the file will be stored
 * @return true if the file was uploaded successfully, false otherwise
 * @throws IOException if any network or IO error occurred.
 */
@Override
public void uploadFile(String localFilePath, String remoteDirectoryPath, boolean logProcess) throws FtpException {

    if (!Paths.get(localFilePath).toFile().exists()) {
        throw new FtpException("Unable to upload file, file does not exist :  " + localFilePath);
    }

    if (!existsDirectory(remoteDirectoryPath))
        createDirectory(remoteDirectoryPath);

    try {
        channel.put(localFilePath, remoteDirectoryPath);
    } catch (SftpException e) {
        e.printStackTrace();
        throw new FtpException("Unable to upload file :  " + localFilePath);
    }

    logger.info("upload file succeed : " + localFilePath);
}
 
源代码15 项目: spring-boot   文件: SftpConnection.java
/**
     * Determines whether a directory exists or not
     * 如果是文件,也会抛出异常,返回 false
     *
     * @param remoteDirectoryPath
     * @return true if exists, false otherwise
     * @throws IOException thrown if any I/O error occurred.
     */
    @Override
    public boolean existsDirectory(String remoteDirectoryPath) {

        try {
            // System.out.println(channel.realpath(remoteFilePath));
            SftpATTRS attrs = channel.stat(remoteDirectoryPath);
            return attrs.isDir();
        } catch (SftpException e) {
            // e.printStackTrace();
            return false;
        }


//        String originalWorkingDirectory = getWorkingDirectory();
//        try {
//            changeDirectory(remoteDirectoryPath);
//        } catch (FtpException e) {
//            //文件夹不存在,会抛出异常。
//            return false;
//        }
//        //恢复 ftpClient 当前工作目录属性
//        changeDirectory(originalWorkingDirectory);
//        return true;
    }
 
源代码16 项目: CloverETL-Engine   文件: SFTPOperationHandler.java
private void delete(ChannelSftp channel, Info info, DeleteParameters params) throws IOException, SftpException {
	if (Thread.currentThread().isInterrupted()) {
		throw new IOException(FileOperationMessages.getString("IOperationHandler.interrupted")); //$NON-NLS-1$
	}
	URI uri = info.getURI();
	if (info.isDirectory()) {
		if (params.isRecursive()) {
			List<Info> children = list(uri, channel, LIST_NONRECURSIVE);
			for (Info child: children) {
				delete(channel, child, params);
			}
			channel.rmdir(getPath(uri));
		} else {
			throw new IOException(MessageFormat.format(FileOperationMessages.getString("IOperationHandler.cannot_remove_directory"), uri)); //$NON-NLS-1$
		}
	} else {
		channel.rm(getPath(uri));
	}
}
 
源代码17 项目: sftp-fs   文件: SFTPFileSystemTest.java
@Test
public void testCopyReplaceNonEmptyDirAllowed() throws IOException {
    addDirectory("/foo");
    addFile("/foo/bar");
    addFile("/baz");

    CopyOption[] options = { StandardCopyOption.REPLACE_EXISTING };
    FileSystemException exception = assertThrows(FileSystemException.class,
            () -> fileSystem.copy(createPath("/baz"), createPath("/foo"), options));
    assertEquals("/foo", exception.getFile());

    verify(getExceptionFactory()).createDeleteException(eq("/foo"), any(SftpException.class), eq(true));
    assertTrue(Files.isDirectory(getPath("/foo")));
    assertTrue(Files.isRegularFile(getPath("/foo/bar")));
    assertTrue(Files.isRegularFile(getPath("/baz")));
}
 
源代码18 项目: jumbune   文件: CommandDelegatorMethods.java
/**
 * This method downloads whole directory recursively
 * @param channelSftp
 * @param remotePath remote directory (not file) path
 * @param localPath
 * @throws SftpException
 */
private void downloadDirectory(ChannelSftp channelSftp, String remotePath, String localPath, String user) throws SftpException {
	@SuppressWarnings("unchecked")
	Vector<ChannelSftp.LsEntry> childs = channelSftp.ls(remotePath);
	changeOwnership(localPath, user);
	for (ChannelSftp.LsEntry child : childs) {
		if (child.getAttrs().isDir()) {
			if (CURRENT_DIR.equals(child.getFilename()) || PARENT_DIR.equals(child.getFilename())) {
				continue;
			}
			new File(localPath + File.separator + child.getFilename()).mkdirs();
			changeOwnership(localPath + File.separator + child.getFilename(), user);
			downloadDirectory(channelSftp, remotePath + File.separator + child.getFilename() + File.separator,
					localPath + File.separator + child.getFilename(),user);
		} else {
			channelSftp.get(remotePath + File.separator + child.getFilename(),
					localPath + File.separator + child.getFilename());
			changeOwnership(localPath + File.separator + child.getFilename(), user);
		}
		
	}
}
 
private void delete(ChannelSftp channel, Info info, DeleteParameters params) throws IOException, SftpException {
	if (Thread.currentThread().isInterrupted()) {
		throw new IOException(FileOperationMessages.getString("IOperationHandler.interrupted")); //$NON-NLS-1$
	}
	URI uri = info.getURI();
	if (info.isDirectory()) {
		if (params.isRecursive()) {
			List<Info> children = list(uri, channel, LIST_NONRECURSIVE);
			for (Info child: children) {
				delete(channel, child, params);
			}
			channel.rmdir(getPath(uri));
		} else {
			throw new IOException(MessageFormat.format(FileOperationMessages.getString("IOperationHandler.cannot_remove_directory"), uri)); //$NON-NLS-1$
		}
	} else {
		channel.rm(getPath(uri));
	}
}
 
源代码20 项目: vividus   文件: SftpExecutor.java
@Override
protected SftpOutput executeCommand(ServerConfiguration serverConfiguration, Commands commands, ChannelSftp channel)
        throws JSchException, IOException
{
    channel.setAgentForwarding(serverConfiguration.isAgentForwarding());
    channel.connect();
    SftpOutput executionOutput = new SftpOutput();
    StringBuilder output = new StringBuilder();
    try
    {
        for (SingleCommand<SftpCommand> command : commands.getSingleCommands(SftpCommand::fromString))
        {
            String commandOutput = command.getCommand().execute(channel, command.getParameters());
            if (commandOutput != null)
            {
                if (output.length() > 0)
                {
                    output.append(System.lineSeparator());
                }
                output.append(commandOutput);
            }
        }
    }
    catch (SftpException e)
    {
        softAssert.recordFailedAssertion("SFTP command error", e);
    }
    executionOutput.setResult(output.toString());
    return executionOutput;
}
 
源代码21 项目: vividus   文件: SftpCommandTests.java
@Test
void shouldExecuteCdCommand() throws IOException, SftpException
{
    ChannelSftp channel = mock(ChannelSftp.class);
    String path = "/path";
    String result = SftpCommand.CD.execute(channel, path);
    assertNull(result);
    verify(channel).cd(path);
}
 
源代码22 项目: vividus   文件: SftpCommandTests.java
@Test
void shouldExecutePwdCommand() throws SftpException
{
    ChannelSftp channel = mock(ChannelSftp.class);
    String pwd = "~";
    when(channel.pwd()).thenReturn(pwd);
    String result = SftpCommand.PWD.execute(channel);
    assertEquals(pwd, result);
}
 
源代码23 项目: vividus   文件: SftpCommandTests.java
@Test
void shouldExecuteMkdirCommand() throws IOException, SftpException
{
    ChannelSftp channel = mock(ChannelSftp.class);
    String path = "/dir-to-create";
    String result = SftpCommand.MKDIR.execute(channel, path);
    assertNull(result);
    verify(channel).mkdir(path);
}
 
源代码24 项目: sftp-fs   文件: SFTPFileSystemTest.java
@Test
public void testNewOutputStreamDirectoryDeleteOnClose() throws IOException {
    addDirectory("/foo");

    OpenOption[] options = { StandardOpenOption.DELETE_ON_CLOSE };
    FileSystemException exception = assertThrows(FileSystemException.class, () -> fileSystem.newOutputStream(createPath("/foo"), options));
    assertEquals("/foo", exception.getFile());
    assertEquals(Messages.fileSystemProvider().isDirectory("/foo").getReason(), exception.getReason());

    verify(getExceptionFactory(), never()).createNewOutputStreamException(anyString(), any(SftpException.class), anyCollection());
    assertTrue(Files.isDirectory(getPath("/foo")));
    assertEquals(0, getChildCount("/foo"));
}
 
private List<Info> list(URI parentUri, ChannelSftp channel, ListParameters params) throws IOException, SftpException {
	if (Thread.currentThread().isInterrupted()) {
		throw new IOException(FileOperationMessages.getString("IOperationHandler.interrupted")); //$NON-NLS-1$
	}
	Info rootInfo = info(parentUri, channel);
	if (rootInfo == null) {
		throw new FileNotFoundException(MessageFormat.format(FileOperationMessages.getString("IOperationHandler.file_not_found"), parentUri.toString())); //$NON-NLS-1$
	}
	if (parentUri.toString().endsWith(URIUtils.PATH_SEPARATOR) && !rootInfo.isDirectory()) {
		throw new FileNotFoundException(format(FileOperationMessages.getString("IOperationHandler.not_a_directory"), parentUri)); //$NON-NLS-1$
	}
	if (!rootInfo.isDirectory()) {
		return Arrays.asList(rootInfo);
	} else {
		@SuppressWarnings("unchecked")
		Vector<LsEntry> files = channel.ls(getPath(parentUri));
		List<Info> result = new ArrayList<Info>(files.size());
		for (LsEntry file: files) {
			if ((file != null) && !file.getFilename().equals(URIUtils.CURRENT_DIR_NAME) && !file.getFilename().equals(URIUtils.PARENT_DIR_NAME)) {
				Info child = info(file, null, parentUri, null);
				result.add(child);
				if (params.isRecursive() && file.getAttrs().isDir()) {
					result.addAll(list(child.getURI(), channel, params));
				}
			}
		}
		return result;
	}
}
 
源代码26 项目: MergeProcessor   文件: SftpUtil.java
private void handleMergeFile(String pathFolder, List<IMergeUnit> mergeunits, LsEntry file)
		throws MergeUnitException, SftpException, SftpUtilException {
	String fileName = file.getFilename();
	String attributes = file.getLongname();
	String path = pathFolder + fileName;

	LOGGER.fine(() -> String.format("Getting file fileName=%s, path=%s, attributes=%s", fileName, path, //$NON-NLS-1$
			attributes));
	try (InputStream is = sftpChannel.get(path)) {

		IMergeUnit mergeunit = null;
		if (fileName.endsWith(Configuration.EXTENSION_PLAINMERGE_FILE)
				|| fileName.endsWith(Configuration.SVN_EXTENSION_FILE)
				|| fileName.endsWith(Configuration.SVN_PACKAGE_MERGE_EXTENSION_FILE)) {
			LOGGER.fine(() -> String.format("Parsing SVN merge file %s.", path)); //$NON-NLS-1$
			mergeunit = SVNMergeUnitFactory.createMergeUnitFromPlainMergeFile(configuration, path, fileName,
					is);
		} else if (fileName.endsWith(Configuration.GIT_EXTENSION_FILE)) {
			LOGGER.fine(() -> String.format("Parsing GIT merge file %s.", path)); //$NON-NLS-1$
			mergeunit = GITMergeUnitFactory.create(configuration, Paths.get(path), is);
		} else {
			LOGGER.info(() -> String.format("Skipping file fileName=%s, path=%s, attributes=%s", //$NON-NLS-1$
					fileName, path, attributes));
			return;
		}
		mergeunits.add(mergeunit);
	} catch (IOException e) {
		String message = String.format("Caught exception while parsing merge unit from path=[%s].", path); //$NON-NLS-1$
		throw LogUtil.throwing(new SftpUtilException(message, e));
	}
}
 
源代码27 项目: MergeProcessor   文件: SftpFileSystemProvider.java
/**
 * {@inheritDoc}
 */
@Override
public void write(String filePath, String content) throws IOException {
	try {
		SftpUtil.getInstance().writeToRemotePath(content, filePath);
		LogUtil.getLogger().info(() -> String.format("Write to remote path %s.", filePath));
	} catch (SftpException | SftpUtilException e) {
		throw LogUtil.throwing(new IOException(e));
	}
}
 
源代码28 项目: sftp-fs   文件: SFTPFileSystemTest.java
@Test
public void testDeleteRoot() {

    FileSystemException exception = assertThrows(FileSystemException.class, () -> fileSystem.delete(createPath("/")));
    assertEquals("/", exception.getFile());

    verify(getExceptionFactory()).createDeleteException(eq("/"), any(SftpException.class), eq(true));
}
 
源代码29 项目: sftp-fs   文件: SFTPFileSystemTest.java
@Test
public void testReadSymbolicLinkNoLinkButFile() throws IOException {
    addFile("/foo");

    FileSystemException exception = assertThrows(FileSystemException.class, () -> fileSystem.readSymbolicLink(createPath("/foo")));
    assertEquals("/foo", exception.getFile());

    verify(getExceptionFactory()).createReadLinkException(eq("/foo"), any(SftpException.class));
}
 
@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);
  }
}