8

I have shared a folder on my server using Windows sharing. On another computer, where I am running my code on, I have mapped a network drive pointing to that folder.

In my code, I transfer files from my local computer to my server every now and then. Something like this:

File srcFile = new File("C:\\test.mpg");
File destFile = new File(...);

// error checking
FileUtils.moveFile(srcFile, destFile);

For destFile, which approach should I use? My current approach:

File destFile = new File("Z:\\folder\\test.mpg");

or using a network path:

File destFile = new File("\\192.168.123.123\\folder\\test.mpg");

I ask this because recently I have encountered cases where the file transfer fails because my program is unable to write to my network drive because it is not logged on, and I have to manually go to the drive and enter my credentials and enable "Stay connected" option.

ohseekay
  • 795
  • 3
  • 20
  • 37

2 Answers2

8

You can use mapped drives or full network paths equivalently; Java doesn't care and just passes the file name on to the OS. Note that if you're using a network path, you need \\\\ at the beginning.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
8

You can use the JCIFS library to access a Windows SMB share in Java. Using it, you could do something like the following:

String smbUrl = "smb://username:password@server/share/file";
SmbFileOutputStream fos = new SmbFileOutputStream(new SmbFile(smbURL));
Ahsan Shah
  • 3,931
  • 1
  • 34
  • 48
  • 1
    if you have domain then you have to use it like domain;user:password e.g String smbUrl = "smb://domain;username:password@server/share/file"; – Ishan Liyanage Oct 02 '13 at 04:43
  • but how we will be moving file from source to destination ? I think your code tells us to connect password protected folder but how to move a file to that folder, As FileUutils.moveFile() accecpt parameter of File type only not of SmbFile – Muddassir Rahman Apr 18 '20 at 07:51