Apache commons VFS неизвестный файл схемы
Я даже зашел на страницу Google № 5, но не получил ответа... У меня есть следующее (я проверил войну, и она тоже там):
commons-collections4-4.0.jar
commons-io-2.4.jar
commons-lang3-3.3.2.jar
commons-logging-1.2.jar
commons-net-3.3.jar
commons-vfs2-2.0.jar
jackrabbit-standalone-2.8.0.jar
javax.mail-1.4.4.jar
jcifs-1.3.18.jar
jsch-0.1.51.jar
primefaces-5.0.jar
Я хочу 1) создать локальный файл и 2) скопировать этот файл через sftp на мой сервер. Файл создается локально в правильном каталоге с правильными данными.
Мой код обрезан:
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystem;
import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.FileSystemOptions;
import org.apache.commons.vfs2.Selectors;
import org.apache.commons.vfs2.UserAuthenticator;
import org.apache.commons.vfs2.auth.StaticUserAuthenticator;
import org.apache.commons.vfs2.impl.DefaultFileSystemConfigBuilder;
import org.apache.commons.vfs2.impl.StandardFileSystemManager;
import org.apache.commons.vfs2.provider.sftp.SftpFileSystemConfigBuilder;
...
public void dosftp(String data) throws FileSystemException
{
String username = "username";
String password = "password";
String fileName = "test.txt";
String localPath = "C:\\Temp\\";
String fullPath = "sftp://xxx.xxx.xxx.xxx:22/~/temp/" + fileName;
StandardFileSystemManager fsManager = null;
FileObject remoteFileObject = null;
FileObject localFileObject = null;
try
{
File file = new File(localPath + fileName);
FileUtils.writeStringToFile(file, data);
fsManager = new StandardFileSystemManager();
FileSystemOptions opts = new FileSystemOptions();
//Do auth stuff
UserAuthenticator auth = new StaticUserAuthenticator(null, username, password);
DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);
try
{
URI uri = new URI(fullPath);
if(StringUtils.equalsIgnoreCase(uri.getScheme(), "sftp"))
{
LOG.info("Scheme is sftp");
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");
SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);
}
catch(URISyntaxException e)
{
LOG.log(Level.WARNING,"Error while checking file's uri", e);
}
}
localFileObject = fsManager.resolveFile("file:///" + file.getAbsolutePath());
remoteFileObject = fsManager.resolveFile(fullPath, opts);
remoteFileObject.copyFrom(localFileObject, Selectors.SELECT_SELF);
}
catch (FileSystemException e)
{
LOG.log(Level.SEVERE, "Error while writing file out", e);
}
catch (IOException e)
{
LOG.log(Level.SEVERE, "Error while writing file out", e);
}
finally
{
if((fsManager != null) && (remoteFileObject != null))
{
FileSystem fs = null;
remoteFileObject.close(); // Seems to still work even if this line is omitted
fs = remoteFileObject.getFileSystem(); // This works even after the src is closed.
fsManager.closeFileSystem(fs);
}
if((fsManager != null) && (localFileObject != null))
{
FileSystem fs = null;
localFileObject.close(); // Seems to still work even if this line is omitted
fs = localFileObject.getFileSystem(); // This works even after the src is closed.
fsManager.closeFileSystem(fs);
}
}
}
Если я использую "file:///", то получаю следующее исключение, где я разрешаю локальный файл:
org.apache.commons.vfs2.FileSystemException: Unknown scheme "file" in URI "file:///C:\Temp\test.txt".
Если я опущу "file:///", то получу:
org.apache.commons.vfs2.FileSystemException: Could not find file with URI "C:\Temp\test.txt" because it is a relative path, and no base URI was provided.
Если я делаю remoteFileObject до localFileObject, то я получаю
org.apache.commons.vfs2.FileSystemException: Unknown scheme "sftp" in URI "sftp://xxx.xxx.xxx.xxx:22/~/temp/test.txt"
Пожалуйста помоги.
2 ответа
Решение
Вы должны вызвать init() для экземпляра StandardFileSystemManager, чтобы он загружал схемы из файла "provider.xml" (который должен находиться в вашем пути к классам).
Вы также можете инициализировать программно
DefaultFileSystemManager currMan = new DefaultFileSystemManager();
currMan.addProvider(currSchemeStr, new AzFileProvider());
currMan.addProvider("file", new DefaultLocalFileProvider());
currMan.init();
currMan.resolveFile("file:///tmp" + + file.getAbsolutePath());