Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basics - reading/writing remote files using Java

I started with requirement of reading and writing files in from/in a directory on a remote Ubuntu machine.

First, I wrote a Java program that could read,write files from a shared folder on a remote Windows machine i.e on a LAN. Here, something like this works on my(local) Windows machine :

File inputFile = new File(
                "\\172.17.89.76\EBook PDF");/*ignore the syntax errors, the loc is just for the idea*/

Now when I consider a remote Ubuntu machine, obviously I cannot do something like this as the machine is not on the LAN(I'm not sure if that can be done even if it is on the LAN!). Hence, I tried following approaches :

  1. Using Jsch, establishing the trust between two machines(local - remote Linux , remote Linux - remote Linux) and file writing using sftp.(done)
  2. Running sockets on the two machines - one sender, one receiver(both Java)(done)
  3. Attempting to achieve I/O alike the code snippet for Windows (LAN) machines(not achieved)

While doing all these, I had many queries, read many posts etc. and I felt that I'm missing something on the fundamentals :

  • Some sort of trust-building(between two machines) utility will be required to achieve IO. But finally, I want to write a code like the snippet given, irrespective of the machines, network etc.
  • The Jsch solution and the others suggested(usage of http, ftp etc. over URL) finally are using some services that are running on the remote machine. In other words, it is NOT THAT Java IO is being used to access the remote file system - this doesn't appeal to me as I'm relying on services rather than using good-old I/O.
  • Samba, SSHFS too popped onto the scene, only to add to my confusion. But I don't see them as the solutions to my objective !

To reiterate, I want to write a code using Java I/O(either plain or nio, both are fine) which simply can read, write remote files without using services over protocols like ftp, http etc. or socket sender-receiver model. Is my expectation valid?

  • If not, why and what is the best I can do to read/write remote files using Java?
  • If yes, how to achieve the same !

P.S : Please comment in case I need to elaborate to pose my question accurately !

like image 810
Kaliyug Antagonist Avatar asked Sep 27 '12 06:09

Kaliyug Antagonist


1 Answers

To answer your question - No, your expectation isn't valid.

Retrieving files from a remote server is inherently reliant on the services running on that server. To retrieve a file from a remote server, the remote server needs to be expecting your request for a file.

The cases you listed in your question (using jsch and sftp, using a sender and receiver Java sockets) that you have achieved already, are essentially the same as this:

File inputFile = new File(
            "\\172.17.89.76\EBook PDF");

The only difference is that Java is using the native os's built in support for reading from a windows style share. The remote windows machine has a sharing service running on it (just like Samba on linux, or a java socket program) waiting for your request.

From the Java API docs on File (http://docs.oracle.com/javase/6/docs/api/java/io/File.html)

The canonical pathname of a file that resides on some other machine and is accessed via a remote-filesystem protocol such as SMB or NFS ...

So essentially "Good old Java I/O" is more or less just a wrapper over some common protocols.

To answer the second part of your question (what is the best I can do to read/write remote files using Java?), that depends on what remote system you are accessing and, more importantly, what services are running on it.

In the case of your target remote machine being an Ubuntu machine, I would say the best alternative would be to use Jsch. If your target machine can be either a windows machine or a linux machine, I would probably go for running Java sockets on the two machines (obviously dependant on whether you have access to installing your app on the remote machine).

Generally speaking, go with the common lowest denominator between your target systems (in terms of file sharing protocols).

like image 126
Lawrence Avatar answered Oct 06 '22 00:10

Lawrence