在java中读取需要用户名和密码的远程文件 [英] Read remote file in java which needs username and password
本文介绍了在java中读取需要用户名和密码的远程文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在java中读取远程文件
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.
有没有办法可以通过java代码传递参数并读取文件?
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中读取需要用户名和密码的远程文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文