Write a file in a remote location using Java with a network path or drive?

I shared a folder on my server using Windows sharing. On another computer, where I run my code, I mapped a network drive pointing to this folder.

In my code, I periodically transfer files from the local computer to my server. Something like that:

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 the network path:

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

I ask about this because recently I came across cases where the file transfer failed, because my program cannot write to my network drive, because it was not logged in, and I need to manually go to the drive and enter my credentials data and enable "Stay Connected."

+4
source share
2 answers

You can use mapped drives or full network paths equivalently; Java doesn't care and just passes the file name to the OS. Please note: if you use the network path, you need \\\\ at the beginning.

+5
source

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

 String smbUrl = "smb://username: password@server /share/file"; SmbFileOutputStream fos = new SmbFileOutputStream(new SmbFile(smbURL)); 
+3
source

Source: https://habr.com/ru/post/1497161/


All Articles