【发布时间】:2017-06-07 11:03:00
【问题描述】:
我正在使用 Apache Commons VFS2 将文件上传到服务器。下面是相同的代码。我拥有所有凭据。代码也打印了“文件上传成功”字符串。但是,当我交叉检查它时,我在服务器上找不到该文件。代码中有什么我遗漏的吗?
我有所有需要的罐子(Apache Commons VFS 罐子、JSH 罐子)
public static void main(String[] args) {
SendMyFiles sendMyFiles = new SendMyFiles();
sendMyFiles.startFTP("C:/useragent.log");
}
public boolean startFTP(String fileToFTP) {
props = new Properties();
StandardFileSystemManager manager = new StandardFileSystemManager();
try {
// props.load(new FileInputStream("properties/" +
// propertiesFilename));
String serverAddress = "10.111.111.11";
String userId = "username";
String password = "password";
String remoteDirectory = "local/home/client/files/";
// check if the file exists
String filepath = fileToFTP;
File file = new File(filepath);
if (!file.exists())
throw new RuntimeException("Error. Local file not found");
// Initializes the file manager
manager.init();
// Setup our SFTP configuration
FileSystemOptions opts = new FileSystemOptions();
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");
SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);
// Create the SFTP URI using the host name, userid, password, remote
// path and file name
String sftpUri = "sftp://" + userId + ":" + password + "@" + serverAddress + "/" + remoteDirectory;
// Create local file object
FileObject localFile = manager.resolveFile(file.getAbsolutePath());
// Create remote file object
FileObject remoteFile = manager.resolveFile(sftpUri, opts);
// Copy local file to sftp server
remoteFile.copyFrom(localFile, Selectors.SELECT_SELF);
System.out.println("File upload successful");
} catch (Exception ex) {
ex.printStackTrace();
return false;
} finally {
manager.close();
}
return true;
}
【问题讨论】:
-
这里有什么问题?
-
代码运行没有任何异常,但我在服务器上找不到我从代码上传的文件。我想在代码中添加一些东西吗?
-
向我们展示一个独立 SFTP 客户端(例如 WinSCP)的日志文件,其中列出了上传后的目录
/local/home/client/files。
标签: java sftp apache-commons-vfs