我正在尝试使用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
,它与文件的实际内容不匹配。
Here是调试期间fo FileObject的屏幕截图。
答案 0 :(得分:0)
@Slaw所说,您需要从文件内容中获取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();
}