0

I am trying to copy a file which is at remote machine shared folder using following code.

myMethod (String paramUrl) {    //URL of the folder shared on Remote machine
    logger.info("paramUrl="+paramUrl);
    URL url = new URL(paramUrl);
    logger.info("Source file address="+url.getFile());
    File inFile = new File(url.getFile());                      
    String destFileName = "D://LOCAL_FOLDER+"//"+xyz.xml;
    logger.info("destFileName="+destFileName);                      
    File destFile = new File(destFileName);                     
    FileUtils.copyFile(inFile, destFile);
}

::::output is:::::::

paramUrl=file:////10.0.0.3//SHARED_FOLDER//xyz.xml
Source file address=////10.0.0.3//SHARED_FOLDER//xyz.xml
destFileName=D:/LOCAL_FOLDER//xyz.xml
java.io.FileNotFoundException: Source '\\10.0.0.3\SHARED_FOLDER\xyz.xml' does not exist
    at org.apache.commons.io.FileUtils.copyFile(FileUtils.java:637)

Note:

If I try to access the URL "\\10.0.0.3\SHARED_FOLDER" it works fine using RUN (in Windows). Folder shared with read/write permission for everyone.

Also If I share local-folder and tried to access from same machine using URL "////localhost//LOCAL_SHARED_FOLDER/xyz.xml" my code works fine.

But I do not know, why it is not working for remote-machine ip-address,Please guys help me why it is not working

1 Answers1

0

Edit: leave out the "file://" part. If you address the file as paramUrl=//10.0.0.3/SHARED_FOLDER/xyz.xml, the copy operation should work.

old:

If you mount \\\10.0.0.3\SHARED_FOLDER to a local drive (e.g. X:), you can pass the parameter

paramUrl=X:\xyz.xml

and you can copy the file because it is behaves a local file.

If you cannot mount the remote drive in Windows as a local drive, have a look at http://jcifs.samba.org/. There's an Java API how to deal with smb/samba shares.

Alexander
  • 2,925
  • 3
  • 33
  • 36
  • I also tried mapping shared folder as local drive. in that case also i was getting same exception. but same code works fine with actual local folder. I have written this code to work for local-folder and Shared-Folder both. – Abhishek Sharma Feb 25 '15 at 16:00
  • One more thing is that url.getFile() will change the URL to "\\10.0.0.3\SHARED_FOLDER\xyz.xml" and remove flie:// part as you can see in exception. Above code is able to move a file from local-folder to another local-folder, but it is surprising to me why it is not working with folder that is is there in mapped drive. – Abhishek Sharma Feb 25 '15 at 16:06
  • Yea, in this case skip the `new URL(paramUrl)` part, but directly use `new File(paramUrl)`. e.g. `new File("//10.0.0.3/SHARED_FOLDER/xyz.xml")` – Alexander Feb 25 '15 at 16:10
  • No it was not working even for local-system I have tested that before. That time I was using *new File("//Loca-IP-Address/LOCAL_SHARED_FOLDER/xyz.xml")* . One more thing why mapped-drive and local-drive behave differently. – Abhishek Sharma Feb 25 '15 at 16:17
  • works on my machine. BTW: do you run the code on a linux box? – Alexander Feb 25 '15 at 16:31
  • no it is windows. Also I found one issue that when I run it as a windows Service it is not working. But from command line it works fine now. – Abhishek Sharma Feb 25 '15 at 17:04