gpt4 book ai didi

java - 使用 Apache VFS2 复制文件夹导致错误 Caused by : org. apache.commons.vfs2.FileSystemException: Expecting/to follow the hostname in URI

转载 作者:行者123 更新时间:2023-11-30 09:06:34 25 4
gpt4 key购买 nike

我尝试使用 apache VFS2 将文件夹从网络复制到本地系统。我在他们的 pom.xml 中添加了来自 Maven 的所有依赖项。但我仍然收到此错误“Caused by: org.apache.commons.vfs2.FileSystemException:

期望/跟随 URI 中的主机名“由:org.apache.commons.vfs2.FileSystemException:期望/跟随 URI 中的主机名”sftp://user:*****@network/opt/file.txt"**

我搜索了 SO 站点、stackexchange、 super 用户并找到了这个问题主题,建议的补救措施是缺少 jar。有人请对这个问题有所了解,请提供我要用 intellij 下载的 jar 的确切列表。

我正在使用此代码。

import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.FileSystemOptions;
import org.apache.commons.vfs2.Selectors;
import org.apache.commons.vfs2.impl.StandardFileSystemManager;
import org.apache.commons.vfs2.provider.sftp.SftpFileSystemConfigBuilder;

import java.io.File;

/**
* The class SFTPUtil containing uploading, downloading, checking if file exists
* and deleting functionality using Apache Commons VFS (Virtual File System)
* Library
*
* @author Ashok
*
*/
public class SFTPUtility {

public static void main(String[] args) {
String hostName = "10.2.55.66";
String username = "user";
String password = "password@network";

String localFilePath = "/opt/FakeFile.txt";
String remoteFilePath = "/opt/file.txt";
String remoteTempFilePath = "/tmp";

//upload(hostName, username, password, localFilePath, remoteFilePath);
exist(hostName, username, password, remoteFilePath);
download(hostName, username, password, localFilePath,remoteFilePath);
//move(hostName, username, password, remoteFilePath, remoteTempFilePath);
//delete(hostName, username, password, remoteFilePath);
}

/**
* Method to upload a file in Remote server
*
* @param hostName
* HostName of the server
* @param username
* UserName to login
* @param password
* Password to login
* @param localFilePath
* LocalFilePath. Should contain the entire local file path -
* Directory and Filename with \\ as separator
* @param remoteFilePath
* remoteFilePath. Should contain the entire remote file path -
* Directory and Filename with / as separator
*/
public static void upload(String hostName, String username, String password, String localFilePath, String remoteFilePath) {

File file = new File(localFilePath);
if (!file.exists())
throw new RuntimeException("Error. Local file not found");

StandardFileSystemManager manager = new StandardFileSystemManager();

try {
manager.init();

// Create local file object
FileObject localFile = manager.resolveFile(file.getAbsolutePath());

// Create remote file object
FileObject remoteFile = manager.resolveFile(createConnectionString(hostName, username, password, remoteFilePath), createDefaultOptions());
/*
* use createDefaultOptions() in place of fsOptions for all default
* options - Ashok.
*/

// Copy local file to sftp server
remoteFile.copyFrom(localFile, Selectors.SELECT_SELF);

System.out.println("File upload success");
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
manager.close();
}
}

public static boolean move(String hostName, String username, String password, String remoteSrcFilePath, String remoteDestFilePath){
StandardFileSystemManager manager = new StandardFileSystemManager();

try {
manager.init();

// Create remote object
FileObject remoteFile = manager.resolveFile(createConnectionString(hostName, username, password, remoteSrcFilePath), createDefaultOptions());
FileObject remoteDestFile = manager.resolveFile(createConnectionString(hostName, username, password, remoteDestFilePath), createDefaultOptions());

if (remoteFile.exists()) {
remoteFile.moveTo(remoteDestFile);;
System.out.println("Move remote file success");
return true;
}
else{
System.out.println("Source file doesn't exist");
return false;
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
manager.close();
}
}

/**
* Method to download the file from remote server location
*
* @param hostName
* HostName of the server
* @param username
* UserName to login
* @param password
* Password to login
* @param localFilePath
* LocalFilePath. Should contain the entire local file path -
* Directory and Filename with \\ as separator
* @param remoteFilePath
* remoteFilePath. Should contain the entire remote file path -
* Directory and Filename with / as separator
*/
public static void download(String hostName, String username, String password, String localFilePath, String remoteFilePath) {

StandardFileSystemManager manager = new StandardFileSystemManager();

try {
manager.init();

// Append _downlaod_from_sftp to the given file name.
//String downloadFilePath = localFilePath.substring(0, localFilePath.lastIndexOf(".")) + "_downlaod_from_sftp" + localFilePath.substring(localFilePath.lastIndexOf("."), localFilePath.length());

// Create local file object. Change location if necessary for new downloadFilePath
FileObject localFile = manager.resolveFile(localFilePath);

// Create remote file object
FileObject remoteFile = manager.resolveFile(createConnectionString(hostName, username, password, remoteFilePath), createDefaultOptions());

// Copy local file to sftp server
localFile.copyFrom(remoteFile, Selectors.SELECT_SELF);

System.out.println("File download success");
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
manager.close();
}
}

/**
* Method to delete the specified file from the remote system
*
* @param hostName
* HostName of the server
* @param username
* UserName to login
* @param password
* Password to login
* @param localFilePath
* LocalFilePath. Should contain the entire local file path -
* Directory and Filename with \\ as separator
* @param remoteFilePath
* remoteFilePath. Should contain the entire remote file path -
* Directory and Filename with / as separator
*/
public static void delete(String hostName, String username, String password, String remoteFilePath) {
StandardFileSystemManager manager = new StandardFileSystemManager();

try {
manager.init();

// Create remote object
FileObject remoteFile = manager.resolveFile(createConnectionString(hostName, username, password, remoteFilePath), createDefaultOptions());

if (remoteFile.exists()) {
remoteFile.delete();
System.out.println("Delete remote file success");
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
manager.close();
}
}

// Check remote file is exist function:
/**
* Method to check if the remote file exists in the specified remote
* location
*
* @param hostName
* HostName of the server
* @param username
* UserName to login
* @param password
* Password to login
* @param remoteFilePath
* remoteFilePath. Should contain the entire remote file path -
* Directory and Filename with / as separator
* @return Returns if the file exists in the specified remote location
*/
public static boolean exist(String hostName, String username, String password, String remoteFilePath) {
StandardFileSystemManager manager = new StandardFileSystemManager();

try {
manager.init();

// Create remote object
FileObject remoteFile = manager.resolveFile(createConnectionString(hostName, username, password, remoteFilePath), createDefaultOptions());

System.out.println("File exist: " + remoteFile.exists());

return remoteFile.exists();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
manager.close();
}
}

/**
* Generates SFTP URL connection String
*
* @param hostName
* HostName of the server
* @param username
* UserName to login
* @param password
* Password to login
* @param remoteFilePath
* remoteFilePath. Should contain the entire remote file path -
* Directory and Filename with / as separator
* @return concatenated SFTP URL string
*/
public static String createConnectionString(String hostName, String username, String password, String remoteFilePath) {
if(remoteFilePath.startsWith("/")||remoteFilePath.startsWith("\\"))
remoteFilePath=remoteFilePath.substring(1);
return "sftp://" + username + ":" + password + "@" + hostName + "/" + remoteFilePath;
}

/**
* Method to setup default SFTP config
*
* @return the FileSystemOptions object containing the specified
* configuration options
* @throws FileSystemException
*/
public static FileSystemOptions createDefaultOptions() throws FileSystemException {
// Create SFTP options
FileSystemOptions opts = new FileSystemOptions();

// SSH Key checking
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");

/*
* Using the following line will cause VFS to choose File System's Root
* as VFS's root. If I wanted to use User's home as VFS's root then set
* 2nd method parameter to "true"
*/
// Root directory set to user home
SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, false);

// Timeout is count by Milliseconds
SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);

return opts;
}
}

最佳答案

请指定文件位置如下并检查

如果是本地文件,

file:///home/someuser/somedir       
file:///C:/DocumentsandSettings
file://///somehost/test/text.xml

或者如果是ftp,

> ftp://myusername:mypassword@hostname/test/test.tar.gz

关于java - 使用 Apache VFS2 复制文件夹导致错误 Caused by : org. apache.commons.vfs2.FileSystemException: Expecting/to follow the hostname in URI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24401930/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com