2

In my case i need to copy a file from local folder to shared location.

Files.copy(new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\Tulips.jpg").toPath(), new File("\\\10.101.1.2\\resources\\Files\\exbury\\Tulips.jpg").toPath(),
                    java.nio.file.StandardCopyOption.REPLACE_EXISTING);

java.nio.file.InvalidPathException: Illegal char <> at index 1: \.101.1.2\ZoneResources\File Share\burusoth\Tulips.jpg at sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:182) at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:153) at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77) at sun.nio.fs.WindowsPath.parse(WindowsPath.java:94) at sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:255) at java.io.File.toPath(File.java:2234) at com.zone.qv2.s2c.resultupload.TestClass.method(TestClass.java:31) at com.zone.qv2.s2c.resultupload.TestClass.main(TestClass.java:22)

It means NIO doesn't allow slashes \ in front of a path as said in this question. In my case i have to specify shared location as url which starts with slashes. How can i overcome this issue?

Is there are any ways to copy files from local location to shared location?

Community
  • 1
  • 1
Bruce
  • 8,609
  • 8
  • 54
  • 83

1 Answers1

3

The Java String value you are using for your UNC path is:

\\\10.101.1.2\\resources\\Files\\exbury\\Tulips.jpg

A UNC path normally takes the form of:

\\10.101.1.2\resources\Files\exbury\Tulips.jpg  

Each slash \ must be escaped as \\ in a Java String.

The Java String value of the resulting path should be:

\\\\10.101.1.2\\resources\\Files\\exbury\\Tulips.jpg

You are missing a preceding \ character.

Using / also works and does not need to be escaped; the Java String value using / is:

//10.101.1.2/resources/Files/exbury/Tulips.jpg

JoshDM
  • 4,939
  • 7
  • 43
  • 72