我需要一个纯Apache Commons VFS解决方案/代码/示例
通过SFTP电话.
解决方案应该只使用Apache Commons VFS api,不应该引用底层的JSCH库.
我们正在尝试使用Apache Commons VFS通过SFTP访问某些内容.它声称支持SFTP,但是当它启动时我们会收到MalformedURLException.挖掘我看到apache vfs正在使用java.net.URL.但是文档告诉我它实际上不支持sftp://URL.
保证在搜索路径上存在以下协议的协议处理程序: -
Run Code Online (Sandbox Code Playgroud)http, https, ftp, file, and jar也可以使用其他协议的协议处理程序.
我在Linux上使用java 1.6.0.如何防止java.net.URL在看到sftp://URL 时摇摆不定?我需要继续使用Apache commons VFS库,它使用java.net.URL.
我正在使用Apache Commons VFS2(虚拟文件系统)来监视目录中的更改文件.org.apache.commons.vfs2.FileListener回来org.apache.commons.vfs2.FileObject.如何将a org.apache.commons.vfs2.FileObject转换为java.io.File
我是使用Apache Common vfs的新手,我成功连接到我已经阅读过docs的服务器但是我坚持使用这段代码.我如何列出目录/文件?
....
Session session = null;
FileSystemManager fsManager = null;
FileSystem fs = null;
try {
String host = "host_here";
int port = 22;
String userStr = "user_here";
char [] username = userStr.toCharArray();
String passStr = "password_here";
char [] password = passStr.toCharArray();
session = SftpClientFactory.createConnection(host, port, username, password, null);
//session.connect();
System.out.println("Connected to the server");
FileSystemOptions opts = new FileSystemOptions();
fsManager = VFS.getManager();
FileObject file = fsManager.resolveFile("ftp://"+userStr+":"+passStr+"@"+host+"/home/", opts);
// .... whats next i do here? .....
} catch (Exception …Run Code Online (Sandbox Code Playgroud) 我正在考虑将Apache Commons VFS用于需要通过ftp,sftp和https在本地服务器和远程服务器之间传输文件的项目.
标准用法示例是从静态方法获取FileSystemManager
FileSystemManager fsManager = VFS.getManager();
Run Code Online (Sandbox Code Playgroud)
跨多个线程使用相同的FileSystemManager是否安全?
第二个问题是关于在finally块中正确释放资源:我在Javadoc API中找到以下方法:
但我不清楚哪些资源通常应该被关闭.
我有一个SFTP(Windows与WinSSHD)的问题.我尝试用Apache Commons VFS在一个文件夹中写一个文件.在本地SFTP上我没有上传的问题但在第二个SFTP上我总是得到以下错误.
FTP看起来像这样:

我需要上传到文件夹"alis".奇怪的是它没有User/Group和770权限.但是,使用FileZilla文件上传工作正常(使用相同的登录).
在文件夹"alis"上执行"manager.resolveFile()"(我尝试上传到此文件夹)并打印".getType()"我获取信息"文件"而不是预期的"文件夹".
有没有人知道为什么VFS会将文件夹识别为文件或为什么上传不起作用?
将文件上载到SFTP时的例外情况:
Exception in thread "main" java.lang.RuntimeException: org.apache.commons.vfs2.FileSystemException: Could not copy "file:///D:/Test/test.txt" to "sftp://user:***@host/.../alis/test.txt".
at test.Test.upload(Test.java:77)
at test.Test.main(Test.java:22)
Caused by: org.apache.commons.vfs2.FileSystemException: Could not copy "file:///D:/Test/test.txt" to "sftp://user:***@host/.../alis/test.txt".
at org.apache.commons.vfs2.provider.AbstractFileObject.copyFrom(AbstractFileObject.java:1062)
at test.Test.upload(Test.java:73)
... 1 more
Caused by: org.apache.commons.vfs2.FileSystemException: Could not create folder "sftp://user:***@host/.../alis" because it already exists and is a file.
at org.apache.commons.vfs2.provider.AbstractFileObject.createFolder(AbstractFileObject.java:968)
at org.apache.commons.vfs2.provider.AbstractFileObject.getOutputStream(AbstractFileObject.java:1424)
at org.apache.commons.vfs2.provider.DefaultFileContent.getOutputStream(DefaultFileContent.java:461)
at org.apache.commons.vfs2.provider.DefaultFileContent.getOutputStream(DefaultFileContent.java:441)
at org.apache.commons.vfs2.FileUtil.copyContent(FileUtil.java:111)
at org.apache.commons.vfs2.provider.AbstractFileObject.copyFrom(AbstractFileObject.java:1053)
... 2 more
Run Code Online (Sandbox Code Playgroud)
源代码:(运行示例,您需要"jsch-0.1.50.jar")
import java.io.File;
import org.apache.commons.vfs2.FileObject; …Run Code Online (Sandbox Code Playgroud) 我正在尝试将本地系统文件复制到服务器
package classes;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;
import org.apache.commons.vfs.FileObject;
import org.apache.commons.vfs.FileSystemOptions;
import org.apache.commons.vfs.Selectors;
import org.apache.commons.vfs.impl.StandardFileSystemManager;
import org.apache.commons.vfs.provider.sftp.SftpFileSystemConfigBuilder;
public class SendMyFiles {
public static void main(String[] args) {
SendMyFiles sendMyFiles = new SendMyFiles();
String fileToFTP = "zcol_30092013.xls";
sendMyFiles.startFTP(fileToFTP);
}
public boolean startFTP(String fileToFTP){
Properties prop = new Properties();
InputStream in = getClass().getResourceAsStream("/config.properties");
StandardFileSystemManager manager = new StandardFileSystemManager();
try {
prop.load(in);
String serverAddress = prop.getProperty("serverAddress").trim();
String userId = prop.getProperty("userId").trim();
String password = prop.getProperty("password").trim();
String remoteDirectory = prop.getProperty("remoteDirectory").trim();
String …Run Code Online (Sandbox Code Playgroud) VFS 方法无法处理由 JBoss解析的此 URI${jboss.server.temp.dir}/local/outgoing配置。当我尝试解析 URI 并获取文件时,它会引发异常。任何想法可能是什么问题?jboss-beans.xml"C:\\Download\\jboss-eap-5.1.1\\server\\default\\tmp/local/outgoing"
Exception
17:35:25,024 ERROR [VfsSynchronizerConfImpl] File FromOutgoing cannot be resolved, FileSystemException:
org.apache.commons.vfs2.FileSystemException: Could not find file with URI "C:\Download\jboss-eap-5.1.1\server\default\tmp/local/outgoing" because it is a relative path, and no base URI was provided.
at org.apache.commons.vfs2.impl.DefaultFileSystemManager.resolveFile(DefaultFileSystemManager.java:719)
at org.apache.commons.vfs2.impl.DefaultFileSystemManager.resolveFile(DefaultFileSystemManager.java:649)
at org.apache.commons.vfs2.impl.DefaultFileSystemManager.resolveFile(DefaultFileSystemManager.java:605)
Run Code Online (Sandbox Code Playgroud)
DefaultFileSystemManager.class methods
public FileObject resolveFile(final String uri) throws FileSystemException
-- this method calls the method below
public FileObject resolveFile(final FileObject baseFile, final String uri,
final FileSystemOptions fileSystemOptions)
throws FileSystemException
-- this method cannot process …Run Code Online (Sandbox Code Playgroud) 我有Commons VFS的问题.我想使用SFTP连接到一个目录并列出它.这是代码:
FileSystemOptions opts = new FileSystemOptions();
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");
FileSystemManager fsManager = VFS.getManager();
FileObject sourceDir = fsManager.resolveFile(sourceUrl, opts);
FileObject targetDir = fsManager.resolveFile(config.get("to"));
for (FileObject sourceFile : sourceDir.getChildren()) { // here is the problem
FileObject targetFile = fsManager.resolveFile(targetDir + "/" + nodeName + "_"
+ sourceFile.getName().getBaseName());
logger.debug("Copying files. Source: " + sourceFile.getName().getPath() + " Target: "
+ targetFile.getName().getPath());
}
Run Code Online (Sandbox Code Playgroud)
似乎fsManager正确解决它但getChildren()失败了.我发现这sourceDir是一种IMAGINARY类型; 以下代码说明了这一点
logger.debug(sourceDir.getType());
Run Code Online (Sandbox Code Playgroud)
我检查了文件,它是一个普通的文件夹 - 不是链接或类似的东西.
错误
org.apache.commons.vfs2.FileNotFolderException: Could not list the contents of …Run Code Online (Sandbox Code Playgroud) 目前我正在使用apache vfs2从sftp下载文件.对于身份验证,我使用用户名和密码.
有没有办法只使用vfs2与public-private-keys和没有密码?
我想我已经使用了这个功能,但是怎么样?将其设置为"是"?
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(options, "no");
Run Code Online (Sandbox Code Playgroud)
这是我目前的代码(片段):
private boolean downloadFile(){
StandardFileSystemManager sysManager = new StandardFileSystemManager();
//download der Datei
try {
sysManager.init();
FileObject localFile = sysManager.resolveFile(localFilePath);
FileObject remoteFile = sysManager.resolveFile(createConnectionString(host, user, password, fileName, port),createDefaultOptions());
//Selectors.SELECT_FILES --> A FileSelector that selects only the base file/folder.
localFile.copyFrom(remoteFile, Selectors.SELECT_FILES);
} catch (Exception e) {
logger.error("Downloading file failed: " + e.toString());
return false;
}finally{
sysManager.close();
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
和
private FileSystemOptions createDefaultOptions() throws FileSystemException{
//create options for sftp
FileSystemOptions options = new FileSystemOptions();
//ssh …Run Code Online (Sandbox Code Playgroud)