我正在尝试使用org.apache.commons.vfs2读取远程计算机上的文件。我能够在该机器上找到该文件并将其存储到 FileObject 类中,但我似乎找不到使用该 FileObject 读取文件内容的方法。
我在getContent((方法返回文件内容 home.apache.org 上阅读了它。但是,我没有从中得到预期的结果。
下面是我的代码:
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:UsersH344195AppDataLocalTempvfs_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
。
这是调试期间文件对象的屏幕截图。
正如@Slaw所说,您需要从文件内容中获取输入流,然后从输入流中读取字节。
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();
}