如何读取 "FileObject "apache类对象的内容?

问题描述 投票:0回答:1

我试图使用org.apache.commons.vfs2来读取远程机器上的一个文件。我能够在那台机器上找到文件,并将其存储到FileObject类中,但我似乎找不到使用该FileObject读取文件内容的方法。

我在 home.apache.org getContent()方法返回文件的内容。然而,我并没有得到预期的结果。

下面是我的代码。

import org.apache.commons.vfs2.*;
import org.apache.commons.vfs2.auth.StaticUserAuthenticator;
import org.apache.commons.vfs2.impl.DefaultFileSystemConfigBuilder;

public class ReadGridConfig {

    public static void RemoteGrid() throws IOException {
        String domain="#############.com";
        String userName="######";
        String password="#########";
        String remoteFilePath = "\\\\##.#.##.###\\C$\\Grid\\node_5555.json";


        //domain, username, password
        UserAuthenticator auth=new StaticUserAuthenticator(domain, userName, password);
        FileSystemOptions opts=new FileSystemOptions();
        DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);

        FileObject fo=VFS.getManager().resolveFile(remoteFilePath,opts);

        System.out.println(fo.exists());
        System.out.println(fo.getContent());

        fo.close();

    }
}

这些是我在控制台得到的结果:

Picked up _JAVA_OPTIONS: -Djava.net.preferIPv4Stack=true
Connected to the target VM, address: '###.#.#.#:#####', transport: 'socket'
OpenJDK 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended
11:24:06.760 [main] DEBUG org.apache.commons.vfs2.impl.StandardFileSystemManager - Using "C:\Users\H344195\AppData\Local\Temp\vfs_cache" as temporary files store.
11:24:06.839 [main] DEBUG org.apache.commons.vfs2.impl.StandardFileSystemManager - Skipping provider "org.apache.commons.vfs2.provider.tar.TarFileProvider" because required class "org.apache.commons.compress.archivers.tar.TarArchiveOutputStream" is not available.
11:24:06.840 [main] DEBUG org.apache.commons.vfs2.impl.StandardFileSystemManager - Skipping provider "org.apache.commons.vfs2.provider.bzip2.Bzip2FileProvider" because required class "org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream" is not available.
11:24:06.846 [main] DEBUG org.apache.commons.vfs2.impl.StandardFileSystemManager - Skipping provider "org.apache.commons.vfs2.provider.ftp.FtpFileProvider" because required class "org.apache.commons.net.ftp.FTPFile" is not available.
11:24:06.846 [main] DEBUG org.apache.commons.vfs2.impl.StandardFileSystemManager - Skipping provider "org.apache.commons.vfs2.provider.ftps.FtpsFileProvider" because required class "org.apache.commons.net.ftp.FTPFile" is not available.
11:24:06.847 [main] DEBUG org.apache.commons.vfs2.impl.StandardFileSystemManager - Skipping provider "org.apache.commons.vfs2.provider.http.HttpFileProvider" because required class "org.apache.commons.httpclient.HttpClient" is not available.
11:24:06.847 [main] DEBUG org.apache.commons.vfs2.impl.StandardFileSystemManager - Skipping provider "org.apache.commons.vfs2.provider.https.HttpsFileProvider" because required class "org.apache.commons.httpclient.HttpClient" is not available.
11:24:06.847 [main] DEBUG org.apache.commons.vfs2.impl.StandardFileSystemManager - Skipping provider "org.apache.commons.vfs2.provider.http4.Http4FileProvider" because required class "org.apache.http.client.HttpClient" is not available.
11:24:06.848 [main] DEBUG org.apache.commons.vfs2.impl.StandardFileSystemManager - Skipping provider "org.apache.commons.vfs2.provider.http4s.Http4sFileProvider" because required class "org.apache.http.client.HttpClient" is not available.
11:24:06.849 [main] DEBUG org.apache.commons.vfs2.impl.StandardFileSystemManager - Skipping provider "org.apache.commons.vfs2.provider.sftp.SftpFileProvider" because required class "com.jcraft.jsch.JSch" is not available.
11:24:06.850 [main] DEBUG org.apache.commons.vfs2.impl.StandardFileSystemManager - Skipping provider "org.apache.commons.vfs2.provider.webdav.WebdavFileProvider" because required class "org.apache.commons.httpclient.HttpClient" is not available.
11:24:06.850 [main] DEBUG org.apache.commons.vfs2.impl.StandardFileSystemManager - Skipping provider "org.apache.commons.vfs2.provider.tar.TarFileProvider" because required scheme "tar" is not available.
11:24:06.850 [main] DEBUG org.apache.commons.vfs2.impl.StandardFileSystemManager - Skipping provider "org.apache.commons.vfs2.provider.tar.TarFileProvider" because required scheme "bz2" is not available.
11:24:06.851 [main] DEBUG org.apache.commons.vfs2.impl.StandardFileSystemManager - Skipping provider "org.apache.commons.vfs2.provider.hdfs.HdfsFileProvider" because required class "org.apache.hadoop.fs.FileSystem" is not available.
11:24:06.874 [main] DEBUG org.apache.commons.vfs2.cache.SoftRefFilesCache - putFile: file:////##.#.##.###/C$/Grid/node_5555.json
true
org.apache.commons.vfs2.provider.DefaultFileContent@5c909414

System.out.println(fo.exists()); 返回的结果是 true. 所以我知道,我能够定位文件。

但对于 System.out.println(fo.getContent()); 我本以为会得到.json文件的内容,但我得到的却是 org.apache.commons.vfs2.provider.DefaultFileContent@5c909414 这与文件的实际内容不符。

这里的 是调试时fo FileObject的截图。

java apache io remote-access apache-commons-vfs
1个回答
0
投票

正如@Slaw所说,你需要从File内容中获取InputStream,然后从InputStream中读取字节。

try {
    FileSystemManager fileSystemManager = VFS.getManager();
    FileObject fileToread = fileSystemManager.resolveFile("<file-path>");
    BufferedInputStream inputStreamReader = new BufferedInputStream(fileToread.getContent().getInputStream());
    String content = "";
    while (inputStreamReader.available() > 0) {
        char c = (char) inputStreamReader.read();
        content = content.concat(String.valueOf(c));
    }
    //do something from content
} catch (FileSystemException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
© www.soinside.com 2019 - 2024. All rights reserved.