Link to home
Start Free TrialLog in
Avatar of gnazzoj
gnazzoj

asked on

Mapping an unknown drive in Java

I want to connect to a remote computer on the network that is NOT currently Drive mapped to my local computer.
I then want to copy a file from my local computer to the remote computer.
The code I have been trying to use is this:

 URI fileURI1 = new URI("file:////USERID:PASSWORD@REMOTEHOSTNAME/SHARENAME/myfile.txt");
      File fileTemp = new File(fileURI1);
      OutputStream out = new FileOutputStream(fileTemp);

I get the following exception
java.io.FileNotFoundException: \\USERID:PASSWORD@REMOTEHOSTNAME\SHARENAME\myfile.txt (The network path was not found)

WHat am I doing wrong?
Avatar of KeithWatson
KeithWatson

This isn't good news I'm afraid...

I tried to do something similar a number of months ago and came to the ultimate conclusion that it couldn't be done. In looking up the specification of the URI, it's not clear what's stated; it says file URIs are similar to FTP URIs, but no specific mention is made of usernames and passwords. I think it's legal URI syntax, but I don't think it's implemented in the JDK (although I couldn't confirm this in any other way other than the fact that it didn't work). I tried three different approaches; all failed. The only way I could get it to work (which I think you've hinted at already), is that if you map a drive to the remote location, it works - even if you use the full UNC path rather than the mapped drive. I guess Windows must automatically be passing the credentials.

Sorry that's of little help, but unless someone else can come up with an idea that works, at least it may save you additional effort. I wound up placing an FTP server on the target machine and just using FTP to access the file.

Keith.
Open up My Computer and try doing the following,

\\USERID:PASSWORD@REMOTEHOSTNAME\SHARENAME\

Tell me what it says.

AJ
ASKER CERTIFIED SOLUTION
Avatar of sbrown2k
sbrown2k

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It should solve your problems, and I forgot to mention the most important part ... it's open source and free.