SFTP文件使用Apache Commons VFS2传输



我正在使用Apache Commons VFS2将文件上传到服务器。以下是相同的代码。我有所有的证书。代码也打印出"成功上传的文件"字符串。但是,当我交叉检查时,我在服务器上找不到文件。我在代码中有什么我缺少的吗?

我有所需的所有罐子(Apache Commons VFS Jars,JSH Jar(

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;
}

我假设sftpUri应该是通往目标文件的途径,而不是目录:

String sftpUri =
    "sftp://" + userId + ":" + password + "@" + serverAddress + "/" + remoteDirectory + "/" +
    file.getName(); 

相关内容

  • 没有找到相关文章

最新更新