I am trying to read a remote file in java
File f = new File("//192.168.1.120/home/hustler/file.txt");
The remote machine needs a Username and Password to allow me to access the file.
Is there a way I could pass the parameters through the java code and read the file?
解决方案
Here is the code, I've written and it is working Perfectly.
File f=new File("abc.txt"); //Takes the default path, else, you can specify the required path
if(f.exists())
{
f.delete();
}
f.createNewFile();
FileObject destn = VFS.getManager().resolveFile(f.getAbsolutePath());
UserAuthenticator auth = new StaticUserAuthenticator("", "myusername", "secret_password");
FileSystemOptions opts = new FileSystemOptions();
DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);
FileObject fo = VFS.getManager().resolveFile("\\192.168.0.1\direcory\to\GetData\sourceFile.txt",opts);
destn.copyFrom(fo,Selectors.SELECT_SELF);
destn.close();
Now you can use the file to perform the required operations. Something like...
InputStream is = new FileInputStream(f);
最后
以上就是包容草丛最近收集整理的关于java读取带密码的文件,在java中读取需要用户名和密码的远程文件的全部内容,更多相关java读取带密码内容请搜索靠谱客的其他文章。
发表评论 取消回复