Tun*_*enX 4 java 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
Run Code Online (Sandbox Code Playgroud)
我想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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果我使用“ file:///”,那么在解析本地文件时会出现以下异常:
org.apache.commons.vfs2.FileSystemException: Unknown scheme "file" in URI "file:///C:\Temp\test.txt".
Run Code Online (Sandbox Code Playgroud)
如果我省略“ 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.
Run Code Online (Sandbox Code Playgroud)
如果我在localFileObject之前执行remoteFileObject,那么我得到
org.apache.commons.vfs2.FileSystemException: Unknown scheme "sftp" in URI "sftp://xxx.xxx.xxx.xxx:22/~/temp/test.txt"
Run Code Online (Sandbox Code Playgroud)
请帮忙。
| 归档时间: |
|
| 查看次数: |
4406 次 |
| 最近记录: |