• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2616
  • Last Modified:

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

0
nicoleheritage
Asked:
nicoleheritage
  • 5
  • 4
  • 4
1 Solution
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 5
  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now