Access network drive through JAVA (while windows XP screen locked)

I am trying access a shared network drive through JAVA. If I am logged in to Windows XP, it works fine. I am using Eclipse. When I create jar file and automate the process for the times that my desktop is locked, it gives "Access is denied" exception.

I looked to the knowledge base, but couldn't find my solution.

The error is:

java.io.FileNotFoundException: \\serverName\FileName.csv (Access is denied)

How can I get around this error? Is there a way to login to a network drive as if I am logging in to a JDBC drive?
package utilities;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.mail.MessagingException;
 
public class CopyFile {
 
	protected String myFromFileDirectory;
	protected String myDestinationFileDirectory;
	
	public CopyFile(String fromFileName,String toFileName) throws MessagingException{
		Email em = new Email();
		myFromFileDirectory = fromFileName;
		myDestinationFileDirectory = toFileName;
		 try {
			 File fromFile = new File(fromFileName);
			 File toFile = new File(toFileName);
			 FileInputStream from = null;
			 FileOutputStream to = null;
			 from = new FileInputStream(fromFile);
			 to = new FileOutputStream(toFile);
			 byte[] buffer = new byte[4096];
			 int bytesRead;
			 while ((bytesRead = from.read(buffer)) != -1)
			 to.write(buffer, 0, bytesRead); // write	  	
		  } catch (IOException e) {
		    System.err.println(e.getMessage());
		    em.postExceptionToMe("myEmail@hotmail.com", "File Transfer Exception","myEmail@hotmail.com", e);
		  }
		 }
	public static void main(String[] args) {
		String from="D:\\FileName.csv";
		String to="\\\\serverName\\FileName.csv";
		try {
			new CopyFile(from,to);
		} catch (MessagingException e) {
	           e.printStackTrace();
		}
	}
	}

Open in new window

nicoleheritageAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

darron_chapmanCommented:
String to="\\\\serverName\\FileName.csv"  ... you most likely mean

String to="\\\\serverName\\SHARENAME\FileName.csv"

You have to specify a sharename when referencing a UNC path...
0
darron_chapmanCommented:
sorry, missed the second backslash between sharename and filename.csv

String to="\\\\serverName\\SHARENAME\\FileName.csv"
0
edster9999Commented:
Are you happy to view / edit the registery ?

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa

set
restrictanonymous
from 1 to 0

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

nicoleheritageAuthor Commented:
Sorry about that, I do have a SHARENAME in my code. It looks like what you have posted. Problem continues.

0
darron_chapmanCommented:
When you automate it, what are you using?  if it's scheduled tasks, you might want to go to Properties and on the Task tab at Run as: make sure your username is in there, then click Set password... and type in your password.
0
nicoleheritageAuthor Commented:
to edster9999:

"restrictanonymous" was 0, so I didn't change it, but I had another entry called "restrictanonymoussam". It was 1, I changed that one also  to 0.

And now it works!!!

Great!!!

Thank you so much edster9999!!!

0
edster9999Commented:
You are welcome.

You can also get round this by specifing a login and going that way - then it will work without updating that.
It is a security feature to lock down anonymous attachments while the screen saver is active and the system is locked.
0
darron_chapmanCommented:
I agree... specifying a login is probably the better option.  There's a reason why that's set in the registry, and unsetting it could potentially open your computer up to attack.
0
nicoleheritageAuthor Commented:
Then, how can I specify a login?
0
edster9999Commented:
Not really.... When you are logged in like you are now you have shares that can be read by 'anonymous'.  
I've never really seen why you would want this more secure when logged off or on screen saver.
If something is open all day what good would it be to lock it while you are out on your lunch break.  If someone wants to access it they can do it in an hour.

(just my thoughts)
0
darron_chapmanCommented:
Good info... thanks edster9999
0
edster9999Commented:
I'm not a java expert so maybe someone else could tell nicoleheritage (author) how to specify username (and password) in this type of transaction.
0
nicoleheritageAuthor Commented:
to edster9999:

Please see question titled:

"Access network drive through JAVA (when logged off)"

thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Server Software

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.