0

OS: macbook

I wanna read remote file in Java. I test it on my local computer first.

  1. I tried to use File, but you guys told me File can only be used on local file system, thanks.

    public static void main(String[] args) throws URISyntaxException {
        URI uri = new URI("file://127.0.0.1/Users/jian/Public/logfiles/Hadoop/hdp_log1.log");
        File file = new File(uri);
        System.out.println(file.exists());
    }
    
  2. I find a library for Macbook, Samba JCIFS, from this link access to file using Java with Samba JCIFS . Here is my code :

    public static void main(String[] args) throws URISyntaxException, MalformedURLException, SmbException {
        NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("","jian", "ywnk");
        SmbFile smbFile = new SmbFile("smb://127.0.0.1/Users/jian/Public/logfiles/Hadoop/hdp_log1.log",auth);
        System.out.println(smbFile.exists()); 
    }
    

Then I got this error:
Exception in thread "main" jcifs.smb.SmbAuthException: Logon failure: unknown user name or bad password.
username is the same as I run who am i in terminal, and password is my login password. I don't know why I get this username or password incorrect error.
Thanks for your guys help.

Community
  • 1
  • 1
Jian
  • 57
  • 1
  • 9
  • Does this help: http://stackoverflow.com/questions/11724576/read-remote-file-in-java-which-needs-username-and-password ? To do sth with the file it seems you need to copy it to a local drive. – Crine Mar 25 '16 at 17:35
  • Take a look at [this link](http://www.rfc-base.org/txt/rfc-3986.txt). It contains information about uri authority component. Basically, `hostname:port` forms the authority component. – abhisheknirmal Mar 25 '16 at 17:41
  • I'm not sure what authority component means exactly but it sounds like it could be a permission issue. – Sam Orozco Mar 25 '16 at 17:47
  • This is not a legal URI to use with `File`. And why `File`? This is 2016, [you should not use `File` anymore](http://java7fs.wikia.com/wiki/Why_File_sucks) – fge Mar 25 '16 at 17:47
  • Can you try using `file://localhost/Users/jian/Public/logfiles/Hadoop/hdp_log1.log` or just `file:///Users/jian/Public/logfiles/Hadoop/hdp_log1.log` – abhisheknirmal Mar 25 '16 at 17:48
  • @nickspol file:///Users/jian/Public/logfiles/Hadoop/hdp_log1.log works. but I need to connect to remote IP address finally, so I need to add IP address somewhere – Jian Mar 25 '16 at 17:51
  • @fge can you give me a example to read remote file using Path ? – Jian Mar 25 '16 at 17:52
  • @nickspol this link is too much .......... – Jian Mar 25 '16 at 17:53
  • What you want to do is impossible. basically for that to be possible, the remote system must expose its file system through some kind of service, which will look for incoming requests and returns the response back. As you are a mac user, take a look at NFS (Network file system). – abhisheknirmal Mar 25 '16 at 17:57
  • @nickspol impossible _with `File`_. If you use JSR 203, provided you have the implementation for it, then it becomes possible. – fge Mar 25 '16 at 17:58
  • Correct @fge. I think its better to approach this problem by some other means, like a script (ssh, xcopy) to get the file first and then make the java program to read the file returned by the script and do required operations. – abhisheknirmal Mar 25 '16 at 18:01
  • Possible duplicate of http://stackoverflow.com/questions/26555591/create-new-fileuri-with-authority-component – Raedwald Mar 25 '16 at 18:25

2 Answers2

0

File cannot be used to read "remote files". It is meant to be used for filesystem objects which are only local to the machine, or to be more precise, accessible from the local filesystem.

It so happens that with Unix systems you can have mount points to remote filesystems, for instance an NFS mount, or a FUSE mount. But those will always be seen as "local".

If what you want is to access a remote resource to be viewed as a "file", then File can't do it.

But JSR 203 can. See here for an example.

Therefore, you first need to define what you mean by a "remote file". It entirely depends on the protocol you use. And File cannot do it. Unless the OS has sorted it for you (that is, as mentioned above, you have an NFS mount, a FUSE mount or other).


As to your error, the File constructor warns you that there is an authority component in your URI; and there is: 127.0.0.1. As File expects nothing but a path component to the URI (in addition to the URI scheme, file), this explains the error.

fge
  • 119,121
  • 33
  • 254
  • 329
  • thanks. remote file, I mean, NFS. can I do it with `Path`? – Jian Mar 25 '16 at 17:59
  • You could, if there were a JSR203 implementation for it... As far as I know there isn't one. Therefore your best resort here is to mount it at the OS level and work with that. Note that you can view the "mount points" in Java by querying the different `FileStore`s available: if your NFS server is mounted, then you can see it as a filestore. See `FileSystems.getDefault().getFileStores()`. – fge Mar 25 '16 at 18:07
  • I edit my post, can you give me some new feedback? thanks – Jian Mar 25 '16 at 18:51
0

Your requirement can easily be fulfilled by some java FTP library.

Take a look at ftp4j, its a good library for upload and download of files and also, for directory listings.

Apache commons-net is also a good option. They have some really good sample programs too.