How to access to a remote host?

A

alfredo.amato

Hi to all!
I'm new of Java and I'm trying to develope some application to improve
myself.
I have just built a simple application that, by class File(), displays
content of my (local) file system..
C:\
C:\Folder1
C:\Folder2
....
....
C:\FolderN

Now, I'm trying to access to an host on the local net to do the same
but I don't know
from where I have to start... I read about URI but I ask you.. Is it
need? What must I use?
Thank you very much in advance for help.
Alfredo
 
C

Carl

Alfredo,

If you want to try to access a network share and know the UNC path, you
can simply use the java.io.File object to get a list of the share
directory contents.

For example, suppose your server is name "myserver" and the share is
"myshare"...

// the unc path is '\\myserver\myshare\', the '\' should be escaped in
the string.
final String uncPath = "\\\\myserver\\myshare\\";
File f = new File(uncPath);
if (f.isDirectory()) {
String[] listing = f.list();
for (int x=0; x<listing.length; x++){
System.out.println(listing[x]);
}
}
 
M

Martin Gregorie

I'm new of Java and I'm trying to develope some application to improve
myself.
I have just built a simple application that, by class File(), displays
content of my (local) file system.. ....
Now, I'm trying to access to an host on the local net to do the same
but I don't know from where I have to start...
If you're wanting a general way of reading files on a remote machine
without using a share, you need the write a server to run on the remote
machine and a client to run on your local machine. You use the Socket
class to allow the client to talk to the server.

This is a big topic: before tackling it you need to know a bit about
TCP/IP networks and the Sockets concept in order to understand what the
Socket class methods do.
 
A

Alf_Italy

Thank you Carl!!! your solution seems working...but..It requires a
previous access to the server...
Do you know if there's a way to pass credentials as argument in the
uncPath???
 
C

Carl

Alf_Italy said:
Thank you Carl!!! your solution seems working...but..It requires a
previous access to the server...
Do you know if there's a way to pass credentials as argument in the
uncPath???

Alfredo,

That is a very good question, and unfortunately I don't have a simple
answer.
The short answer is no, I don't believe that there is any way to
elevate your user privileges when using File, and I am unaware of any
method to pass credentials in the UNC path.

If the security of the data you are transmitting is important I would
suggest you look to another method ( SFTP? ). Otherwise you will need
to be accessing a directory/file which the application user already has
access to (does the program run in a windows domain?), or an
unauthenticated share.

Carl.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top